671e9e8f3d8a3e4998d967d8dca658d4de8bcca2
[evolisprinter.git] / evolis.c
1
2 /*
3  *
4  * Contents:
5  *
6  */
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "evoliserror.h"
11
12 // Macros...
13 #define RgbToGray(r, g, b) (((long) (r)*74L + (long)(g)*155L +(long)(b)*27L) >> 8)
14
15 // Globals...
16 int           STATUS=0; //0 means succes, 1 means failure
17 unsigned char *lpMem;
18 unsigned char *lpRecto,*lpVerso;
19 int           Model;            /* Model number */
20 long          dwSizeNeeded;     // taille mmoire du lpMem en octets
21
22 char
23 OverlayPannel[10],
24 OverlayBackPannel[10],
25 TreatementK[2];
26
27 int TB,LB,BB,RB,TW,LW,BW,RW;
28
29 int colorspace,levelB;
30
31 // *3.5
32 int DitherPattern1[8][8] = {
33     {0, 112, 28, 140, 7, 119, 35, 147},
34     {168, 56, 196, 84, 175, 63, 203, 78},
35     {42, 154, 14, 126, 49, 161, 21, 114},
36     {210, 98, 182, 70, 217, 105, 189, 77},
37     {11, 123, 39, 151, 4, 116, 32, 143},
38     {179, 67, 207, 95, 172, 59, 200, 88},
39     {53, 165, 24, 137, 46, 158, 18, 111},
40     {221, 109, 193, 81, 214, 102, 159, 74}
41 };
42
43 // Prototypes...
44
45 // Compress and format data for the printer
46 long ReduceBlack(unsigned char *lpMemIn, unsigned char *lpMemOut,int nbrline);
47 long ReduceColor(unsigned char *lpMemIn, unsigned char *lpMemOut, int uiBitComp);//,int nbrline);
48 //int CutPage(long *stop);
49
50 // Download functions
51 int DBNC(int col,int bl,int ov);//,int line);
52 int DB32NC(long lPos, char color);  //y,m,c pannels 5 bits per color
53 int DB64NC(long lPos, char color);  //y,m,c pannels 6 bits per color
54 int DB128NC(long lPos, char color); //y,m,c pannels 7 bits per color
55 /*int DB32NCS(long lPos, char color,int line);  //y,m,c pannels 5 bits per color
56  int DB64NCS(long lPos, char color,int line);   //y,m,c pannels 6 bits per color
57  int DB128NCS(long lPos, char color,int line);  //y,m,c pannels 7 bits per color*/
58 int DB2NC(long lPos, char pannel[10]);  // k,o panel 2 levels
59 int DB2MNC(long lPos);      // k panel 2 levels
60
61 // Convert RVB to k functions
62 void RVBtoGray(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte);
63 void KinYMC(long Height, long Width);
64 void GrayToFloyd(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height);
65 void GrayToDither(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height);
66 void GrayToThreshold(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte);
67 void ConvertRVBtoK(unsigned char *lpbRVB, long RVBSize, long Height, long Width, unsigned char *lpBlack,int face);
68 void GetFirstYMCDot(long Height, long Width , int *x, int *y);
69
70
71 //===============================================================================//
72 // Reduce color data from 8 bits to uiBitComp.
73
74 // Entree : 1 color panel (1016 * 648 octets)
75 // Sortie : 1 color panel with usable data ((1016*648)*uiBitComp)/8
76 //===============================================================================//
77
78
79 long ReduceColor(unsigned char *lpMemIn, unsigned char *lpMemOut, int uiBitComp)//,int nbrline)
80 {
81     long lIndex = 0, lIndex1 = 0;
82     unsigned char *lpbBuf, *lpbData, *lpbDataOut;
83     long lComp = 0;
84     long lNbrByte = (1016*648);
85     
86     //Reservation m√î√∏Œ©oire pour traitement d'une ligne
87     
88     lpbBuf = malloc(8);
89     
90     if (!lpbBuf)
91     {
92         
93         //fprintf(stderr, "**************** EVOLIS ReduceColor manque de memoire pour lpBuf: taille demande 8 octets... \n");
94         fatal("ReduceColor Fails memory lpBuf: requested size 8 bytes...");
95         
96         /// sortir de l'impression....
97         return (0);
98     }
99     
100     //lpbData ppV mem bmp brute
101     lpbData = &lpMemIn[0];
102     
103     lpbDataOut = &lpMemOut[0];
104     // pour toutes les donnees
105     while (lIndex < lNbrByte)
106     {
107         //Traite byte par 8
108         for (lIndex1 = 0; lIndex1 < 8; lIndex1++)
109         {
110             if (lIndex < lNbrByte)
111             {
112                 //decalage donnees utiles
113                 lpbBuf[lIndex1] = *(lpbData++) >> (8 - uiBitComp);
114                 lIndex++;
115             }
116             else
117             {
118                 
119                 lpbBuf[lIndex1] = 0x00;
120             }
121         }
122         
123         switch (uiBitComp)
124         {
125             case 6:
126                 *(lpbDataOut++) = (lpbBuf[0] << 2) | (lpbBuf[1] >> 4);
127                 *(lpbDataOut++) = (lpbBuf[1] << 4) | (lpbBuf[2] >> 2);
128                 *(lpbDataOut++) = (lpbBuf[2] << 6) | (lpbBuf[3]);
129                 *(lpbDataOut++) = (lpbBuf[4] << 2) | (lpbBuf[5] >> 4);
130                 *(lpbDataOut++) = (lpbBuf[5] << 4) | (lpbBuf[6] >> 2);
131                 *(lpbDataOut++) = (lpbBuf[6] << 6) | (lpbBuf[7]);
132                 break;
133             case 7:
134                 *(lpbDataOut++) = (lpbBuf[0] << 1) | (lpbBuf[1] >> 6);
135                 *(lpbDataOut++) = (lpbBuf[1] << 2) | (lpbBuf[2] >> 5);
136                 *(lpbDataOut++) = (lpbBuf[2] << 3) | (lpbBuf[3] >> 4);
137                 *(lpbDataOut++) = (lpbBuf[3] << 4) | (lpbBuf[4] >> 3);
138                 
139                 *(lpbDataOut++) = (lpbBuf[4] << 5) | (lpbBuf[5] >> 2);
140                 *(lpbDataOut++) = (lpbBuf[5] << 6) | (lpbBuf[6] >> 1);
141                 *(lpbDataOut++) = (lpbBuf[6] << 7) | (lpbBuf[7]);
142                 break;
143                 
144             default:    //5 bits
145                 *(lpbDataOut++) = (lpbBuf[0] << 3) | (lpbBuf[1] >> 2);
146                 *(lpbDataOut++) = (lpbBuf[1] << 6) | (lpbBuf[2] << 1) | (lpbBuf[3] >> 4);
147                 *(lpbDataOut++) = (lpbBuf[3] << 4) | (lpbBuf[4] >> 1);
148                 
149                 *(lpbDataOut++) = (lpbBuf[4] << 7) | (lpbBuf[5] << 2) | (lpbBuf[6] >> 3);
150                 *(lpbDataOut++) = (lpbBuf[6] << 5) | (lpbBuf[7]);
151                 //break;
152         }
153     }
154     free(lpbBuf);
155     lComp = (((lNbrByte * 10) / 8) * uiBitComp);
156     lComp /= 10;
157     if (lNbrByte % 8)
158         lComp++;
159     
160     
161     return (lComp);
162 }
163
164 //===================================================//
165 // fonction pour le panneau noir & overlay uniquement
166 // entree : 1 octect = 1 points
167 // sortie : 1 octect = 8 points 
168
169
170
171 //===================================================//
172 long ReduceBlack(unsigned char *lpMemIn, unsigned char *lpMemOut,int nbrline)
173 {
174     long lIndex = 0, lIndex1 = 0;
175     unsigned char *lpbData, *lpbDataOut;
176     long lComp = 0;
177     unsigned char bBuf = 0x00;
178     
179     unsigned char Mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
180     long lNbrByte = nbrline * 648;
181     
182     
183     //lpbData ppV mem bmp brute
184     lpbData = &lpMemIn[0];
185     lpbDataOut = &lpMemOut[0];
186     // pour toutes les donnees
187     
188     while (lIndex < lNbrByte)
189     {
190         
191         
192         //Traite byte par 8
193         for (lIndex1 = 0; lIndex1 < 8; lIndex1++)
194             
195         {
196             if (lIndex < lNbrByte)
197             {
198                 if (*(lpbData++) == 0xFF)
199                 {
200                     bBuf |= Mask[lIndex1];
201                 }
202                 lIndex++;
203             }
204         }
205         *(lpbDataOut++) = bBuf;
206         bBuf = 0x00;
207         lComp++;
208     }
209     return (lComp);
210 }
211
212 //=====================================================================
213 //   Convert bitmap RVB data to evolis data format                             
214 //=====================================================================
215
216
217 int DBNC(int col,int bl,int ov)//,int line)
218 {
219     //output("\033Ss\015"); // debut sequence + CR
220     
221     if(col == 1) {
222         DB32NC(0 * (dwSizeNeeded / 5), 'y');
223         DB32NC(1 * (dwSizeNeeded / 5), 'm');
224         DB32NC(2 * (dwSizeNeeded / 5), 'c');
225     }
226     else if (col == 2) {
227         DB64NC(0 * (dwSizeNeeded / 5), 'y');
228         DB64NC(1 * (dwSizeNeeded / 5), 'm');
229         DB64NC(2 * (dwSizeNeeded / 5), 'c');
230     }
231     else if (col == 3) {
232         DB128NC(0 * (dwSizeNeeded / 5), 'y');
233         DB128NC(1 * (dwSizeNeeded / 5), 'm');
234         DB128NC(2 * (dwSizeNeeded / 5), 'c');
235     }
236     
237     if(bl){
238         DB2NC(3 * (dwSizeNeeded / 5), "ABP");
239     }
240     
241     if(ov){
242         if(ov == 1){//overlay at front
243             DB2NC(0, OverlayPannel);
244         }
245         else {
246             DB2NC(0, OverlayBackPannel);
247         }
248     }
249     
250     //output("\033Se\015.....................................");    // fin sequence + CR
251     
252 }
253
254 //===============================================================//
255 // Telechargement d'un panneau non compresse 5 bits
256 // Input : ucData / raw data
257 //         color /y,m or c      
258 // Return:      0 si OK
259 //              -1 si erreur ecriture  
260 //===============================================================//
261 int DB32NC(long lPos, char color)
262 {
263     unsigned char *ucCommand, *ucCol;
264     long lCommandSize = ((1016 * 648) * 5) / 8;
265     long lComp = 0;
266     int  numwritten = 0;
267     
268     //Reservation memoire pour une commande de telechargement
269     ucCommand = malloc(lCommandSize + 12);
270     if (!ucCommand) {
271         fatal("DP32NC Error: Fails malloc... ");
272         return (-1);
273     }
274     
275     ucCol = &lpMem[lPos];
276     strcpy((char*)  ucCommand, "\033Db;y;32;");
277     strncpy((char*) &ucCommand[4], &color, 1);
278     
279     lComp = ReduceColor(ucCol, &ucCommand[9], 5);//,1016);
280     lCommandSize += 10;
281     numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
282     if (numwritten != lCommandSize) {
283         fatal("DP32NC Error: Fails printf %d... \n", numwritten);
284         free(ucCommand);
285         return (-1);
286     }
287     /*lComp += 10;
288      while(lComp > 4096) {
289      numwritten += fwrite(&ucCommand[numwritten], sizeof(unsigned char), 4096, stdout);
290      lComp -= 4096;     
291      }
292      debug("NuwWritten %d", numwritten);
293      if(lComp > 0) {
294      numwritten += fwrite(&ucCommand[numwritten], sizeof(unsigned char), lComp, stdout);
295      }
296      debug("lComp %d numwritten %d lCommandSize %d", lComp,numwritten,lCommandSize);
297      if (numwritten != lCommandSize) {
298      fatal("DP32NC Error: Fails printf %d... \n", numwritten);
299      free(ucCommand);
300      return (-1);
301      }*/
302     
303     
304     free(ucCommand);
305     return (0);
306 }
307
308 //===============================================================//
309 // Telechargement d'un panneau non compresse 6 bits
310 // Input : ucData / raw data
311 //         color /y,m or c      
312 // Return:      0 si OK
313 //              -1 si erreur ecriture  
314 //===============================================================//
315
316 int DB64NC(long lPos, char color)
317 {
318     unsigned char *ucCommand, *ucCol;
319     
320     
321     long lCommandSize = ((1016 * 648) * 6) / 8;
322     long lComp;
323     int numwritten = 0;
324     
325     
326     //Reservation memoire pour une commande de telechargement
327     ucCommand = malloc(lCommandSize + 10);
328     debug("DP64NC Size for one panel: %d... \n", (lCommandSize + 10));
329     
330     if (!ucCommand)
331     {
332         
333         fatal("DP64NC Error: Fails malloc...");
334         
335         free(ucCommand);
336         return (-1);
337         
338     }
339     ucCol = &lpMem[lPos];
340     strcpy((char*) ucCommand, "\033Db;y;64;");
341     strncpy((char*) &ucCommand[4], &color, 1);
342     
343     
344     lComp = ReduceColor(ucCol, &ucCommand[9], 6);//,1016);
345     //fprintf(stderr, "**************** EVOLIS DP64NC taille apres compression %d... \n",lComp);
346     strncpy((char*)&ucCommand[lCommandSize + 9], "\015", 1);
347     lCommandSize += 10;
348     numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
349     if (numwritten != lCommandSize)
350     {
351         fatal("DP64NC Error: Fails fwrite %d... \n", numwritten);
352         
353         free(ucCommand);
354         return (-1);
355     }
356     
357     free(ucCommand);
358     
359     return (0);
360 }
361
362
363 //===============================================================//
364 // Telechargement d'un panneau non compresse 7 bits
365 // Input : ucData / raw data
366 //         color /y,m or c      
367 // Return:      0 si OK
368 //              -1 si erreur ecriture  
369 //===============================================================//
370 int DB128NC(long lPos, char color)
371
372 {
373     unsigned char *ucCommand, *ucCol;
374     long lCommandSize = ((1016 * 648) * 7) / 8;
375     long lComp;
376     int numwritten = 0;
377     
378     //Reservation memoire pour une commande de telechargement
379     ucCommand = malloc(lCommandSize + 11);
380     debug("DP128NC Size for one panel: %d... ", (lCommandSize + 11));
381     if (!ucCommand)
382     {
383         fatal("DP128NC Error: Fails malloc... ");
384         
385         free(ucCommand);
386         return (-1);
387     }
388     ucCol = &lpMem[lPos];
389     strcpy((char*) ucCommand, "\033Db;y;128;");
390     strncpy((char*) &ucCommand[4], &color, 1);
391     lComp = ReduceColor(ucCol, &ucCommand[10], 7);//,1016);
392     strncpy((char*) &ucCommand[lCommandSize + 10], "\015", 1);
393     lCommandSize += 11;
394     numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
395     if (numwritten != lCommandSize)
396     {
397         fatal("DP128NC Error: Fails fwrite %d... \n", numwritten);
398         
399         free(ucCommand);
400         return (-1);
401     }
402     free(ucCommand);
403     return (0);
404 }
405 //===============================================================//
406 // Telechargement d'un panneau non compresse 5 bits YMCKOS
407 // Input : ucData / raw data
408 //         color /y,m or c
409 // Return:      0 si OK
410 //              -1 si erreur ecriture
411 //===============================================================//
412 /*int DB32NCS(long lPos, char color,int line)
413  {
414  unsigned char *ucCommand, *ucCol;
415  long lCommandSize = ((1016 * 648) * 5) / 8;
416  long lComp = 0;
417  int numwritten = 0;
418  
419  //Reservation memoire pour une commande de telechargement
420  ucCommand = malloc(lCommandSize + 20);
421  //fprintf(stderr, "**************** EVOLIS DP32NC Size for one panel: %d... \n", (lCommandSize+10));
422  if (!ucCommand)
423  {
424  //fprintf(stderr, "**************** EVOLIS DP32NC Error: Fails malloc... \n");
425  fatal("DP32NC Error: Fails malloc... ");
426  
427  return (-1);
428  }
429  ucCol = &lpMem[lPos];
430  //strcpy(ucCommand, "\033Dbc;y;32;");
431  //nbroct = 540 * 648;
432  lCommandSize = ((520 * 648) * 5) / 8;
433  sprintf(ucCommand, "\033Dbc;y;32;%.4d;%.6d;",line,lCommandSize);
434  strncpy(&ucCommand[5], &color, 1);
435  
436  info("%s",ucCommand);
437  lComp = ReduceColor(ucCol, &ucCommand[22], 5, 520);
438  strncpy(&ucCommand[lCommandSize + 22], "\015", 1);
439  lCommandSize += 23;
440  
441  numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
442  //info("%d  %s", numwritten,ucCommand);
443  if (numwritten != lCommandSize)
444  {
445  //fprintf(stderr, "**************** EVOLIS DP32NC Error: Fails printf %d... \n",numwritten);
446  fatal("DP32NC Error: Fails printf %d... \n", numwritten);
447  free(ucCommand);
448  return (-1);
449  }
450  free(ucCommand);
451  return (0);
452  }
453  
454  //===============================================================//
455  // Telechargement d'un panneau non compresse 6 bits YMCKOS
456  // Input : ucData / raw data
457  //         color /y,m or c
458  // Return:      0 si OK
459  //              -1 si erreur ecriture
460  //===============================================================//
461  
462  int DB64NCS(long lPos, char color,int line)
463  {
464  unsigned char *ucCommand, *ucCol;
465  
466  long lCommandSize = ((1016 * 648) * 6) / 8;
467  long lComp;
468  int numwritten = 0;
469  
470  
471  
472  //Reservation memoire pour une commande de telechargement
473  ucCommand = malloc(lCommandSize + 10);
474  debug("DP64NC Size for one panel: %d... \n", (lCommandSize + 10));
475  
476  if (!ucCommand)
477  {
478  
479  fatal("DP64NC Error: Fails malloc...");
480  
481  free(ucCommand);
482  return (-1);
483  
484  }
485  ucCol = &lpMem[lPos];
486  strcpy(ucCommand, "\033Db;y;64;");
487  strncpy(&ucCommand[4], &color, 1);
488  
489  
490  lComp = ReduceColor(ucCol, &ucCommand[9], 6, 540);
491  //fprintf(stderr, "**************** EVOLIS DP64NC taille apres compression %d... \n",lComp);
492  strncpy(&ucCommand[lCommandSize + 9], "\015", 1);
493  lCommandSize += 10;
494  numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
495  if (numwritten != lCommandSize)
496  {
497  fatal("DP64NC Error: Fails fwrite %d... \n", numwritten);
498  
499  free(ucCommand);
500  return (-1);
501  }
502  
503  free(ucCommand);
504  
505  return (0);
506  }
507  
508  
509  //===============================================================//
510  // Telechargement d'un panneau non compresse 7 bits YMCKOS
511  // Input : ucData / raw data
512  //         color /y,m or c
513  // Return:      0 si OK
514  //              -1 si erreur ecriture
515  //===============================================================//
516  int DB128NCS(long lPos, char color,int line)
517  
518  {
519  unsigned char *ucCommand, *ucCol;
520  long lCommandSize = ((1016 * 648) * 7) / 8;
521  long lComp;
522  int numwritten = 0;
523  
524  //Reservation memoire pour une commande de telechargement
525  ucCommand = malloc(lCommandSize + 11);
526  //fprintf(stderr, "**************** EVOLIS DP128NC Size for one panel: %d... \n", (lCommandSize+11));
527  debug("DP128NC Size for one panel: %d... ", (lCommandSize + 11));
528  
529  
530  if (!ucCommand)
531  {
532  fatal("DP128NC Error: Fails malloc... ");
533  
534  free(ucCommand);
535  return (-1);
536  }
537  ucCol = &lpMem[lPos];
538  strcpy(ucCommand, "\033Db;y;128;");
539  strncpy(&ucCommand[4], &color, 1);
540  lComp = ReduceColor(ucCol, &ucCommand[10], 7, 540);
541  //fprintf(stderr, "**************** EVOLIS DP128NC taille apres compression %d... \n",lComp);
542  strncpy(&ucCommand[lCommandSize + 10], "\015", 1);
543  lCommandSize += 11;
544  numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
545  if (numwritten != lCommandSize)
546  {
547  fatal("DP128NC Error: Fails fwrite %d... \n", numwritten);
548  
549  free(ucCommand);
550  return (-1);
551  }
552  free(ucCommand);
553  return (0);
554  } */
555 //===============================================================//
556 // Telechargement d'un panneau non compresse overlay noir
557 // Input : ucData / raw datal978
558 //         color /      k: noir
559 //                      f: full varnish 
560 // Return:      0 si OK
561 //              -1 si erreur ecriture  
562 //===============================================================//
563
564 int DB2NC(long lPos, char pannel[10])
565 {
566     unsigned char *ucCommand, *ucCol;
567     long lComp;
568     
569     long lCommandSize = ((1016 * 648)) / 8;
570     int numwritten = 0;
571     int B, T, L, R, i = 0, j = 0;
572     int pB, pT, ModuloB, ModuloT;
573     
574     //unsigned char        MaskB[8] = { 0x00,0x80,0xC0,0xE0,0xF0,0x7F,0x3F,0x1F};
575     //unsigned char        MaskT[8] = { 0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F};
576     unsigned char MaskB[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
577     unsigned char MaskT[8] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };
578     
579     
580     //Reservation memoire pour une commande de telechargement
581     
582     ucCommand = malloc(lCommandSize + 9);
583     
584     //debug("DP2NC Size for one panel: %d... ", (lCommandSize + 9));
585     
586     
587     if (!ucCommand)
588     {
589         fatal("DP2NC Error: Fails malloc... ");
590         free(ucCommand);
591         return (-1);
592     }
593     
594     if (!(strcmp(pannel, "ABP")))// == NULL)
595     {           // panneau noir
596         ucCol = &lpMem[lPos];
597         strcpy((char*) ucCommand, "\033Db;k;2;");
598         lComp = ReduceBlack(ucCol, &ucCommand[8],1016);
599         strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
600         lCommandSize += 9;
601         numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
602         if (numwritten != lCommandSize)
603         {
604             fatal("DP2NC Error: Fails fprintf %d... ", numwritten);
605             
606             free(ucCommand);
607             return (-1);
608         }
609     }
610     else if (!(strcmp(pannel, "NO")))// == NULL)
611     {       //No Varnish
612         memset(ucCommand, 0x00, lCommandSize);
613         strcpy((char*)ucCommand, "\033Db;o;2;");
614         strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
615         lCommandSize += 9;
616         numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
617         if (numwritten != lCommandSize)
618         {
619             fatal("DP2NC Error: Fails fwrite %d... ", numwritten);
620             free(ucCommand);
621             return (-1);
622         }
623     }
624     else if (!(strcmp(pannel, "FO")))// == NULL)
625     {   //Full Varnish
626         memset(ucCommand, 0xFF, lCommandSize);
627         strcpy((char*) ucCommand, "\033Db;o;2;");
628         strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
629         lCommandSize += 9;
630         numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
631         if (numwritten != lCommandSize)
632         {
633             fatal("DP2NC Error: Fails fwrite %d... ", numwritten);
634             free(ucCommand);
635             return (-1);
636         }
637     }
638     else
639     {
640         
641         if (!(strcmp(pannel, "OA")))// == NULL)
642         {
643             //Cover Overlay
644             B = BB * 12;
645             T = TB * 12;
646             L = LB * 12;
647             R = RB * 12;
648             debug("DP2NC OA... ");
649             memset(ucCommand, 0x00, lCommandSize);
650             
651             
652             for (j = L; j <= R; j++)
653                 
654             {
655                 pB = (j * 648) + (648 - B);
656                 ModuloB = pB % 8;
657                 pB /= 8;
658                 if (ModuloB)
659                     pB++;
660                 ucCommand[pB + 7] = MaskB[ModuloB];
661                 pB++;
662                 pT = (j * 648) + (648 - T);
663                 ModuloT = pT % 8;
664                 pT /= 8;
665                 if (ModuloT)
666                     pT++;
667                 ucCommand[pT + 7] = MaskT[ModuloT];
668                 pT--;
669                 for (i = (pB); i <= (pT); i++)
670                 {
671                     ucCommand[i + 7] = 0xFF;
672                 }
673             }
674             B = BW * 12;
675             T = TW * 12;
676             L = LW * 12;
677             R = RW * 12;
678         }
679         else if (!(strcmp(pannel, "SCI")))// == NULL)
680         {
681             B = 319;
682             T = 106;
683             L = 71;
684             R = 307;
685             
686             memset(ucCommand, 0xFF, lCommandSize);
687             debug("DP2NC SCI... ");
688         }
689         else if (!(strcmp(pannel, "SCA")))// == NULL)
690         {
691             B = 354;
692             T = 177;
693             L = 71;
694             R = 283;
695             
696             memset(ucCommand, 0xFF, lCommandSize);
697             debug("DP2NC SCA... ");
698         } 
699         else if (!(strcmp(pannel, "MS")))// == NULL)
700         {
701             B = 216;
702             T = 60;
703             L = 0;
704             R = 1016;
705             
706             memset(ucCommand, 0xFF, lCommandSize);
707             debug("DP2NC MS... ");
708         }
709         //White part
710         for (j = L; j <= R; j++)
711         {
712             pB = (j * 648) + (648 - B);
713             ModuloB = pB % 8;
714             pB /= 8;
715             if (ModuloB)
716                 pB++;
717             ucCommand[pB + 7] = MaskB[ModuloB];
718             pB++;
719             pT = (j * 648) + (648 - T);
720             ModuloT = pT % 8;
721             pT /= 8;
722             if (ModuloT)
723                 pT++;
724             ucCommand[pT + 7] = MaskT[ModuloT];
725             pT--;
726             for (i = (pB); i <= (pT); i++)
727             {
728                 ucCommand[i + 7] = 0x00;
729             }
730         }//end of white part
731         
732         strcpy((char*) ucCommand, "\033Db;o;2;");
733         
734         strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
735         lCommandSize += 9;
736         numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
737         if (numwritten != lCommandSize)
738         {
739             fatal("DP2NC Error: Fails fwrite %d... ", numwritten);
740             free(ucCommand);
741             return (-1);
742         }
743         
744         
745     }
746     
747     free(ucCommand);
748     return (0);
749 }
750
751 //=====================================================================
752 //   Convert color bitmap to monochrome bitmap                         
753 //=====================================================================
754
755 //////////////////////////////////////////////////////////////////////////////////////
756 ///////                                                         RVB TO GRAY  
757 /////// Input parameters
758 ///////                         lpMemIn : RVB data (3 byte by dot)
759 ///////                         lNbrByte: number of byte to deal
760 /////// Output parameter
761 ///////                         lpMemOut: pointer on the gray data, result of the conversion
762
763
764 //////////////////////////////////////////////////////////////////////////////////////
765
766 void RVBtoGray(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte)
767 {
768     
769     long lIndex = 0;
770     unsigned char *lpbData, *lpbDataOut;
771     unsigned char red, green, blue;
772     long d;
773     
774     //lpbData ppV mem bmp brute
775     lpbData = &lpMemIn[0];
776     lpbDataOut = &lpMemOut[0];
777     // pour toutes les donnees
778     while (lIndex < lNbrByte)
779     {
780         blue  = lpbData[(lIndex * 3)];
781         green = lpbData[(lIndex * 3) + 1];
782         red   = lpbData[(lIndex * 3) + 2];
783         d     = RgbToGray(red, green, blue);
784         lpbDataOut[lIndex] = (unsigned char) d;
785         lIndex++;
786     }
787     
788 }
789
790
791 //////////////////////////////////////////////////////////////////////////////////////
792 ///////                                                         GRAY  THRESHOLD
793 /////// Input parameters
794 ///////                         lpMemIn : gray data (1 byte by dot)
795 ///////                         lNbrByte: number of byte to deal
796
797
798
799 /////// Output parameter
800 ///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion
801
802 //////////////////////////////////////////////////////////////////////////////////////
803
804 void GrayToThreshold(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte)
805 {
806     long lIndex = 0;
807     unsigned char *lpbData, *lpbDataOut;
808     int gray, k;
809     
810     //lpbData ppV mem bmp brute
811     lpbData = &lpMemIn[0];
812     lpbDataOut = &lpMemOut[0];
813     // pour toutes les donnees
814     while (lIndex < lNbrByte)
815         
816         
817     {
818         gray = lpbData[lIndex];
819         k = 0xff - gray;
820         if (k > 128)
821             gray = 0xFF;
822         else
823             gray = 0x00;
824         lpbDataOut[lIndex] = (unsigned char) gray;
825         lIndex++;
826     }
827 }
828
829
830
831
832 //////////////////////////////////////////////////////////////////////////////////////
833 ///////                                                         GRAY  DITHER
834 /////// Input parameters
835 ///////                         lpMemIn : gray data (1 byte by dot)
836 ///////                         Height  : height of the BMP image (648)
837 ///////                         Width   : width of the BMP image (1016)
838
839 /////// Output parameter
840 ///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion
841 //////////////////////////////////////////////////////////////////////////////////////
842
843 void GrayToDither(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height)
844 {
845     long index_line = 0, index_dot = 0;
846     unsigned char *lpbData, *lpbDataOut;
847     int gray, k;
848     
849     
850     //lpbData ppV mem bmp brute
851     lpbData = &lpMemIn[0];
852     
853     lpbDataOut = &lpMemOut[0];
854     // pour toutes les donnees
855     if (Height < Width)
856     {
857         index_line = Width;
858         Width = Height;
859         Height = index_line;
860     }
861     
862     
863     for (index_line = 0; index_line < Height; index_line++)
864     {
865         for (index_dot = 0; index_dot < Width; index_dot++)
866         {
867             gray = lpbData[index_dot + (index_line * Width)];
868             k = 0xff - gray;
869             if (k > DitherPattern1[index_line % 8][index_dot % 8])
870                 gray = 0xFF;
871             else
872                 gray = 0x00;
873             lpbDataOut[index_dot + (index_line * Width)] = (unsigned char) gray;
874         }
875     }
876     
877 }
878
879 //////////////////////////////////////////////////////////////////////////////////////
880 ///////                                                         GRAY  FLOYD
881 /////// Input parameters
882 ///////                         lpMemIn : gray data (1 byte by dot)
883 ///////                         Height  : height of the BMP image (648)
884 ///////                         Width   : width of the BMP image (1016)
885 /////// Output parameter
886 ///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion
887 //////////////////////////////////////////////////////////////////////////////////////
888
889 void GrayToFloyd(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height)
890 {
891     long index_line = 0, index_dot = 0;
892     unsigned char *lpbData, *lpbDataOut;
893     int gray, k, l;
894     int Error[1032];
895     int NextError[1032];
896     int Cerror;
897     int CE3, CE5, CE7, CE1;
898     
899     if (Height < Width)
900     {
901         index_line = Width;
902         
903         Width = Height;
904         Height = index_line;
905         
906     }
907     
908     for (index_line = 0; index_line < 1032; index_line++)
909     {
910         Error[index_line] = NextError[index_line] = 0;
911     }
912     //lpbData ppV mem bmp brute
913     
914     
915     lpbData = &lpMemIn[0];
916     lpbDataOut = &lpMemOut[0];
917     // pour toutes les donnees
918     for (index_line = 0; index_line < Height; index_line++)
919         
920     {
921         //for(index_dot = Width ; index_dot > 0; index_dot--)
922         for (index_dot = 0; index_dot < Width; index_dot++)
923         {
924             gray = lpbData[index_dot + (index_line * Width)];
925             
926             k = (gray + (int) Error[index_dot]);
927             if (k > -128 && k < 128)
928                 l = 0x00;
929             
930             else
931                 l = 0xFF;
932             
933             
934             lpbDataOut[index_dot + (index_line * Width)] = 0xFF - (unsigned char) l;
935             
936             
937             
938             Cerror = k - l;
939             CE3 = (Cerror * 3 / 16);
940             CE5 = (Cerror * 5 / 16);
941             CE7 = (Cerror * 7 / 16);
942             CE1 = Cerror - CE3 - CE5 - CE7;
943             
944             NextError[index_dot] += CE5;
945             if (index_dot < 1031)
946             {
947                 Error[index_dot + 1] += CE7;
948                 NextError[index_dot + 1] += CE1;
949             }
950             if (index_dot > 0)
951                 NextError[index_dot - 1] += CE3;
952         }
953         for (index_dot = 0; index_dot < 1032; index_dot++)
954         {
955             Error[index_dot] = NextError[index_dot];
956             NextError[index_dot] = 0;
957         }
958     }
959 }
960
961
962 ////////////////////////////////////////////////////////////////////////////////////
963
964 ///////                                         RVB  to K
965 /////// Input parameters
966 ///////                         lpbRVB  : rvb color data from a 24 bits 1 panel BMP file
967 ///////                         RVBSize : number of byte for the color data [(1016*648)]*3
968
969 ///////                         Height  : height of the BMP image (648)
970 ///////                         Width   : width of the BMP image (1016)
971 ///////                         Type    : type of monochrome dealing (threshold, floyd or dither)
972 ///////                         face    : RECTO / VERSO 
973
974 /////// Output parameter
975
976 ///////                         lpBlack : pointer on the monochrome data, result of the color conversion
977 //////////////////////////////////////////////////////////////////////////////////////
978 void ConvertRVBtoK(unsigned char *lpbRVB, long RVBSize, long Height, long Width, unsigned char *lpBlack, int face)
979 {
980     if ( toupper(TreatementK[face]) =='G' ) // Grey Mode
981     {
982         GrayToFloyd(lpbRVB, lpBlack, Width, Height);
983     }
984     else // Line Mode
985     {
986         GrayToThreshold(lpbRVB, lpBlack, (RVBSize / 5));
987     }
988 }
989
990 //========================================================//
991 // OPTION : Not real black printed
992 // Insert sublimable black from K panel to Y,M & C panels
993 //========================================================//
994 void KinYMC(long Height, long Width)
995 {
996     long dwSize, i;
997     unsigned char *lpYellow, *lpMagenta, *lpCyan, *lpBlack;
998     
999     
1000     dwSize = Width * Height;
1001     lpYellow = &lpMem[0];
1002     lpMagenta = &lpMem[dwSize];
1003     lpCyan = &lpMem[2 * dwSize];
1004     lpBlack = &lpMem[3 * dwSize];
1005     for (i = 0; i < dwSize; i++)
1006     {
1007         if (lpBlack[i])
1008         {
1009             lpYellow[i] |= lpBlack[i];
1010             lpMagenta[i] |= lpBlack[i];
1011             lpCyan[i] |= lpBlack[i];
1012         }
1013     }
1014 }
1015
1016 //========================================================//
1017 // OPTION : Get the first YMC printed dots
1018 //========================================================//
1019 void GetFirstYMCDot(long Height, long Width , int *x, int *y)
1020 {
1021     long dwSize, i=0;
1022     unsigned char *lpYellow, *lpMagenta, *lpCyan, *lpBlack;
1023     int fi = 0;
1024     
1025     dwSize = Width * Height;
1026     lpYellow = &lpMem[0];
1027     lpMagenta = &lpMem[dwSize];
1028     lpCyan = &lpMem[2 * dwSize];
1029     //for (i = 0; i < dwSize; i++)
1030     while(i < dwSize && !fi)
1031     {
1032         if(lpYellow[i] || lpMagenta[i] || lpCyan[i] )
1033             fi = 1;
1034         i++;
1035     }
1036     *x = i % Width;
1037     *y = i / Width;
1038 }
1039
1040 // End of "evolis.c,v 1.0  ".