evolisprinter-3.0
[evolisprinter.git] / src / evolis.c
index cd71e71..671e9e8 100755 (executable)
-\r
-/*\r
- *\r
- * Contents:\r
- *\r
- */\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include "evoliserror.h"\r
-\r
-// Macros...\r
-#define        RgbToGray(r, g, b) (((long) (r)*74L + (long)(g)*155L +(long)(b)*27L) >> 8)\r
-// Globals...\r
-// Globals...\r
-int           STATUS=0; //0 means succes, 1 means failure\r
-unsigned char *lpMem;\r
-unsigned char *lpRecto,*lpVerso;\r
-int           Model;                   /* Model number */\r
-long          dwSizeNeeded;            // taille mmoire du lpMem en octets\r
-\r
-char\r
-  ColorSmooth[15],\r
-  OverlayPannel[10],\r
-  OverlayBackPannel[10],\r
-  TreatementK[10],\r
-  TestCard[10],\r
-  EjectCard[10],\r
-  SelfClean[10],\r
-  AdjPrinter[10],\r
-  BlackIn[10],\r
-  Soften[10];\r
-\r
-int Brightness, Contrast, SensibilityK, QualityK, SensibilityO;\r
-int TB,LB,BB,RB,TW,LW,BW,RW;\r
-int DetectAuto,StartDist;\r
-int colorspace,levelB;\r
-\r
-\r
-\r
-// *3.5\r
-int DitherPattern1[8][8] = {\r
-       {0, 112, 28, 140, 7, 119, 35, 147},\r
-       {168, 56, 196, 84, 175, 63, 203, 78},\r
-       {42, 154, 14, 126, 49, 161, 21, 114},\r
-       {210, 98, 182, 70, 217, 105, 189, 77},\r
-       {11, 123, 39, 151, 4, 116, 32, 143},\r
-       {179, 67, 207, 95, 172, 59, 200, 88},\r
-       {53, 165, 24, 137, 46, 158, 18, 111},\r
-       {221, 109, 193, 81, 214, 102, 159, 74}\r
-};\r
-\r
-// Prototypes...\r
-\r
-// Compress and format data for the printer\r
-long ReduceBlack(unsigned char *lpMemIn, unsigned char *lpMemOut,int nbrline);\r
-long ReduceColor(unsigned char *lpMemIn, unsigned char *lpMemOut, int uiBitComp);//,int nbrline);\r
-//int CutPage(long *stop);\r
-\r
-// Download functions\r
-int DBNC(int col,int bl,int ov);//,int line);\r
-int DB32NC(long lPos, char color);     //y,m,c pannels 5 bits per color\r
-int DB64NC(long lPos, char color);     //y,m,c pannels 6 bits per color\r
-int DB128NC(long lPos, char color);    //y,m,c pannels 7 bits per color\r
-/*int DB32NCS(long lPos, char color,int line); //y,m,c pannels 5 bits per color\r
-int DB64NCS(long lPos, char color,int line);   //y,m,c pannels 6 bits per color\r
-int DB128NCS(long lPos, char color,int line);  //y,m,c pannels 7 bits per color*/\r
-int DB2NC(long lPos, char pannel[10]); // k,o panel 2 levels\r
-int DB2MNC(long lPos);         // k panel 2 levels\r
-\r
-// Convert RVB to k functions\r
-void RVBtoGray(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte);\r
-void KinYMC(long Height, long Width);\r
-void GrayToFloyd(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height);\r
-void GrayToDither(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height);\r
-void GrayToThreshold(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte);\r
-void ConvertRVBtoK(unsigned char *lpbRVB, long RVBSize, long Height, long Width, unsigned char *lpBlack);\r
-void GetFirstYMCDot(long Height, long Width , int *x, int *y);\r
-\r
-\r
-//===============================================================================//\r
-// Reduce color data from 8 bits to uiBitComp.\r
-\r
-// Entree : 1 color panel (1016 * 648 octets)\r
-// Sortie : 1 color panel with usable data ((1016*648)*uiBitComp)/8\r
-//===============================================================================//\r
-\r
-\r
-long ReduceColor(unsigned char *lpMemIn, unsigned char *lpMemOut, int uiBitComp)//,int nbrline)\r
-{\r
-       long lIndex = 0, lIndex1 = 0;\r
-       unsigned char *lpbBuf, *lpbData, *lpbDataOut;\r
-       long lComp = 0;\r
-       long lNbrByte = (1016*648);\r
-\r
-       //Reservation m�oire pour traitement d'une ligne\r
-\r
-       lpbBuf = malloc(8);\r
-\r
-       if (!lpbBuf)\r
-       {\r
-\r
-               //fprintf(stderr, "**************** EVOLIS ReduceColor manque de memoire pour lpBuf: taille demande 8 octets... \n");\r
-               fatal("ReduceColor Fails memory lpBuf: requested size 8 bytes...");\r
-               \r
-               /// sortir de l'impression....\r
-               return (0);\r
-       }\r
-\r
-       //lpbData ppV mem bmp brute\r
-       lpbData = &lpMemIn[0];\r
-\r
-       lpbDataOut = &lpMemOut[0];\r
-       // pour toutes les donnees\r
-       while (lIndex < lNbrByte)\r
-       {\r
-               //Traite byte par 8\r
-               for (lIndex1 = 0; lIndex1 < 8; lIndex1++)\r
-               {\r
-                       if (lIndex < lNbrByte)\r
-                       {\r
-                               //decalage donnees utiles\r
-                               lpbBuf[lIndex1] = *(lpbData++) >> (8 - uiBitComp);\r
-                               lIndex++;\r
-                       }\r
-                       else\r
-                       {\r
-\r
-                               lpbBuf[lIndex1] = 0x00;\r
-                       }\r
-               }\r
-\r
-               switch (uiBitComp)\r
-               {\r
-                       case 6:\r
-                               *(lpbDataOut++) = (lpbBuf[0] << 2) | (lpbBuf[1] >> 4);\r
-                               *(lpbDataOut++) = (lpbBuf[1] << 4) | (lpbBuf[2] >> 2);\r
-                               *(lpbDataOut++) = (lpbBuf[2] << 6) | (lpbBuf[3]);\r
-                               *(lpbDataOut++) = (lpbBuf[4] << 2) | (lpbBuf[5] >> 4);\r
-                               *(lpbDataOut++) = (lpbBuf[5] << 4) | (lpbBuf[6] >> 2);\r
-                               *(lpbDataOut++) = (lpbBuf[6] << 6) | (lpbBuf[7]);\r
-                               break;\r
-                       case 7:\r
-                               *(lpbDataOut++) = (lpbBuf[0] << 1) | (lpbBuf[1] >> 6);\r
-                               *(lpbDataOut++) = (lpbBuf[1] << 2) | (lpbBuf[2] >> 5);\r
-                               *(lpbDataOut++) = (lpbBuf[2] << 3) | (lpbBuf[3] >> 4);\r
-                               *(lpbDataOut++) = (lpbBuf[3] << 4) | (lpbBuf[4] >> 3);\r
-\r
-                               *(lpbDataOut++) = (lpbBuf[4] << 5) | (lpbBuf[5] >> 2);\r
-                               *(lpbDataOut++) = (lpbBuf[5] << 6) | (lpbBuf[6] >> 1);\r
-                               *(lpbDataOut++) = (lpbBuf[6] << 7) | (lpbBuf[7]);\r
-                               break;\r
-\r
-                       default:        //5 bits\r
-                               *(lpbDataOut++) = (lpbBuf[0] << 3) | (lpbBuf[1] >> 2);\r
-                               *(lpbDataOut++) = (lpbBuf[1] << 6) | (lpbBuf[2] << 1) | (lpbBuf[3] >> 4);\r
-                               *(lpbDataOut++) = (lpbBuf[3] << 4) | (lpbBuf[4] >> 1);\r
-\r
-                               *(lpbDataOut++) = (lpbBuf[4] << 7) | (lpbBuf[5] << 2) | (lpbBuf[6] >> 3);\r
-                               *(lpbDataOut++) = (lpbBuf[6] << 5) | (lpbBuf[7]);\r
-                               //break;\r
-               }\r
-       }\r
-       free(lpbBuf);\r
-       lComp = (((lNbrByte * 10) / 8) * uiBitComp);\r
-       lComp /= 10;\r
-       if (lNbrByte % 8)\r
-               lComp++;\r
-\r
-\r
-       return (lComp);\r
-}\r
-\r
-//===================================================//\r
-// fonction pour le panneau noir & overlay uniquement\r
-// entree : 1 octect = 1 points\r
-// sortie : 1 octect = 8 points \r
-//===================================================//\r
-long ReduceBlack(unsigned char *lpMemIn, unsigned char *lpMemOut,int nbrline)\r
-{\r
-       long lIndex = 0, lIndex1 = 0;\r
-       unsigned char *lpbData, *lpbDataOut;\r
-       long lComp = 0;\r
-       unsigned char bBuf = 0x00;\r
-\r
-       unsigned char Mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };\r
-       long lNbrByte = nbrline * 648;\r
-\r
-\r
-       //lpbData ppV mem bmp brute\r
-       lpbData = &lpMemIn[0];\r
-       lpbDataOut = &lpMemOut[0];\r
-       // pour toutes les donnees\r
-\r
-       while (lIndex < lNbrByte)\r
-       {\r
-\r
-\r
-               //Traite byte par 8\r
-               for (lIndex1 = 0; lIndex1 < 8; lIndex1++)\r
-\r
-               {\r
-                       if (lIndex < lNbrByte)\r
-                       {\r
-                               if (*(lpbData++) == 0xFF)\r
-                               {\r
-                                       bBuf |= Mask[lIndex1];\r
-                               }\r
-                               lIndex++;\r
-                       }\r
-               }\r
-               *(lpbDataOut++) = bBuf;\r
-               bBuf = 0x00;\r
-               lComp++;\r
-       }\r
-       return (lComp);\r
-}\r
-\r
- //=====================================================================\r
- //   Convert bitmap RVB data to evolis data format                             \r
- //=====================================================================\r
-\r
\r
-int DBNC(int col,int bl,int ov)//,int line)\r
-{\r
-    //output("\033Ss\015");    // debut sequence + CR\r
-    \r
-    if(col == 1) {\r
-       DB32NC(0 * (dwSizeNeeded / 5), 'y');\r
-       DB32NC(1 * (dwSizeNeeded / 5), 'm');\r
-       DB32NC(2 * (dwSizeNeeded / 5), 'c');\r
-    }\r
-    else if (col == 2) {\r
-       DB64NC(0 * (dwSizeNeeded / 5), 'y');\r
-       DB64NC(1 * (dwSizeNeeded / 5), 'm');\r
-       DB64NC(2 * (dwSizeNeeded / 5), 'c');\r
-    }\r
-    else if (col == 3) {\r
-       DB128NC(0 * (dwSizeNeeded / 5), 'y');\r
-       DB128NC(1 * (dwSizeNeeded / 5), 'm');\r
-       DB128NC(2 * (dwSizeNeeded / 5), 'c');\r
-       }\r
-\r
-    if(bl){\r
-       DB2NC(3 * (dwSizeNeeded / 5), "ABP");\r
-    }\r
-    \r
-       if(ov){\r
-               if(ov == 1){//overlay at front\r
-                       DB2NC(0, OverlayPannel);\r
-               }\r
-               else {\r
-                       DB2NC(0, OverlayBackPannel);\r
-               }\r
-       }\r
-       \r
-\r
-    //output("\033Se\015.....................................");       // fin sequence + CR\r
-\r
-}\r
-\r
-//===============================================================//\r
-// Telechargement d'un panneau non compresse 5 bits\r
-// Input : ucData / raw data\r
-//         color /y,m or c      \r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture  \r
-//===============================================================//\r
-int DB32NC(long lPos, char color)\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-       long lCommandSize = ((1016 * 648) * 5) / 8;\r
-       long lComp = 0;\r
-       int  numwritten = 0;\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-       ucCommand = malloc(lCommandSize + 12);\r
-       if (!ucCommand) {\r
-               fatal("DP32NC Error: Fails malloc... ");\r
-               return (-1);\r
-       }\r
-               \r
-       ucCol = &lpMem[lPos];\r
-       strcpy(ucCommand, "\033Db;y;32;");\r
-       strncpy(&ucCommand[4], &color, 1);\r
-       \r
-       lComp = ReduceColor(ucCol, &ucCommand[9], 5);//,1016);\r
-       lCommandSize += 10;\r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-       if (numwritten != lCommandSize) {\r
-               fatal("DP32NC Error: Fails printf %d... \n", numwritten);\r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-       /*lComp += 10;\r
-       while(lComp > 4096) {\r
-               numwritten += fwrite(&ucCommand[numwritten], sizeof(unsigned char), 4096, stdout);\r
-               lComp -= 4096;          \r
-       }\r
-       debug("NuwWritten %d", numwritten);\r
-       if(lComp > 0) {\r
-               numwritten += fwrite(&ucCommand[numwritten], sizeof(unsigned char), lComp, stdout);\r
-       }\r
-       debug("lComp %d numwritten %d lCommandSize %d", lComp,numwritten,lCommandSize);\r
-       if (numwritten != lCommandSize) {\r
-               fatal("DP32NC Error: Fails printf %d... \n", numwritten);\r
-               free(ucCommand);\r
-               return (-1);\r
-       }*/\r
-       \r
-       \r
-       free(ucCommand);\r
-       return (0);\r
-}\r
-\r
- //===============================================================//\r
-// Telechargement d'un panneau non compresse 6 bits\r
-// Input : ucData / raw data\r
-//         color /y,m or c      \r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture  \r
-//===============================================================//\r
-\r
-int DB64NC(long lPos, char color)\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-\r
-\r
-       long lCommandSize = ((1016 * 648) * 6) / 8;\r
-       long lComp;\r
-       int numwritten = 0;\r
-\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-       ucCommand = malloc(lCommandSize + 10);\r
-       debug("DP64NC Size for one panel: %d... \n", (lCommandSize + 10));\r
-       \r
-       if (!ucCommand)\r
-       {\r
-\r
-               fatal("DP64NC Error: Fails malloc...");\r
-               \r
-               free(ucCommand);\r
-               return (-1);\r
-\r
-       }\r
-       ucCol = &lpMem[lPos];\r
-       strcpy(ucCommand, "\033Db;y;64;");\r
-       strncpy(&ucCommand[4], &color, 1);\r
-\r
-\r
-       lComp = ReduceColor(ucCol, &ucCommand[9], 6);//,1016);\r
-       //fprintf(stderr, "**************** EVOLIS DP64NC taille apres compression %d... \n",lComp);\r
-       strncpy(&ucCommand[lCommandSize + 9], "\015", 1);\r
-       lCommandSize += 10;\r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-       if (numwritten != lCommandSize)\r
-       {\r
-               fatal("DP64NC Error: Fails fwrite %d... \n", numwritten);\r
-               \r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-\r
-       free(ucCommand);\r
-\r
-       return (0);\r
-}\r
-\r
-\r
- //===============================================================//\r
-// Telechargement d'un panneau non compresse 7 bits\r
-// Input : ucData / raw data\r
-//         color /y,m or c      \r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture  \r
-//===============================================================//\r
-int DB128NC(long lPos, char color)\r
-\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-       long lCommandSize = ((1016 * 648) * 7) / 8;\r
-       long lComp;\r
-       int numwritten = 0;\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-       ucCommand = malloc(lCommandSize + 11);\r
-       debug("DP128NC Size for one panel: %d... ", (lCommandSize + 11));\r
-       if (!ucCommand)\r
-       {\r
-               fatal("DP128NC Error: Fails malloc... ");\r
-               \r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-       ucCol = &lpMem[lPos];\r
-       strcpy(ucCommand, "\033Db;y;128;");\r
-       strncpy(&ucCommand[4], &color, 1);\r
-       lComp = ReduceColor(ucCol, &ucCommand[10], 7);//,1016);\r
-       strncpy(&ucCommand[lCommandSize + 10], "\015", 1);\r
-       lCommandSize += 11;\r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-       if (numwritten != lCommandSize)\r
-       {\r
-               fatal("DP128NC Error: Fails fwrite %d... \n", numwritten);\r
-               \r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-       free(ucCommand);\r
-       return (0);\r
-}\r
-//===============================================================//\r
-// Telechargement d'un panneau non compresse 5 bits YMCKOS\r
-// Input : ucData / raw data\r
-//         color /y,m or c\r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture\r
-//===============================================================//\r
-/*int DB32NCS(long lPos, char color,int line)\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-       long lCommandSize = ((1016 * 648) * 5) / 8;\r
-       long lComp = 0;\r
-       int numwritten = 0;\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-       ucCommand = malloc(lCommandSize + 20);\r
-       //fprintf(stderr, "**************** EVOLIS DP32NC Size for one panel: %d... \n", (lCommandSize+10));\r
-       if (!ucCommand)\r
-       {\r
-               //fprintf(stderr, "**************** EVOLIS DP32NC Error: Fails malloc... \n");\r
-               fatal("DP32NC Error: Fails malloc... ");\r
-\r
-               return (-1);\r
-       }\r
-       ucCol = &lpMem[lPos];\r
-       //strcpy(ucCommand, "\033Dbc;y;32;");\r
-  //nbroct = 540 * 648;\r
-  lCommandSize = ((520 * 648) * 5) / 8;\r
-  sprintf(ucCommand, "\033Dbc;y;32;%.4d;%.6d;",line,lCommandSize);\r
-  strncpy(&ucCommand[5], &color, 1);\r
-\r
-  info("%s",ucCommand);\r
-  lComp = ReduceColor(ucCol, &ucCommand[22], 5, 520);\r
-       strncpy(&ucCommand[lCommandSize + 22], "\015", 1);\r
-       lCommandSize += 23;\r
-  \r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-  //info("%d  %s", numwritten,ucCommand);\r
-  if (numwritten != lCommandSize)\r
-       {\r
-               //fprintf(stderr, "**************** EVOLIS DP32NC Error: Fails printf %d... \n",numwritten);\r
-    fatal("DP32NC Error: Fails printf %d... \n", numwritten);\r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-       free(ucCommand);\r
-       return (0);\r
-}\r
-\r
- //===============================================================//\r
-// Telechargement d'un panneau non compresse 6 bits YMCKOS\r
-// Input : ucData / raw data\r
-//         color /y,m or c\r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture\r
-//===============================================================//\r
-\r
-int DB64NCS(long lPos, char color,int line)\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-\r
-       long lCommandSize = ((1016 * 648) * 6) / 8;\r
-       long lComp;\r
-       int numwritten = 0;\r
-\r
-\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-       ucCommand = malloc(lCommandSize + 10);\r
-       debug("DP64NC Size for one panel: %d... \n", (lCommandSize + 10));\r
-\r
-       if (!ucCommand)\r
-       {\r
-\r
-               fatal("DP64NC Error: Fails malloc...");\r
-\r
-               free(ucCommand);\r
-               return (-1);\r
-\r
-       }\r
-       ucCol = &lpMem[lPos];\r
-       strcpy(ucCommand, "\033Db;y;64;");\r
-       strncpy(&ucCommand[4], &color, 1);\r
-\r
-\r
-       lComp = ReduceColor(ucCol, &ucCommand[9], 6, 540);\r
-       //fprintf(stderr, "**************** EVOLIS DP64NC taille apres compression %d... \n",lComp);\r
-       strncpy(&ucCommand[lCommandSize + 9], "\015", 1);\r
-       lCommandSize += 10;\r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-       if (numwritten != lCommandSize)\r
-       {\r
-               fatal("DP64NC Error: Fails fwrite %d... \n", numwritten);\r
-\r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-\r
-       free(ucCommand);\r
-\r
-       return (0);\r
-}\r
-\r
-\r
- //===============================================================//\r
-// Telechargement d'un panneau non compresse 7 bits YMCKOS\r
-// Input : ucData / raw data\r
-//         color /y,m or c\r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture\r
-//===============================================================//\r
-int DB128NCS(long lPos, char color,int line)\r
-\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-       long lCommandSize = ((1016 * 648) * 7) / 8;\r
-       long lComp;\r
-       int numwritten = 0;\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-       ucCommand = malloc(lCommandSize + 11);\r
-       //fprintf(stderr, "**************** EVOLIS DP128NC Size for one panel: %d... \n", (lCommandSize+11));\r
-       debug("DP128NC Size for one panel: %d... ", (lCommandSize + 11));\r
-\r
-\r
-       if (!ucCommand)\r
-       {\r
-               fatal("DP128NC Error: Fails malloc... ");\r
-\r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-       ucCol = &lpMem[lPos];\r
-       strcpy(ucCommand, "\033Db;y;128;");\r
-       strncpy(&ucCommand[4], &color, 1);\r
-       lComp = ReduceColor(ucCol, &ucCommand[10], 7, 540);\r
-       //fprintf(stderr, "**************** EVOLIS DP128NC taille apres compression %d... \n",lComp);\r
-       strncpy(&ucCommand[lCommandSize + 10], "\015", 1);\r
-       lCommandSize += 11;\r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-       if (numwritten != lCommandSize)\r
-       {\r
-               fatal("DP128NC Error: Fails fwrite %d... \n", numwritten);\r
-\r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-       free(ucCommand);\r
-       return (0);\r
-} */\r
-//===============================================================//\r
-// Telechargement d'un panneau non compresse overlay noir\r
-// Input : ucData / raw datal978\r
-//         color /      k: noir\r
-//                      f: full varnish \r
-// Return:      0 si OK\r
-//              -1 si erreur ecriture  \r
-//===============================================================//\r
-\r
-int DB2NC(long lPos, char pannel[10])\r
-{\r
-       unsigned char *ucCommand, *ucCol;\r
-       long lComp;\r
-\r
-       long lCommandSize = ((1016 * 648)) / 8;\r
-       int numwritten = 0;\r
-       int B, T, L, R, i = 0, j = 0;\r
-       int pB, pT, ModuloB, ModuloT;\r
-\r
-       //unsigned char        MaskB[8] = { 0x00,0x80,0xC0,0xE0,0xF0,0x7F,0x3F,0x1F};\r
-       //unsigned char        MaskT[8] = { 0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F};\r
-       unsigned char MaskB[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };\r
-       unsigned char MaskT[8] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };\r
-\r
-\r
-       //Reservation memoire pour une commande de telechargement\r
-\r
-       ucCommand = malloc(lCommandSize + 9);\r
-\r
-       //debug("DP2NC Size for one panel: %d... ", (lCommandSize + 9));\r
-       \r
-\r
-       if (!ucCommand)\r
-       {\r
-               fatal("DP2NC Error: Fails malloc... ");\r
-               free(ucCommand);\r
-               return (-1);\r
-       }\r
-\r
-       if (!(strcmp(pannel, "ABP")))// == NULL)\r
-       {                       // panneau noir\r
-               ucCol = &lpMem[lPos];\r
-               strcpy(ucCommand, "\033Db;k;2;");\r
-               lComp = ReduceBlack(ucCol, &ucCommand[8],1016);\r
-               strncpy(&ucCommand[lCommandSize + 8], "\015", 1);\r
-               lCommandSize += 9;\r
-               numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-               if (numwritten != lCommandSize)\r
-       {\r
-                       fatal("DP2NC Error: Fails fprintf %d... ", numwritten);\r
-               \r
-                       free(ucCommand);\r
-                       return (-1);\r
-               }\r
-       }\r
-       else if (!(strcmp(pannel, "NO")))// == NULL)\r
-       {               //No Varnish\r
-                       memset(ucCommand, 0x00, lCommandSize);\r
-                       strcpy(ucCommand, "\033Db;o;2;");\r
-                       strncpy(&ucCommand[lCommandSize + 8], "\015", 1);\r
-                       lCommandSize += 9;\r
-                       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-                       if (numwritten != lCommandSize)\r
-                       {\r
-                               fatal("DP2NC Error: Fails fwrite %d... ", numwritten);\r
-                               free(ucCommand);\r
-                               return (-1);\r
-                       }\r
-       }\r
-       else if (!(strcmp(pannel, "FO")))// == NULL)\r
-       {       //Full Varnish\r
-                         memset(ucCommand, 0xFF, lCommandSize);\r
-                               strcpy(ucCommand, "\033Db;o;2;");\r
-                               strncpy(&ucCommand[lCommandSize + 8], "\015", 1);\r
-                               lCommandSize += 9;\r
-                               numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-                               if (numwritten != lCommandSize)\r
-                               {\r
-                                       fatal("DP2NC Error: Fails fwrite %d... ", numwritten);\r
-                                       free(ucCommand);\r
-                                       return (-1);\r
-                               }\r
-       }\r
-       else\r
-  {\r
-\r
-  if (!(strcmp(pannel, "OA")))// == NULL)\r
-  {\r
-    //Cover Overlay\r
-    B = BB * 12;\r
-         T = TB * 12;\r
-               L = LB * 12;\r
-               R = RB * 12;\r
-               debug("DP2NC OA... ");\r
-    memset(ucCommand, 0x00, lCommandSize);\r
-    \r
-\r
-    for (j = L; j <= R; j++)\r
-\r
-               {\r
-                 pB = (j * 648) + (648 - B);\r
-                       ModuloB = pB % 8;\r
-                       pB /= 8;\r
-                       if (ModuloB)\r
-                         pB++;\r
-                       ucCommand[pB + 7] = MaskB[ModuloB];\r
-                       pB++;\r
-                       pT = (j * 648) + (648 - T);\r
-                       ModuloT = pT % 8;\r
-                       pT /= 8;\r
-                       if (ModuloT)\r
-                         pT++;\r
-                       ucCommand[pT + 7] = MaskT[ModuloT];\r
-                       pT--;\r
-                       for (i = (pB); i <= (pT); i++)\r
-                       {\r
-                         ucCommand[i + 7] = 0xFF;\r
-                       }\r
-               }\r
-    B = BW * 12;\r
-               T = TW * 12;\r
-               L = LW * 12;\r
-               R = RW * 12;\r
-  }\r
-  else if (!(strcmp(pannel, "SCI")))// == NULL)\r
-       {\r
-         B = 319;\r
-               T = 106;\r
-               L = 71;\r
-               R = 307;\r
-    \r
-    memset(ucCommand, 0xFF, lCommandSize);\r
-               debug("DP2NC SCI... ");\r
-       }\r
-  else if (!(strcmp(pannel, "SCA")))// == NULL)\r
-       {\r
-                 B = 354;\r
-                 T = 177;\r
-                 L = 71;\r
-                 R = 283;\r
-     \r
-      memset(ucCommand, 0xFF, lCommandSize);\r
-                 debug("DP2NC SCA... ");\r
-       } \r
-       else if (!(strcmp(pannel, "MS")))// == NULL)\r
-  {\r
-                 B = 216;\r
-                 T = 60;\r
-                 L = 0;\r
-                 R = 1016;\r
-      \r
-      memset(ucCommand, 0xFF, lCommandSize);\r
-                 debug("DP2NC MS... ");\r
-  }\r
-    //White part\r
-    for (j = L; j <= R; j++)\r
-         {\r
-           pB = (j * 648) + (648 - B);\r
-           ModuloB = pB % 8;\r
-           pB /= 8;\r
-           if (ModuloB)\r
-           pB++;\r
-           ucCommand[pB + 7] = MaskB[ModuloB];\r
-           pB++;\r
-           pT = (j * 648) + (648 - T);\r
-           ModuloT = pT % 8;\r
-           pT /= 8;\r
-           if (ModuloT)\r
-           pT++;\r
-           ucCommand[pT + 7] = MaskT[ModuloT];\r
-           pT--;\r
-           for (i = (pB); i <= (pT); i++)\r
-           {\r
-                   ucCommand[i + 7] = 0x00;\r
-           }\r
-    }//end of white part\r
-\r
-  strcpy(ucCommand, "\033Db;o;2;");\r
-\r
-       strncpy(&ucCommand[lCommandSize + 8], "\015", 1);\r
-       lCommandSize += 9;\r
-       numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);\r
-       if (numwritten != lCommandSize)\r
-       {\r
-                       fatal("DP2NC Error: Fails fwrite %d... ", numwritten);\r
-                       free(ucCommand);\r
-                       return (-1);\r
-       }\r
\r
-\r
-}\r
-\r
-  free(ucCommand);\r
-       return (0);\r
-}\r
-\r
-//=====================================================================\r
-//   Convert color bitmap to monochrome bitmap                         \r
-//=====================================================================\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-///////                                                         RVB TO GRAY  \r
-/////// Input parameters\r
-///////                         lpMemIn : RVB data (3 byte by dot)\r
-///////                         lNbrByte: number of byte to deal\r
-/////// Output parameter\r
-///////                         lpMemOut: pointer on the gray data, result of the conversion\r
-\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-\r
-void RVBtoGray(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte)\r
-{\r
-\r
-       long lIndex = 0;\r
-       unsigned char *lpbData, *lpbDataOut;\r
-       unsigned char red, green, blue;\r
-       long d;\r
-\r
-       //lpbData ppV mem bmp brute\r
-       lpbData = &lpMemIn[0];\r
-       lpbDataOut = &lpMemOut[0];\r
-       // pour toutes les donnees\r
-       while (lIndex < lNbrByte)\r
-       {\r
-               blue  = lpbData[(lIndex * 3)];\r
-               green = lpbData[(lIndex * 3) + 1];\r
-               red   = lpbData[(lIndex * 3) + 2];\r
-               d     = RgbToGray(red, green, blue);\r
-               lpbDataOut[lIndex] = (unsigned char) d;\r
-               lIndex++;\r
-       }\r
-\r
-}\r
-\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-///////                                                         GRAY  THRESHOLD\r
-/////// Input parameters\r
-///////                         lpMemIn : gray data (1 byte by dot)\r
-///////                         lNbrByte: number of byte to deal\r
-\r
-\r
-\r
-/////// Output parameter\r
-///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-\r
-void GrayToThreshold(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte)\r
-{\r
-       long lIndex = 0;\r
-       unsigned char *lpbData, *lpbDataOut;\r
-       int gray, k;\r
-\r
-       //lpbData ppV mem bmp brute\r
-       lpbData = &lpMemIn[0];\r
-       lpbDataOut = &lpMemOut[0];\r
-       // pour toutes les donnees\r
-       while (lIndex < lNbrByte)\r
-\r
-\r
-       {\r
-               gray = lpbData[lIndex];\r
-               k = 0xff - gray;\r
-               if (k > 128)\r
-                       gray = 0xFF;\r
-               else\r
-                       gray = 0x00;\r
-               lpbDataOut[lIndex] = (unsigned char) gray;\r
-               lIndex++;\r
-       }\r
-}\r
-\r
-\r
-\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-///////                                                         GRAY  DITHER\r
-/////// Input parameters\r
-///////                         lpMemIn : gray data (1 byte by dot)\r
-///////                         Height  : height of the BMP image (648)\r
-///////                         Width   : width of the BMP image (1016)\r
-\r
-/////// Output parameter\r
-///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-\r
-void GrayToDither(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height)\r
-{\r
-       long index_line = 0, index_dot = 0;\r
-       unsigned char *lpbData, *lpbDataOut;\r
-       int gray, k;\r
-\r
-\r
-       //lpbData ppV mem bmp brute\r
-       lpbData = &lpMemIn[0];\r
-\r
-       lpbDataOut = &lpMemOut[0];\r
-       // pour toutes les donnees\r
-       if (Height < Width)\r
-       {\r
-               index_line = Width;\r
-               Width = Height;\r
-               Height = index_line;\r
-       }\r
-\r
-\r
-       for (index_line = 0; index_line < Height; index_line++)\r
-       {\r
-               for (index_dot = 0; index_dot < Width; index_dot++)\r
-               {\r
-                       gray = lpbData[index_dot + (index_line * Width)];\r
-                       k = 0xff - gray;\r
-                       if (k > DitherPattern1[index_line % 8][index_dot % 8])\r
-                               gray = 0xFF;\r
-                       else\r
-                               gray = 0x00;\r
-                       lpbDataOut[index_dot + (index_line * Width)] = (unsigned char) gray;\r
-               }\r
-       }\r
-\r
-}\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-///////                                                         GRAY  FLOYD\r
-/////// Input parameters\r
-///////                         lpMemIn : gray data (1 byte by dot)\r
-///////                         Height  : height of the BMP image (648)\r
-///////                         Width   : width of the BMP image (1016)\r
-/////// Output parameter\r
-///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-\r
-void GrayToFloyd(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height)\r
-{\r
-       long index_line = 0, index_dot = 0;\r
-       unsigned char *lpbData, *lpbDataOut;\r
-       int gray, k, l;\r
-       int Error[1032];\r
-       int NextError[1032];\r
-       int Cerror;\r
-       int CE3, CE5, CE7, CE1;\r
-\r
-       if (Height < Width)\r
-       {\r
-               index_line = Width;\r
-\r
-               Width = Height;\r
-               Height = index_line;\r
-\r
-       }\r
-\r
-       for (index_line = 0; index_line < 1032; index_line++)\r
-       {\r
-               Error[index_line] = NextError[index_line] = 0;\r
-       }\r
-       //lpbData ppV mem bmp brute\r
-\r
-\r
-       lpbData = &lpMemIn[0];\r
-       lpbDataOut = &lpMemOut[0];\r
-       // pour toutes les donnees\r
-       for (index_line = 0; index_line < Height; index_line++)\r
-\r
-       {\r
-               //for(index_dot = Width ; index_dot > 0; index_dot--)\r
-               for (index_dot = 0; index_dot < Width; index_dot++)\r
-               {\r
-                       gray = lpbData[index_dot + (index_line * Width)];\r
-\r
-                       k = (gray + (int) Error[index_dot]);\r
-                       if (k > -128 && k < 128)\r
-                               l = 0x00;\r
-\r
-                       else\r
-                               l = 0xFF;\r
-\r
-\r
-                       lpbDataOut[index_dot + (index_line * Width)] = 0xFF - (unsigned char) l;\r
-\r
-\r
-\r
-                       Cerror = k - l;\r
-                       CE3 = (Cerror * 3 / 16);\r
-                       CE5 = (Cerror * 5 / 16);\r
-                       CE7 = (Cerror * 7 / 16);\r
-                       CE1 = Cerror - CE3 - CE5 - CE7;\r
-\r
-                       NextError[index_dot] += CE5;\r
-                       if (index_dot < 1031)\r
-                       {\r
-                               Error[index_dot + 1] += CE7;\r
-                               NextError[index_dot + 1] += CE1;\r
-                       }\r
-                       if (index_dot > 0)\r
-                               NextError[index_dot - 1] += CE3;\r
-               }\r
-               for (index_dot = 0; index_dot < 1032; index_dot++)\r
-               {\r
-                       Error[index_dot] = NextError[index_dot];\r
-                       NextError[index_dot] = 0;\r
-               }\r
-       }\r
-}\r
-\r
-\r
-////////////////////////////////////////////////////////////////////////////////////\r
-\r
-///////                                         RVB  to K\r
-/////// Input parameters\r
-///////                         lpbRVB  : rvb color data from a 24 bits 1 panel BMP file\r
-///////                         RVBSize : number of byte for the color data [(1016*648)]*3\r
-\r
-///////                         Height  : height of the BMP image (648)\r
-///////                         Width   : width of the BMP image (1016)\r
-///////                         Type    : type of monochrome dealing (threshold, floyd or dither)\r
-\r
-/////// Output parameter\r
-\r
-///////                         lpBlack : pointer on the monochrome data, result of the color conversion\r
-//////////////////////////////////////////////////////////////////////////////////////\r
-void ConvertRVBtoK(unsigned char *lpbRVB, long RVBSize, long Height, long Width, unsigned char *lpBlack)\r
-{\r
-       //long  y = 0,x = 0;\r
-       //long  index_line = 0 , index_dot = 0;\r
-\r
-       if (!(strcmp(TreatementK, "GE")))// == NULL)\r
-       {\r
-               GrayToFloyd(lpbRVB, lpBlack, Width, Height);\r
-       }\r
-       else if (!(strcmp(TreatementK, "LM")))// == NULL)\r
-       {\r
-               GrayToThreshold(lpbRVB, lpBlack, (RVBSize / 5));\r
-       }\r
-\r
-\r
-}\r
-\r
-//========================================================//\r
-// OPTION : Not real black printed\r
-// Insert sublimable black from K panel to Y,M & C panels\r
-//========================================================//\r
-void KinYMC(long Height, long Width)\r
-{\r
-       long dwSize, i;\r
-       unsigned char *lpYellow, *lpMagenta, *lpCyan, *lpBlack;\r
-\r
-\r
-       dwSize = Width * Height;\r
-       lpYellow = &lpMem[0];\r
-       lpMagenta = &lpMem[dwSize];\r
-       lpCyan = &lpMem[2 * dwSize];\r
-       lpBlack = &lpMem[3 * dwSize];\r
-       for (i = 0; i < dwSize; i++)\r
-       {\r
-               if (lpBlack[i])\r
-               {\r
-                       lpYellow[i] |= lpBlack[i];\r
-                       lpMagenta[i] |= lpBlack[i];\r
-                       lpCyan[i] |= lpBlack[i];\r
-               }\r
-       }\r
-}\r
\r
-//========================================================//\r
-// OPTION : Get the first YMC printed dots\r
-//========================================================//\r
-void GetFirstYMCDot(long Height, long Width , int *x, int *y)\r
-{\r
-       long dwSize, i=0;\r
-       unsigned char *lpYellow, *lpMagenta, *lpCyan, *lpBlack;\r
-  int fi = 0;\r
-\r
-       dwSize = Width * Height;\r
-       lpYellow = &lpMem[0];\r
-       lpMagenta = &lpMem[dwSize];\r
-       lpCyan = &lpMem[2 * dwSize];\r
-       //for (i = 0; i < dwSize; i++)\r
-  while(i < dwSize && !fi)\r
-       {\r
-                       if(lpYellow[i] || lpMagenta[i] || lpCyan[i] )\r
-        fi = 1;\r
-      i++;\r
-       }\r
-  *x = i % Width;\r
-  *y = i / Width;\r
-}\r
-                  \r
-// End of "evolis.c,v 1.0  ".\r
+
+/*
+ *
+ * Contents:
+ *
+ */
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "evoliserror.h"
+
+// Macros...
+#define RgbToGray(r, g, b) (((long) (r)*74L + (long)(g)*155L +(long)(b)*27L) >> 8)
+
+// Globals...
+int           STATUS=0; //0 means succes, 1 means failure
+unsigned char *lpMem;
+unsigned char *lpRecto,*lpVerso;
+int           Model;            /* Model number */
+long          dwSizeNeeded;     // taille mmoire du lpMem en octets
+
+char
+OverlayPannel[10],
+OverlayBackPannel[10],
+TreatementK[2];
+
+int TB,LB,BB,RB,TW,LW,BW,RW;
+
+int colorspace,levelB;
+
+// *3.5
+int DitherPattern1[8][8] = {
+    {0, 112, 28, 140, 7, 119, 35, 147},
+    {168, 56, 196, 84, 175, 63, 203, 78},
+    {42, 154, 14, 126, 49, 161, 21, 114},
+    {210, 98, 182, 70, 217, 105, 189, 77},
+    {11, 123, 39, 151, 4, 116, 32, 143},
+    {179, 67, 207, 95, 172, 59, 200, 88},
+    {53, 165, 24, 137, 46, 158, 18, 111},
+    {221, 109, 193, 81, 214, 102, 159, 74}
+};
+
+// Prototypes...
+
+// Compress and format data for the printer
+long ReduceBlack(unsigned char *lpMemIn, unsigned char *lpMemOut,int nbrline);
+long ReduceColor(unsigned char *lpMemIn, unsigned char *lpMemOut, int uiBitComp);//,int nbrline);
+//int CutPage(long *stop);
+
+// Download functions
+int DBNC(int col,int bl,int ov);//,int line);
+int DB32NC(long lPos, char color);  //y,m,c pannels 5 bits per color
+int DB64NC(long lPos, char color);  //y,m,c pannels 6 bits per color
+int DB128NC(long lPos, char color); //y,m,c pannels 7 bits per color
+/*int DB32NCS(long lPos, char color,int line);  //y,m,c pannels 5 bits per color
+ int DB64NCS(long lPos, char color,int line);   //y,m,c pannels 6 bits per color
+ int DB128NCS(long lPos, char color,int line);  //y,m,c pannels 7 bits per color*/
+int DB2NC(long lPos, char pannel[10]);  // k,o panel 2 levels
+int DB2MNC(long lPos);      // k panel 2 levels
+
+// Convert RVB to k functions
+void RVBtoGray(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte);
+void KinYMC(long Height, long Width);
+void GrayToFloyd(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height);
+void GrayToDither(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height);
+void GrayToThreshold(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte);
+void ConvertRVBtoK(unsigned char *lpbRVB, long RVBSize, long Height, long Width, unsigned char *lpBlack,int face);
+void GetFirstYMCDot(long Height, long Width , int *x, int *y);
+
+
+//===============================================================================//
+// Reduce color data from 8 bits to uiBitComp.
+
+// Entree : 1 color panel (1016 * 648 octets)
+// Sortie : 1 color panel with usable data ((1016*648)*uiBitComp)/8
+//===============================================================================//
+
+
+long ReduceColor(unsigned char *lpMemIn, unsigned char *lpMemOut, int uiBitComp)//,int nbrline)
+{
+    long lIndex = 0, lIndex1 = 0;
+    unsigned char *lpbBuf, *lpbData, *lpbDataOut;
+    long lComp = 0;
+    long lNbrByte = (1016*648);
+    
+    //Reservation m√î√∏Œ©oire pour traitement d'une ligne
+    
+    lpbBuf = malloc(8);
+    
+    if (!lpbBuf)
+    {
+        
+        //fprintf(stderr, "**************** EVOLIS ReduceColor manque de memoire pour lpBuf: taille demande 8 octets... \n");
+        fatal("ReduceColor Fails memory lpBuf: requested size 8 bytes...");
+        
+        /// sortir de l'impression....
+        return (0);
+    }
+    
+    //lpbData ppV mem bmp brute
+    lpbData = &lpMemIn[0];
+    
+    lpbDataOut = &lpMemOut[0];
+    // pour toutes les donnees
+    while (lIndex < lNbrByte)
+    {
+        //Traite byte par 8
+        for (lIndex1 = 0; lIndex1 < 8; lIndex1++)
+        {
+            if (lIndex < lNbrByte)
+            {
+                //decalage donnees utiles
+                lpbBuf[lIndex1] = *(lpbData++) >> (8 - uiBitComp);
+                lIndex++;
+            }
+            else
+            {
+                
+                lpbBuf[lIndex1] = 0x00;
+            }
+        }
+        
+        switch (uiBitComp)
+        {
+            case 6:
+                *(lpbDataOut++) = (lpbBuf[0] << 2) | (lpbBuf[1] >> 4);
+                *(lpbDataOut++) = (lpbBuf[1] << 4) | (lpbBuf[2] >> 2);
+                *(lpbDataOut++) = (lpbBuf[2] << 6) | (lpbBuf[3]);
+                *(lpbDataOut++) = (lpbBuf[4] << 2) | (lpbBuf[5] >> 4);
+                *(lpbDataOut++) = (lpbBuf[5] << 4) | (lpbBuf[6] >> 2);
+                *(lpbDataOut++) = (lpbBuf[6] << 6) | (lpbBuf[7]);
+                break;
+            case 7:
+                *(lpbDataOut++) = (lpbBuf[0] << 1) | (lpbBuf[1] >> 6);
+                *(lpbDataOut++) = (lpbBuf[1] << 2) | (lpbBuf[2] >> 5);
+                *(lpbDataOut++) = (lpbBuf[2] << 3) | (lpbBuf[3] >> 4);
+                *(lpbDataOut++) = (lpbBuf[3] << 4) | (lpbBuf[4] >> 3);
+                
+                *(lpbDataOut++) = (lpbBuf[4] << 5) | (lpbBuf[5] >> 2);
+                *(lpbDataOut++) = (lpbBuf[5] << 6) | (lpbBuf[6] >> 1);
+                *(lpbDataOut++) = (lpbBuf[6] << 7) | (lpbBuf[7]);
+                break;
+                
+            default:    //5 bits
+                *(lpbDataOut++) = (lpbBuf[0] << 3) | (lpbBuf[1] >> 2);
+                *(lpbDataOut++) = (lpbBuf[1] << 6) | (lpbBuf[2] << 1) | (lpbBuf[3] >> 4);
+                *(lpbDataOut++) = (lpbBuf[3] << 4) | (lpbBuf[4] >> 1);
+                
+                *(lpbDataOut++) = (lpbBuf[4] << 7) | (lpbBuf[5] << 2) | (lpbBuf[6] >> 3);
+                *(lpbDataOut++) = (lpbBuf[6] << 5) | (lpbBuf[7]);
+                //break;
+        }
+    }
+    free(lpbBuf);
+    lComp = (((lNbrByte * 10) / 8) * uiBitComp);
+    lComp /= 10;
+    if (lNbrByte % 8)
+        lComp++;
+    
+    
+    return (lComp);
+}
+
+//===================================================//
+// fonction pour le panneau noir & overlay uniquement
+// entree : 1 octect = 1 points
+// sortie : 1 octect = 8 points 
+
+
+
+//===================================================//
+long ReduceBlack(unsigned char *lpMemIn, unsigned char *lpMemOut,int nbrline)
+{
+    long lIndex = 0, lIndex1 = 0;
+    unsigned char *lpbData, *lpbDataOut;
+    long lComp = 0;
+    unsigned char bBuf = 0x00;
+    
+    unsigned char Mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
+    long lNbrByte = nbrline * 648;
+    
+    
+    //lpbData ppV mem bmp brute
+    lpbData = &lpMemIn[0];
+    lpbDataOut = &lpMemOut[0];
+    // pour toutes les donnees
+    
+    while (lIndex < lNbrByte)
+    {
+        
+        
+        //Traite byte par 8
+        for (lIndex1 = 0; lIndex1 < 8; lIndex1++)
+            
+        {
+            if (lIndex < lNbrByte)
+            {
+                if (*(lpbData++) == 0xFF)
+                {
+                    bBuf |= Mask[lIndex1];
+                }
+                lIndex++;
+            }
+        }
+        *(lpbDataOut++) = bBuf;
+        bBuf = 0x00;
+        lComp++;
+    }
+    return (lComp);
+}
+
+//=====================================================================
+//   Convert bitmap RVB data to evolis data format                             
+//=====================================================================
+
+
+int DBNC(int col,int bl,int ov)//,int line)
+{
+    //output("\033Ss\015"); // debut sequence + CR
+    
+    if(col == 1) {
+        DB32NC(0 * (dwSizeNeeded / 5), 'y');
+        DB32NC(1 * (dwSizeNeeded / 5), 'm');
+        DB32NC(2 * (dwSizeNeeded / 5), 'c');
+    }
+    else if (col == 2) {
+        DB64NC(0 * (dwSizeNeeded / 5), 'y');
+        DB64NC(1 * (dwSizeNeeded / 5), 'm');
+        DB64NC(2 * (dwSizeNeeded / 5), 'c');
+    }
+    else if (col == 3) {
+        DB128NC(0 * (dwSizeNeeded / 5), 'y');
+        DB128NC(1 * (dwSizeNeeded / 5), 'm');
+        DB128NC(2 * (dwSizeNeeded / 5), 'c');
+    }
+    
+    if(bl){
+        DB2NC(3 * (dwSizeNeeded / 5), "ABP");
+    }
+    
+    if(ov){
+        if(ov == 1){//overlay at front
+            DB2NC(0, OverlayPannel);
+        }
+        else {
+            DB2NC(0, OverlayBackPannel);
+        }
+    }
+    
+    //output("\033Se\015.....................................");    // fin sequence + CR
+    
+}
+
+//===============================================================//
+// Telechargement d'un panneau non compresse 5 bits
+// Input : ucData / raw data
+//         color /y,m or c      
+// Return:      0 si OK
+//              -1 si erreur ecriture  
+//===============================================================//
+int DB32NC(long lPos, char color)
+{
+    unsigned char *ucCommand, *ucCol;
+    long lCommandSize = ((1016 * 648) * 5) / 8;
+    long lComp = 0;
+    int  numwritten = 0;
+    
+    //Reservation memoire pour une commande de telechargement
+    ucCommand = malloc(lCommandSize + 12);
+    if (!ucCommand) {
+        fatal("DP32NC Error: Fails malloc... ");
+        return (-1);
+    }
+    
+    ucCol = &lpMem[lPos];
+    strcpy((char*)  ucCommand, "\033Db;y;32;");
+    strncpy((char*) &ucCommand[4], &color, 1);
+    
+    lComp = ReduceColor(ucCol, &ucCommand[9], 5);//,1016);
+    lCommandSize += 10;
+    numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+    if (numwritten != lCommandSize) {
+        fatal("DP32NC Error: Fails printf %d... \n", numwritten);
+        free(ucCommand);
+        return (-1);
+    }
+    /*lComp += 10;
+     while(lComp > 4096) {
+     numwritten += fwrite(&ucCommand[numwritten], sizeof(unsigned char), 4096, stdout);
+     lComp -= 4096;     
+     }
+     debug("NuwWritten %d", numwritten);
+     if(lComp > 0) {
+     numwritten += fwrite(&ucCommand[numwritten], sizeof(unsigned char), lComp, stdout);
+     }
+     debug("lComp %d numwritten %d lCommandSize %d", lComp,numwritten,lCommandSize);
+     if (numwritten != lCommandSize) {
+     fatal("DP32NC Error: Fails printf %d... \n", numwritten);
+     free(ucCommand);
+     return (-1);
+     }*/
+    
+    
+    free(ucCommand);
+    return (0);
+}
+
+//===============================================================//
+// Telechargement d'un panneau non compresse 6 bits
+// Input : ucData / raw data
+//         color /y,m or c      
+// Return:      0 si OK
+//              -1 si erreur ecriture  
+//===============================================================//
+
+int DB64NC(long lPos, char color)
+{
+    unsigned char *ucCommand, *ucCol;
+    
+    
+    long lCommandSize = ((1016 * 648) * 6) / 8;
+    long lComp;
+    int numwritten = 0;
+    
+    
+    //Reservation memoire pour une commande de telechargement
+    ucCommand = malloc(lCommandSize + 10);
+    debug("DP64NC Size for one panel: %d... \n", (lCommandSize + 10));
+    
+    if (!ucCommand)
+    {
+        
+        fatal("DP64NC Error: Fails malloc...");
+        
+        free(ucCommand);
+        return (-1);
+        
+    }
+    ucCol = &lpMem[lPos];
+    strcpy((char*) ucCommand, "\033Db;y;64;");
+    strncpy((char*) &ucCommand[4], &color, 1);
+    
+    
+    lComp = ReduceColor(ucCol, &ucCommand[9], 6);//,1016);
+    //fprintf(stderr, "**************** EVOLIS DP64NC taille apres compression %d... \n",lComp);
+    strncpy((char*)&ucCommand[lCommandSize + 9], "\015", 1);
+    lCommandSize += 10;
+    numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+    if (numwritten != lCommandSize)
+    {
+        fatal("DP64NC Error: Fails fwrite %d... \n", numwritten);
+        
+        free(ucCommand);
+        return (-1);
+    }
+    
+    free(ucCommand);
+    
+    return (0);
+}
+
+
+//===============================================================//
+// Telechargement d'un panneau non compresse 7 bits
+// Input : ucData / raw data
+//         color /y,m or c      
+// Return:      0 si OK
+//              -1 si erreur ecriture  
+//===============================================================//
+int DB128NC(long lPos, char color)
+
+{
+    unsigned char *ucCommand, *ucCol;
+    long lCommandSize = ((1016 * 648) * 7) / 8;
+    long lComp;
+    int numwritten = 0;
+    
+    //Reservation memoire pour une commande de telechargement
+    ucCommand = malloc(lCommandSize + 11);
+    debug("DP128NC Size for one panel: %d... ", (lCommandSize + 11));
+    if (!ucCommand)
+    {
+        fatal("DP128NC Error: Fails malloc... ");
+        
+        free(ucCommand);
+        return (-1);
+    }
+    ucCol = &lpMem[lPos];
+    strcpy((char*) ucCommand, "\033Db;y;128;");
+    strncpy((char*) &ucCommand[4], &color, 1);
+    lComp = ReduceColor(ucCol, &ucCommand[10], 7);//,1016);
+    strncpy((char*) &ucCommand[lCommandSize + 10], "\015", 1);
+    lCommandSize += 11;
+    numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+    if (numwritten != lCommandSize)
+    {
+        fatal("DP128NC Error: Fails fwrite %d... \n", numwritten);
+        
+        free(ucCommand);
+        return (-1);
+    }
+    free(ucCommand);
+    return (0);
+}
+//===============================================================//
+// Telechargement d'un panneau non compresse 5 bits YMCKOS
+// Input : ucData / raw data
+//         color /y,m or c
+// Return:      0 si OK
+//              -1 si erreur ecriture
+//===============================================================//
+/*int DB32NCS(long lPos, char color,int line)
+ {
+ unsigned char *ucCommand, *ucCol;
+ long lCommandSize = ((1016 * 648) * 5) / 8;
+ long lComp = 0;
+ int numwritten = 0;
+ //Reservation memoire pour une commande de telechargement
+ ucCommand = malloc(lCommandSize + 20);
+ //fprintf(stderr, "**************** EVOLIS DP32NC Size for one panel: %d... \n", (lCommandSize+10));
+ if (!ucCommand)
+ {
+ //fprintf(stderr, "**************** EVOLIS DP32NC Error: Fails malloc... \n");
+ fatal("DP32NC Error: Fails malloc... ");
+ return (-1);
+ }
+ ucCol = &lpMem[lPos];
+ //strcpy(ucCommand, "\033Dbc;y;32;");
+ //nbroct = 540 * 648;
+ lCommandSize = ((520 * 648) * 5) / 8;
+ sprintf(ucCommand, "\033Dbc;y;32;%.4d;%.6d;",line,lCommandSize);
+ strncpy(&ucCommand[5], &color, 1);
+ info("%s",ucCommand);
+ lComp = ReduceColor(ucCol, &ucCommand[22], 5, 520);
+ strncpy(&ucCommand[lCommandSize + 22], "\015", 1);
+ lCommandSize += 23;
+ numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+ //info("%d  %s", numwritten,ucCommand);
+ if (numwritten != lCommandSize)
+ {
+ //fprintf(stderr, "**************** EVOLIS DP32NC Error: Fails printf %d... \n",numwritten);
+ fatal("DP32NC Error: Fails printf %d... \n", numwritten);
+ free(ucCommand);
+ return (-1);
+ }
+ free(ucCommand);
+ return (0);
+ }
+ //===============================================================//
+ // Telechargement d'un panneau non compresse 6 bits YMCKOS
+ // Input : ucData / raw data
+ //         color /y,m or c
+ // Return:      0 si OK
+ //              -1 si erreur ecriture
+ //===============================================================//
+ int DB64NCS(long lPos, char color,int line)
+ {
+ unsigned char *ucCommand, *ucCol;
+ long lCommandSize = ((1016 * 648) * 6) / 8;
+ long lComp;
+ int numwritten = 0;
+ //Reservation memoire pour une commande de telechargement
+ ucCommand = malloc(lCommandSize + 10);
+ debug("DP64NC Size for one panel: %d... \n", (lCommandSize + 10));
+ if (!ucCommand)
+ {
+ fatal("DP64NC Error: Fails malloc...");
+ free(ucCommand);
+ return (-1);
+ }
+ ucCol = &lpMem[lPos];
+ strcpy(ucCommand, "\033Db;y;64;");
+ strncpy(&ucCommand[4], &color, 1);
+ lComp = ReduceColor(ucCol, &ucCommand[9], 6, 540);
+ //fprintf(stderr, "**************** EVOLIS DP64NC taille apres compression %d... \n",lComp);
+ strncpy(&ucCommand[lCommandSize + 9], "\015", 1);
+ lCommandSize += 10;
+ numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+ if (numwritten != lCommandSize)
+ {
+ fatal("DP64NC Error: Fails fwrite %d... \n", numwritten);
+ free(ucCommand);
+ return (-1);
+ }
+ free(ucCommand);
+ return (0);
+ }
+ //===============================================================//
+ // Telechargement d'un panneau non compresse 7 bits YMCKOS
+ // Input : ucData / raw data
+ //         color /y,m or c
+ // Return:      0 si OK
+ //              -1 si erreur ecriture
+ //===============================================================//
+ int DB128NCS(long lPos, char color,int line)
+ {
+ unsigned char *ucCommand, *ucCol;
+ long lCommandSize = ((1016 * 648) * 7) / 8;
+ long lComp;
+ int numwritten = 0;
+ //Reservation memoire pour une commande de telechargement
+ ucCommand = malloc(lCommandSize + 11);
+ //fprintf(stderr, "**************** EVOLIS DP128NC Size for one panel: %d... \n", (lCommandSize+11));
+ debug("DP128NC Size for one panel: %d... ", (lCommandSize + 11));
+ if (!ucCommand)
+ {
+ fatal("DP128NC Error: Fails malloc... ");
+ free(ucCommand);
+ return (-1);
+ }
+ ucCol = &lpMem[lPos];
+ strcpy(ucCommand, "\033Db;y;128;");
+ strncpy(&ucCommand[4], &color, 1);
+ lComp = ReduceColor(ucCol, &ucCommand[10], 7, 540);
+ //fprintf(stderr, "**************** EVOLIS DP128NC taille apres compression %d... \n",lComp);
+ strncpy(&ucCommand[lCommandSize + 10], "\015", 1);
+ lCommandSize += 11;
+ numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+ if (numwritten != lCommandSize)
+ {
+ fatal("DP128NC Error: Fails fwrite %d... \n", numwritten);
+ free(ucCommand);
+ return (-1);
+ }
+ free(ucCommand);
+ return (0);
+ } */
+//===============================================================//
+// Telechargement d'un panneau non compresse overlay noir
+// Input : ucData / raw datal978
+//         color /      k: noir
+//                      f: full varnish 
+// Return:      0 si OK
+//              -1 si erreur ecriture  
+//===============================================================//
+
+int DB2NC(long lPos, char pannel[10])
+{
+    unsigned char *ucCommand, *ucCol;
+    long lComp;
+    
+    long lCommandSize = ((1016 * 648)) / 8;
+    int numwritten = 0;
+    int B, T, L, R, i = 0, j = 0;
+    int pB, pT, ModuloB, ModuloT;
+    
+    //unsigned char        MaskB[8] = { 0x00,0x80,0xC0,0xE0,0xF0,0x7F,0x3F,0x1F};
+    //unsigned char        MaskT[8] = { 0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F};
+    unsigned char MaskB[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
+    unsigned char MaskT[8] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };
+    
+    
+    //Reservation memoire pour une commande de telechargement
+    
+    ucCommand = malloc(lCommandSize + 9);
+    
+    //debug("DP2NC Size for one panel: %d... ", (lCommandSize + 9));
+    
+    
+    if (!ucCommand)
+    {
+        fatal("DP2NC Error: Fails malloc... ");
+        free(ucCommand);
+        return (-1);
+    }
+    
+    if (!(strcmp(pannel, "ABP")))// == NULL)
+    {           // panneau noir
+        ucCol = &lpMem[lPos];
+        strcpy((char*) ucCommand, "\033Db;k;2;");
+        lComp = ReduceBlack(ucCol, &ucCommand[8],1016);
+        strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
+        lCommandSize += 9;
+        numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+        if (numwritten != lCommandSize)
+        {
+            fatal("DP2NC Error: Fails fprintf %d... ", numwritten);
+            
+            free(ucCommand);
+            return (-1);
+        }
+    }
+    else if (!(strcmp(pannel, "NO")))// == NULL)
+    {       //No Varnish
+        memset(ucCommand, 0x00, lCommandSize);
+        strcpy((char*)ucCommand, "\033Db;o;2;");
+        strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
+        lCommandSize += 9;
+        numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+        if (numwritten != lCommandSize)
+        {
+            fatal("DP2NC Error: Fails fwrite %d... ", numwritten);
+            free(ucCommand);
+            return (-1);
+        }
+    }
+    else if (!(strcmp(pannel, "FO")))// == NULL)
+    {   //Full Varnish
+        memset(ucCommand, 0xFF, lCommandSize);
+        strcpy((char*) ucCommand, "\033Db;o;2;");
+        strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
+        lCommandSize += 9;
+        numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+        if (numwritten != lCommandSize)
+        {
+            fatal("DP2NC Error: Fails fwrite %d... ", numwritten);
+            free(ucCommand);
+            return (-1);
+        }
+    }
+    else
+    {
+        
+        if (!(strcmp(pannel, "OA")))// == NULL)
+        {
+            //Cover Overlay
+            B = BB * 12;
+            T = TB * 12;
+            L = LB * 12;
+            R = RB * 12;
+            debug("DP2NC OA... ");
+            memset(ucCommand, 0x00, lCommandSize);
+            
+            
+            for (j = L; j <= R; j++)
+                
+            {
+                pB = (j * 648) + (648 - B);
+                ModuloB = pB % 8;
+                pB /= 8;
+                if (ModuloB)
+                    pB++;
+                ucCommand[pB + 7] = MaskB[ModuloB];
+                pB++;
+                pT = (j * 648) + (648 - T);
+                ModuloT = pT % 8;
+                pT /= 8;
+                if (ModuloT)
+                    pT++;
+                ucCommand[pT + 7] = MaskT[ModuloT];
+                pT--;
+                for (i = (pB); i <= (pT); i++)
+                {
+                    ucCommand[i + 7] = 0xFF;
+                }
+            }
+            B = BW * 12;
+            T = TW * 12;
+            L = LW * 12;
+            R = RW * 12;
+        }
+        else if (!(strcmp(pannel, "SCI")))// == NULL)
+        {
+            B = 319;
+            T = 106;
+            L = 71;
+            R = 307;
+            
+            memset(ucCommand, 0xFF, lCommandSize);
+            debug("DP2NC SCI... ");
+        }
+        else if (!(strcmp(pannel, "SCA")))// == NULL)
+        {
+            B = 354;
+            T = 177;
+            L = 71;
+            R = 283;
+            
+            memset(ucCommand, 0xFF, lCommandSize);
+            debug("DP2NC SCA... ");
+        } 
+        else if (!(strcmp(pannel, "MS")))// == NULL)
+        {
+            B = 216;
+            T = 60;
+            L = 0;
+            R = 1016;
+            
+            memset(ucCommand, 0xFF, lCommandSize);
+            debug("DP2NC MS... ");
+        }
+        //White part
+        for (j = L; j <= R; j++)
+        {
+            pB = (j * 648) + (648 - B);
+            ModuloB = pB % 8;
+            pB /= 8;
+            if (ModuloB)
+                pB++;
+            ucCommand[pB + 7] = MaskB[ModuloB];
+            pB++;
+            pT = (j * 648) + (648 - T);
+            ModuloT = pT % 8;
+            pT /= 8;
+            if (ModuloT)
+                pT++;
+            ucCommand[pT + 7] = MaskT[ModuloT];
+            pT--;
+            for (i = (pB); i <= (pT); i++)
+            {
+                ucCommand[i + 7] = 0x00;
+            }
+        }//end of white part
+        
+        strcpy((char*) ucCommand, "\033Db;o;2;");
+        
+        strncpy((char*) &ucCommand[lCommandSize + 8], "\015", 1);
+        lCommandSize += 9;
+        numwritten = fwrite(ucCommand, sizeof(unsigned char), lCommandSize, stdout);
+        if (numwritten != lCommandSize)
+        {
+            fatal("DP2NC Error: Fails fwrite %d... ", numwritten);
+            free(ucCommand);
+            return (-1);
+        }
+        
+        
+    }
+    
+    free(ucCommand);
+    return (0);
+}
+
+//=====================================================================
+//   Convert color bitmap to monochrome bitmap                         
+//=====================================================================
+
+//////////////////////////////////////////////////////////////////////////////////////
+///////                                                         RVB TO GRAY  
+/////// Input parameters
+///////                         lpMemIn : RVB data (3 byte by dot)
+///////                         lNbrByte: number of byte to deal
+/////// Output parameter
+///////                         lpMemOut: pointer on the gray data, result of the conversion
+
+
+//////////////////////////////////////////////////////////////////////////////////////
+
+void RVBtoGray(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte)
+{
+    
+    long lIndex = 0;
+    unsigned char *lpbData, *lpbDataOut;
+    unsigned char red, green, blue;
+    long d;
+    
+    //lpbData ppV mem bmp brute
+    lpbData = &lpMemIn[0];
+    lpbDataOut = &lpMemOut[0];
+    // pour toutes les donnees
+    while (lIndex < lNbrByte)
+    {
+        blue  = lpbData[(lIndex * 3)];
+        green = lpbData[(lIndex * 3) + 1];
+        red   = lpbData[(lIndex * 3) + 2];
+        d     = RgbToGray(red, green, blue);
+        lpbDataOut[lIndex] = (unsigned char) d;
+        lIndex++;
+    }
+    
+}
+
+
+//////////////////////////////////////////////////////////////////////////////////////
+///////                                                         GRAY  THRESHOLD
+/////// Input parameters
+///////                         lpMemIn : gray data (1 byte by dot)
+///////                         lNbrByte: number of byte to deal
+
+
+
+/////// Output parameter
+///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion
+
+//////////////////////////////////////////////////////////////////////////////////////
+
+void GrayToThreshold(unsigned char *lpMemIn, unsigned char *lpMemOut, long lNbrByte)
+{
+    long lIndex = 0;
+    unsigned char *lpbData, *lpbDataOut;
+    int gray, k;
+    
+    //lpbData ppV mem bmp brute
+    lpbData = &lpMemIn[0];
+    lpbDataOut = &lpMemOut[0];
+    // pour toutes les donnees
+    while (lIndex < lNbrByte)
+        
+        
+    {
+        gray = lpbData[lIndex];
+        k = 0xff - gray;
+        if (k > 128)
+            gray = 0xFF;
+        else
+            gray = 0x00;
+        lpbDataOut[lIndex] = (unsigned char) gray;
+        lIndex++;
+    }
+}
+
+
+
+
+//////////////////////////////////////////////////////////////////////////////////////
+///////                                                         GRAY  DITHER
+/////// Input parameters
+///////                         lpMemIn : gray data (1 byte by dot)
+///////                         Height  : height of the BMP image (648)
+///////                         Width   : width of the BMP image (1016)
+
+/////// Output parameter
+///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion
+//////////////////////////////////////////////////////////////////////////////////////
+
+void GrayToDither(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height)
+{
+    long index_line = 0, index_dot = 0;
+    unsigned char *lpbData, *lpbDataOut;
+    int gray, k;
+    
+    
+    //lpbData ppV mem bmp brute
+    lpbData = &lpMemIn[0];
+    
+    lpbDataOut = &lpMemOut[0];
+    // pour toutes les donnees
+    if (Height < Width)
+    {
+        index_line = Width;
+        Width = Height;
+        Height = index_line;
+    }
+    
+    
+    for (index_line = 0; index_line < Height; index_line++)
+    {
+        for (index_dot = 0; index_dot < Width; index_dot++)
+        {
+            gray = lpbData[index_dot + (index_line * Width)];
+            k = 0xff - gray;
+            if (k > DitherPattern1[index_line % 8][index_dot % 8])
+                gray = 0xFF;
+            else
+                gray = 0x00;
+            lpbDataOut[index_dot + (index_line * Width)] = (unsigned char) gray;
+        }
+    }
+    
+}
+
+//////////////////////////////////////////////////////////////////////////////////////
+///////                                                         GRAY  FLOYD
+/////// Input parameters
+///////                         lpMemIn : gray data (1 byte by dot)
+///////                         Height  : height of the BMP image (648)
+///////                         Width   : width of the BMP image (1016)
+/////// Output parameter
+///////                         lpMemOut: pointer on the monochrome data, result of the gray conversion
+//////////////////////////////////////////////////////////////////////////////////////
+
+void GrayToFloyd(unsigned char *lpMemIn, unsigned char *lpMemOut, long Width, long Height)
+{
+    long index_line = 0, index_dot = 0;
+    unsigned char *lpbData, *lpbDataOut;
+    int gray, k, l;
+    int Error[1032];
+    int NextError[1032];
+    int Cerror;
+    int CE3, CE5, CE7, CE1;
+    
+    if (Height < Width)
+    {
+        index_line = Width;
+        
+        Width = Height;
+        Height = index_line;
+        
+    }
+    
+    for (index_line = 0; index_line < 1032; index_line++)
+    {
+        Error[index_line] = NextError[index_line] = 0;
+    }
+    //lpbData ppV mem bmp brute
+    
+    
+    lpbData = &lpMemIn[0];
+    lpbDataOut = &lpMemOut[0];
+    // pour toutes les donnees
+    for (index_line = 0; index_line < Height; index_line++)
+        
+    {
+        //for(index_dot = Width ; index_dot > 0; index_dot--)
+        for (index_dot = 0; index_dot < Width; index_dot++)
+        {
+            gray = lpbData[index_dot + (index_line * Width)];
+            
+            k = (gray + (int) Error[index_dot]);
+            if (k > -128 && k < 128)
+                l = 0x00;
+            
+            else
+                l = 0xFF;
+            
+            
+            lpbDataOut[index_dot + (index_line * Width)] = 0xFF - (unsigned char) l;
+            
+            
+            
+            Cerror = k - l;
+            CE3 = (Cerror * 3 / 16);
+            CE5 = (Cerror * 5 / 16);
+            CE7 = (Cerror * 7 / 16);
+            CE1 = Cerror - CE3 - CE5 - CE7;
+            
+            NextError[index_dot] += CE5;
+            if (index_dot < 1031)
+            {
+                Error[index_dot + 1] += CE7;
+                NextError[index_dot + 1] += CE1;
+            }
+            if (index_dot > 0)
+                NextError[index_dot - 1] += CE3;
+        }
+        for (index_dot = 0; index_dot < 1032; index_dot++)
+        {
+            Error[index_dot] = NextError[index_dot];
+            NextError[index_dot] = 0;
+        }
+    }
+}
+
+
+////////////////////////////////////////////////////////////////////////////////////
+
+///////                                         RVB  to K
+/////// Input parameters
+///////                         lpbRVB  : rvb color data from a 24 bits 1 panel BMP file
+///////                         RVBSize : number of byte for the color data [(1016*648)]*3
+
+///////                         Height  : height of the BMP image (648)
+///////                         Width   : width of the BMP image (1016)
+///////                         Type    : type of monochrome dealing (threshold, floyd or dither)
+///////                         face    : RECTO / VERSO 
+
+/////// Output parameter
+
+///////                         lpBlack : pointer on the monochrome data, result of the color conversion
+//////////////////////////////////////////////////////////////////////////////////////
+void ConvertRVBtoK(unsigned char *lpbRVB, long RVBSize, long Height, long Width, unsigned char *lpBlack, int face)
+{
+    if ( toupper(TreatementK[face]) =='G' ) // Grey Mode
+    {
+        GrayToFloyd(lpbRVB, lpBlack, Width, Height);
+    }
+    else // Line Mode
+    {
+        GrayToThreshold(lpbRVB, lpBlack, (RVBSize / 5));
+    }
+}
+
+//========================================================//
+// OPTION : Not real black printed
+// Insert sublimable black from K panel to Y,M & C panels
+//========================================================//
+void KinYMC(long Height, long Width)
+{
+    long dwSize, i;
+    unsigned char *lpYellow, *lpMagenta, *lpCyan, *lpBlack;
+    
+    
+    dwSize = Width * Height;
+    lpYellow = &lpMem[0];
+    lpMagenta = &lpMem[dwSize];
+    lpCyan = &lpMem[2 * dwSize];
+    lpBlack = &lpMem[3 * dwSize];
+    for (i = 0; i < dwSize; i++)
+    {
+        if (lpBlack[i])
+        {
+            lpYellow[i] |= lpBlack[i];
+            lpMagenta[i] |= lpBlack[i];
+            lpCyan[i] |= lpBlack[i];
+        }
+    }
+}
+
+//========================================================//
+// OPTION : Get the first YMC printed dots
+//========================================================//
+void GetFirstYMCDot(long Height, long Width , int *x, int *y)
+{
+    long dwSize, i=0;
+    unsigned char *lpYellow, *lpMagenta, *lpCyan, *lpBlack;
+    int fi = 0;
+    
+    dwSize = Width * Height;
+    lpYellow = &lpMem[0];
+    lpMagenta = &lpMem[dwSize];
+    lpCyan = &lpMem[2 * dwSize];
+    //for (i = 0; i < dwSize; i++)
+    while(i < dwSize && !fi)
+    {
+        if(lpYellow[i] || lpMagenta[i] || lpCyan[i] )
+            fi = 1;
+        i++;
+    }
+    *x = i % Width;
+    *y = i / Width;
+}
+
+// End of "evolis.c,v 1.0  ".