Tizen 2.0 Release
[external/epson-laser-printer-escpage.git] / src / filter.c
1 /*
2  * EPSON ESC/P-R Printer Driver for Linux
3  * Copyright (C) 2002-2005 AVASYS CORPORATION.
4  * Copyright (C) Seiko Epson Corporation 2002-2005.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
19  *
20  * As a special exception, AVASYS CORPORATION gives permission to
21  * link the code of this program with libraries which are covered by
22  * the AVASYS Public License and distribute their linked
23  * combinations.  You must obey the GNU General Public License in all
24  * respects for all of the code used other than the libraries which
25  * are covered by AVASYS Public License.
26  */
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #define HAVE_PPM (0)
32
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <cups/cups.h>
41 #include <cups/raster.h>
42
43 #include "err.h"
44 #include "mem.h"
45 #include "optBase.h"
46 #include "epson-escpr-def.h"
47 #include "epson-escpr-media.h"
48 #include "epson-escpr-pvt.h"
49 #include "epson-escpr-api.h"
50 #include "epson-escpr-services.h"
51 #include "epson-escpr-mem.h"
52 #include "epson-escpage.h" 
53 #include "linux_cmn.h"
54
55 #define PATH_MAX 256
56 #define NAME_MAX 41
57
58 #define EPS_PARAM_MP_TRAY       0x00
59 #define EPS_PARAM_CASSETTE1       0x01
60 #define EPS_PARAM_CASSETTE2       0x02
61 #define EPS_PARAM_CASSETTE3       0x03
62 #define EPS_PARAM_CASSETTE4       0x04
63 #define EPS_PARAM_MANUALFEED        0x0A
64 #define EPS_PARAM_AUTO_TRAY       0xFF
65
66 #define WIDTH_BYTES(bits) (((bits) + 31) / 32 * 4)
67
68 #define PIPSLITE_FILTER_VERSION "* epson-escpr is a part of " PACKAGE_STRING
69
70 #define PIPSLITE_FILTER_USAGE "Usage: $ epson-escpr model width_pixel height_pixel Ink PageSize Quality"
71
72
73 #ifndef EPS_END_PAGE
74 #define EPS_END_PAGE                    0       /* There is no next page                */
75 #endif
76
77 #ifndef EPS_NEXT_PAGE
78 #define EPS_NEXT_PAGE                   1       /* There is a next page                 */
79 #endif
80
81 #define PIPSLITE_WRAPPER_VERSION "* epson-escpr-wrapepr is a part of " PACKAGE_STRING
82 #define CUPS_FILTER_PATH "/opt/epson-abcdef/"
83 #define CUPS_FILTER_NAME "epson-escpage"
84
85 typedef struct rtp_filter_option {
86   char model[NAME_MAX];
87   char model_low[NAME_MAX];
88   char ink[NAME_MAX];
89   char media[NAME_MAX];
90   char quality[NAME_MAX];
91   char duplex[NAME_MAX];
92 } filter_option_t;
93
94 int cancel_flg = 0;
95
96 /* static functions */
97 static int getMediaTypeID(char *);
98 static int getMediaSizeID(char *);
99 const int band_line = 1;
100
101 extern EPS_INT32    libStatus;                  /*  Library (epsInitDriver) status      */
102 extern EPS_PRINT_JOB   printJob;
103 extern EPS_UINT32   sendDataBufSize;
104 extern EPS_UINT8*   sendDataBuf;    /* buffer of SendCommand(save) input                */
105 extern EPS_UINT32   tmpLineBufSize;
106 extern EPS_UINT8*   tmpLineBuf;
107
108 extern EPS_CMN_FUNC epsCmnFnc;
109
110 extern EPS_INT32 back_type;
111 extern EPS_INT32 lWidth;
112 extern EPS_INT32 lHeight;
113 extern EPS_INT32 areaWidth;
114 extern EPS_INT32 areaHeight;
115
116 #ifndef ESCPR_HEADER_LENGTH 
117 #define ESCPR_HEADER_LENGTH            10    /* ESC + CLASS + ParamLen + CmdName */ 
118 #endif
119
120 #ifndef ESCPR_SEND_DATA_LENGTH
121 #define ESCPR_SEND_DATA_LENGTH          7
122 #endif
123
124
125 EPS_ERR_CODE epsInitLib(){
126   EPS_CMN_FUNC cmnFuncPtrs;
127   memset(&cmnFuncPtrs, 0, sizeof(EPS_CMN_FUNC));
128   
129   cmnFuncPtrs.version = EPS_CMNFUNC_VER_CUR;
130   cmnFuncPtrs.findCallback = NULL;
131   cmnFuncPtrs.memAlloc = &epsmpMemAlloc;
132   cmnFuncPtrs.memFree = &epsmpMemFree;
133   cmnFuncPtrs.sleep = &epsmpSleep;
134   cmnFuncPtrs.getTime = &epsmpGetTime;
135   cmnFuncPtrs.getLocalTime = &epsmpGetLocalTime;
136   cmnFuncPtrs.lockSync = &epsmpLockSync;
137   cmnFuncPtrs.unlockSync = &epsmpUnlockSync;
138   cmnFuncPtrs.stateCallback = NULL; /* current version unused */
139
140   memcpy((void*)(&epsCmnFnc), (void*)&cmnFuncPtrs, sizeof(EPS_CMN_FUNC));
141 }
142
143 EPS_ERR_CODE epsInitJob(){
144   
145   memset(&printJob, 0, sizeof(EPS_PRINT_JOB));
146   printJob.printer = (EPS_PRINTER_INN*) malloc(sizeof(EPS_PRINTER_INN));
147   memset(printJob.printer, 0, sizeof(EPS_PRINTER_INN));
148   
149   printJob.jobStatus  = EPS_STATUS_NOT_INITIALIZED;
150   printJob.pageStatus = EPS_STATUS_NOT_INITIALIZED;
151   printJob.findStatus = EPS_STATUS_NOT_INITIALIZED;
152   printJob.bComm    = TRUE; 
153   printJob.platform   = 0x04; /* '0x04 = linux' is default */ 
154
155   libStatus   = EPS_STATUS_NOT_INITIALIZED;
156   sendDataBufSize = 0;
157   sendDataBuf = NULL;
158   tmpLineBufSize = 0;
159   tmpLineBuf  = NULL;
160
161 /*** Initialize continue buffer                                                         */
162   printJob.contData.sendData = NULL;
163   printJob.contData.sendDataSize = 0;
164
165   /* DEL printJob.additional = EPS_ADDDATA_NONE;
166   printJob.qrcode.bits = NULL;
167   printJob.qrcode.cellNum = 0;
168   printJob.qrcode.dpc = 0; */
169   obsClear();
170
171 /*** Set "Endian-ness" for the current cpu                                              */
172   memInspectEndian();
173   
174 /*** Set Communication Mode                                                             */
175   //printJob.commMode = commMode;
176   
177 /*** Change ESC/P-R Lib Status                                                          */
178   libStatus = EPS_STATUS_INITIALIZED;
179   EPS_PRINTER_INN*    printer = printJob.printer;
180   printer->pmData.state = EPS_PM_STATE_NOT_FILTERED;
181   printer->language = EPS_LANG_ESCPAGE;
182   return EPS_ERR_NONE;
183 }
184
185
186 EPS_ERR_CODE epsInitVariable(){
187   sendDataBufSize = (EPS_INT32)(ESCPR_HEADER_LENGTH    +
188                   ESCPR_SEND_DATA_LENGTH +
189                   (printJob.printableAreaWidth * printJob.bpp));
190   sendDataBuf = (EPS_UINT8*)EPS_ALLOC(sendDataBufSize);
191   if(sendDataBuf == NULL){
192     sendDataBufSize = 0;
193     EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION );
194   }
195   
196   memset(sendDataBuf, 0xFF, (EPS_UINT32)sendDataBufSize);
197   /*** Allocate buffer for RLE complession                                        */
198   tmpLineBufSize = (EPS_INT32)(printJob.printableAreaWidth * printJob.bpp) + 256; /* 256 is temp buffer */
199   tmpLineBuf = (EPS_UINT8*)EPS_ALLOC(tmpLineBufSize);
200   if(tmpLineBuf == NULL){
201     tmpLineBufSize = 0;
202     EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION );
203   }
204   memset(tmpLineBuf, 0xFF, (EPS_UINT32)tmpLineBufSize);
205   return EPS_ERR_NONE;
206 }
207
208
209 EPS_ERR_CODE epsFilterEndPage(EPS_BOOL bNextPage){
210   EPS_ERR_CODE    retStatus= EPS_ERR_NONE;
211   
212 #ifdef GCOMSW_CMD_ESCPAGE_S
213     retStatus = pageEndPage();
214 #else
215     retStatus = EPS_ERR_LANGUAGE_NOT_SUPPORTED;
216 #endif
217
218   return retStatus;
219
220 }
221
222 void eps_toupper(char *str){
223   int i = 0;
224   int len;
225   if(str == NULL || strlen(str) == 0){
226     return;
227   }
228   len = strlen(str);
229   for(i = 0; i < len; i++){
230     str[i] = toupper(str[i]);
231   }
232   return;
233 }
234
235 /* Get value for PPD */
236 static char *get_default_choice (ppd_file_t *ppd_p, const char *key)
237 {
238   ppd_option_t *option;
239   ppd_choice_t *choice;
240
241   option = ppdFindOption (ppd_p, key);
242   
243   if( option == NULL && strcmp(key, "FaceUp") == 0 ) {
244      debug_msg("__ Face Down __\n");
245      return "False";
246   }
247   
248   if (!option)
249   {
250     debug_msg("option null\n");
251     return NULL;
252   }
253   
254   choice = ppdFindChoice (option, option->defchoice);
255   if (!choice)
256   {
257     debug_msg("choice null\n");
258     return NULL;
259   }
260   debug_msg("choice ok = [%s]\n", choice->choice);
261   return choice->choice;
262 }
263
264
265 int epsGetInputOption(char** in_argv,int in_argc, char *opt, char *dest)
266 {
267   int i = 0;
268   char *str = NULL;
269   char *str_tmp = NULL;
270   char dest_t[256];
271   if(opt == NULL || strlen(opt) == 0 || dest == NULL ){
272     return -1;
273   }
274   for(i = 0; i < in_argc; i++){
275     if(strcmp(opt, "RiTech") == 0)
276     {
277       str_tmp = strstr(in_argv[i], "noRiTech");
278       if(str_tmp != NULL)
279       {
280         strcpy(dest, "False");
281         return 0;
282       } 
283       else{
284         str_tmp = strstr(in_argv[i], "RiTech");
285         if(str_tmp != NULL)
286         {
287           strcpy(dest, "True");
288           return 0;
289         }
290       }
291     }
292     if(strcmp(opt, "TonerSave") == 0)
293     {
294       str_tmp = strstr(in_argv[i], "noTonerSaving");
295       if(str_tmp != NULL)
296       {
297         strcpy(dest, "False");
298         return 0;
299       } else{
300         str_tmp = strstr(in_argv[i], "TonerSaving");
301         if(str_tmp != NULL)
302         {
303           strcpy(dest, "True");
304           return 0;
305         }
306       }
307     }
308    if(strcmp(opt, "Duplex") == 0)
309     {
310       str_tmp = strstr(in_argv[i], "Duplex=DuplexNoTumble");
311       if(str_tmp != NULL)
312       {
313         strcpy(dest, "DuplexNoTumble");
314         return 0;
315       } else if(str_tmp = strstr(in_argv[i], "Duplex=DuplexTumble")){
316         if(str_tmp != NULL)
317         {
318           strcpy(dest, "DuplexTumble");
319           return 0;
320         }
321       } else {
322       strcpy(dest, "None");
323          return 0;
324     }
325     }
326   if(strcmp(opt, "Orientation") == 0)
327     {
328       str_tmp = strstr(in_argv[i], "Orientation=Portrait");
329       if(str_tmp != NULL)
330       {
331         strcpy(dest, "Portrait");
332         return 0;
333       } else if(str_tmp = strstr(in_argv[i], "Orientation=Landscape")){
334         if(str_tmp != NULL)
335         {
336           strcpy(dest, "Landscape");
337           return 0;
338         }
339       }
340     }
341     str = strstr(in_argv[i], opt);
342     if(str != NULL && (str[strlen(opt)] == '=') && strncmp(str, opt, strlen(opt)) == 0){
343       strcpy(dest_t, str + strlen(opt) + 1);
344       int j = 0;
345       while((dest_t[j] != ' ') && (j < 20))
346       {
347         dest[j] = dest_t[j];
348         j++;
349       }
350       dest[j] = '\0';
351      
352       return 0;
353     }
354   }
355
356   debug_msg("return -1 \n");
357   return -1;
358 }
359 /*tuanvd start add*/
360
361 static char *getOptionForJob(ppd_file_t *ppd_p, const char** argv, const int argc, char *key)
362 {
363   char *opt_tmp = NULL;
364   int error;
365   //char opt_t[64];
366   char *opt_t;
367   char opt_tem[256];
368
369   error = epsGetInputOption (argv, argc, key, opt_t);
370   if(error == 0){
371     opt_tmp = opt_t;
372   }
373
374   if(opt_tmp == NULL){
375     opt_tmp = get_default_choice (ppd_p, key);
376   }
377   if(strcmp(key, "Duplex") == 0)
378   {
379     debug_msg("Duplex = %s \n", opt_tmp);
380   }
381   free(opt_t);
382   return opt_tmp;
383 }
384
385 static int setupOptionforJob(const char** argv, const int argc, EPS_JOB_ATTRIB *jobAttr, const char *printer)
386 {
387   char *ppd_path;   /* Path of PPD */
388   ppd_file_t *ppd_p;  /* Struct of PPD */
389   char *opt = NULL;    /* Temporary buffer (PPD option) */
390   int i;      /* loop */
391   char *err = NULL;
392
393   jobAttr->colorPlane = EPS_CP_FULLCOLOR;
394   jobAttr->printDirection = EPS_PD_BIDIREC;
395   jobAttr->brightness = 0;
396   jobAttr->mediaSizeIdx = -1;
397   jobAttr->mediaTypeIdx = -1;
398   jobAttr->paperSource  = 10000;
399   jobAttr->duplex       = -1;
400   jobAttr->tonerSave    = -1;
401   jobAttr->RiTech       = -1;
402   jobAttr->colLate      = -1;
403   jobAttr->contrast     = 0;
404   jobAttr->orienTation  = -1;
405   jobAttr->manualFeed   = -1;
406   jobAttr->printQuality = 10000;
407
408   /* Get option from PPD. */
409   ppd_path = (char *) cupsGetPPD (printer);
410   ppd_p = ppdOpenFile (ppd_path);
411   if(NULL == ppd_p){
412     return 0;
413   }
414   /* get Color option */
415   opt = getOptionForJob(ppd_p, argv, argc, "Color");
416   if(opt != NULL){
417     debug_msg("opt check = %s \n", opt);
418     if(strcmp(opt, "MONO") == 0){
419       jobAttr->colorMode =  EPS_CM_MONOCHROME;
420     }else{
421       jobAttr->colorMode =  EPS_CM_COLOR;
422     }
423   } else{
424     jobAttr->colorMode =  EPS_CM_MONOCHROME;
425   }
426   debug_msg("debug 1\n");
427   /* get PageSize */
428   opt = getOptionForJob(ppd_p, argv, argc, "PageSize");
429   debug_msg("opt = %s\n", opt);
430   if(opt != NULL){
431     debug_msg("opt check = %s \n", opt);
432     jobAttr->mediaSizeIdx = getMediaSizeID(opt);
433     debug_msg("jobAttr->mediaSizeIdx = %d\n", jobAttr->mediaSizeIdx);
434   } else return 0;
435   debug_msg("debug 2\n");
436   /* get MediaType */
437   opt = getOptionForJob(ppd_p, argv, argc, "MediaType");
438   if(opt != NULL){
439     jobAttr->mediaTypeIdx = getMediaTypeID(opt);
440   } else return 0;
441   debug_msg("debug 3\n");
442   /* get InputSlot */
443
444   opt = getOptionForJob(ppd_p, argv, argc, "InputSlot");
445   if(opt != NULL){
446     eps_toupper(opt);
447     debug_msg("get option InputSlot \n");
448
449
450     if(strcmp(opt, "Auto") == 0){
451       jobAttr->paperSource = IPS_MPTID_AUTO;
452     }else if(strcmp(opt, "TRAY1") == 0){
453       jobAttr->paperSource = IPS_MPTID_TRAY1;
454     }else if(strcmp(opt, "TRAY2") == 0){
455       jobAttr->paperSource = IPS_MPTID_TRAY2;
456     }else if(strcmp(opt, "TRAY3") == 0){
457       jobAttr->paperSource = IPS_MPTID_TRAY3;
458     }else if(strcmp(opt, "TRAY4") == 0){
459       jobAttr->paperSource = IPS_MPTID_TRAY4;
460     }else if(strcmp(opt, "TRAY5") == 0){
461       jobAttr->paperSource = IPS_MPTID_TRAY5;
462     }else if(strcmp(opt, "TRAY6") == 0){
463       jobAttr->paperSource = IPS_MPTID_TRAY6;
464     }else if(strcmp(opt, "TRAY7") == 0){
465       jobAttr->paperSource = IPS_MPTID_TRAY7;
466     }else if(strcmp(opt, "TRAY8") == 0){
467       jobAttr->paperSource = IPS_MPTID_TRAY8;
468     }else if(strcmp(opt, "TRAY9") == 0){
469       jobAttr->paperSource = IPS_MPTID_TRAY9;
470     }else if(strcmp(opt, "TRAY10") == 0){
471       jobAttr->paperSource = IPS_MPTID_TRAY10;
472     }else if(strcmp(opt, "TRAY11") == 0){
473       jobAttr->paperSource = IPS_MPTID_TRAY11;
474     }else if(strcmp(opt, "TRAY12") == 0){
475       jobAttr->paperSource = IPS_MPTID_TRAY12;
476     }else if(strcmp(opt, "TRAY13") == 0){
477       jobAttr->paperSource = IPS_MPTID_TRAY13;
478     }else if(strcmp(opt, "TRAY14") == 0){
479       jobAttr->paperSource = IPS_MPTID_TRAY14;
480     }else if(strcmp(opt, "TRAY15") == 0){
481       jobAttr->paperSource = IPS_MPTID_TRAY15;
482     } else jobAttr->paperSource = IPS_MPTID_AUTO;
483
484
485   } else{
486     jobAttr->paperSource = IPS_MPTID_AUTO;
487   }
488
489
490   /* get Duplex */
491   opt = getOptionForJob(ppd_p, argv, argc, "Duplex");
492   if(opt != NULL){
493     if(strcmp(opt, "DuplexTumble") == 0){
494       jobAttr->duplex = EPS_DUPLEX_SHORT;
495     }else if(strcmp(opt, "DuplexNoTumble") == 0){
496       jobAttr->duplex = EPS_DUPLEX_LONG;
497     }else{
498       jobAttr->duplex = EPS_DUPLEX_NONE;
499     }
500     debug_msg("jobAttr->duplex = %d\n", jobAttr->duplex);
501   } else return 0;
502   debug_msg("debug 4\n");
503   /* get Tonersave */
504   opt = getOptionForJob(ppd_p, argv, argc, "Tonersave");
505   if(opt != NULL){
506     eps_toupper(opt);
507     if(strcmp(opt, "TRUE") == 0){
508       jobAttr->tonerSave = 1; //ON
509     }
510     if(strcmp(opt, "FALSE") == 0){
511       jobAttr->tonerSave = 0; //OFF
512     }
513   } else return 0;
514
515   /* get option RiTech */
516   opt = getOptionForJob(ppd_p, argv, argc, "RiTech");
517   if(opt != NULL){
518     eps_toupper(opt);
519     if(strcmp(opt, "TRUE") == 0){
520       jobAttr->RiTech = 1; //ON
521     }
522     if(strcmp(opt, "FALSE") == 0){
523       jobAttr->RiTech = 0; //OFF
524     }
525   } else return 0;
526   
527   /*get option Quality*/
528   opt = getOptionForJob(ppd_p, argv, argc, "Quality");
529   debug_msg("Quality: = %s\n", opt);
530   if(opt != NULL){
531     if(strcmp(opt, "300x300dpi") == 0){
532       debug_msg("__ Nam 0 __\n");
533       jobAttr->printQuality = EPS_MQID_DRAFT;
534       jobAttr->inputResolution = EPS_IR_300X300;
535       jobAttr->topMargin = 300/6;
536     }
537     if(strcmp(opt, "600x600dpi") == 0){
538       debug_msg("__ Nam 1 __\n");
539       jobAttr->printQuality = EPS_MQID_NORMAL;
540       jobAttr->inputResolution = EPS_IR_600X600;
541       jobAttr->topMargin = 600/6;
542     }
543     if (strcmp(opt, "1200x1200dpi") == 0) {
544       debug_msg("__ Nam 2 __\n");
545       jobAttr->printQuality = EPS_MQID_HIGH;
546       jobAttr->inputResolution = EPS_IR_1200X1200;
547       jobAttr->topMargin = 1200/6;
548     }
549   } else return 0;
550
551   /*get option Collate*/
552   opt = getOptionForJob(ppd_p, argv, argc, "Collate");
553   if(opt != NULL){
554     eps_toupper(opt);
555     if((strcmp(opt, "TRUE") == 0) || (strcmp(opt, "ON") == 0)){
556       jobAttr->colLate = 1; //ON
557     } else jobAttr->colLate = 0; //OFF
558   } else return 0;
559
560   /*get option Orientation*/
561   debug_msg("debug orientation \n");
562   opt = getOptionForJob(ppd_p, argv, argc, "Orientation");
563   if(opt != NULL){
564     eps_toupper(opt);
565     if(strcmp(opt, "LANDSCAPE") == 0){
566       jobAttr->orienTation = 1; //Landscape
567     } else jobAttr->orienTation = 0; //Portrai
568   } else return 0;
569   
570   /*get option ManualFeed*/
571   debug_msg("debug manual feed \n");
572   opt = getOptionForJob(ppd_p, argv, argc, "ManualFeed");
573   if(opt != NULL){
574     eps_toupper(opt);
575     if( strcmp(opt, "MANUAL") == 0 ){
576       debug_msg("check manual feed \n");
577       jobAttr->manualFeed = 1; //Manual
578     } else {
579       jobAttr->manualFeed = 0; //Automatic
580     }
581   } else return 0;
582
583   opt = getOptionForJob(ppd_p, argv, argc, "FaceUp");
584   if(opt != NULL){
585     //eps_toupper(opt);
586     if(strcmp(opt, "True") == 0){
587       jobAttr->FacingPage = 1;
588     } else{
589       jobAttr->FacingPage = 0;
590     }
591     debug_msg("jobAttr->FacingPage = %d\n", jobAttr->FacingPage);
592   }
593
594
595   /*get option copies*/
596   opt = getOptionForJob(ppd_p, argv, argc, "nCopies");
597   if(opt != NULL){
598     jobAttr->copies = atoi(opt);
599     debug_msg("jobAttr->copies = %d", jobAttr->copies);
600     if(jobAttr->copies < 1 || jobAttr->copies > 999){
601       jobAttr->copies = 1;
602     }
603   } else return 0;  
604
605   jobAttr->printLayout = EPS_MLID_BORDERS;
606   jobAttr->feedDirection = EPS_FEEDDIR_PORTRAIT;      /* paper feed direction  hardcode */
607   ppdClose (ppd_p);
608   return 1;
609 }
610 /*tuanvd end add*/
611
612
613 int main (int argc, char *argv[])
614 {
615   int fd;     /* file descriptor */
616   FILE *pfp;
617   int i;      /* loop */
618   int resul = 0;
619   cups_raster_t *ras; /* raster stream for printing */
620   cups_page_header_t header; /* page device dictionary header */
621   EPS_JOB_ATTRIB fopt;
622   
623   debug_msg("%s:%d \t<<%s>>: \t\tStart()\n", __FILE__, __LINE__, __FUNCTION__);
624
625 /* attach point */
626 #ifdef USE_DEBUGGER
627   int flag = 1;
628   while (flag) sleep (3);
629 #endif /* USE_DEBUGGER */
630
631   for (i = 0; i < argc; i++)
632     debug_msg("%s:%d \t<<%s>>: \t\t argv[%d] = [%s]\n", __FILE__, __LINE__, __FUNCTION__, i, argv[i]);
633
634   debug_msg("%s:%d \t<<%s>>: \t\tStart get cups option\n", __FILE__, __LINE__, __FUNCTION__);
635   /*get_option_for_arg(argv, argc, &fopt);
636   get_cups_option(argv[0], &fopt);  */
637   resul = setupOptionforJob(argv, argc, &fopt, argv[0]);
638   if(resul == 0){
639     debug_msg("not setup option for job \n");
640   }
641
642   /* Print start */
643   ras = cupsRasterOpen (fd, CUPS_RASTER_READ);
644   if (ras == NULL)
645   {
646     fprintf (stderr, "Can't open CUPS raster file.");
647     debug_msg("%s:%d \t<<%s>>: \t\tCan't open CUPS raster file\n", __FILE__, __LINE__, __FUNCTION__);
648     return 1;
649   }
650   if(ras){
651     do_printJob(pfp, ras, fopt);
652   }
653   debug_msg("Check start call function close rsater file \n");
654   if(ras){
655     cupsRasterClose (ras);
656   }
657   debug_msg("Check end call function close rsater file");
658   return 0;
659 }
660
661 static int  getMediaTypeID(char *rsc_name)
662 {
663   debug_msg("%s:%d \t<<%s>>: \t\tTrace in()\n", __FILE__, __LINE__, __FUNCTION__);
664   int j;
665   for(j = 0; mediaTypeData[j].value != END_ARRAY; j++){
666     if(strncmp(mediaTypeData[j].rsc_name,rsc_name, strlen(mediaTypeData[j].rsc_name)) == 0){
667       debug_msg("%s:%d \t<<%s>>: \t\tmediaTypeData[%d].rsc_name = %s, value = %d\n", __FILE__, __LINE__, __FUNCTION__, j, mediaTypeData[j].rsc_name, mediaTypeData[j].value);
668       debug_msg("%s:%d \t<<%s>>: \t\tTrace out OK [%d]\n", __FILE__, __LINE__, __FUNCTION__, mediaTypeData[j].value);
669       return mediaTypeData[j].value;
670     }}
671   debug_msg("%s:%d \t<<%s>>: \t\tTrace out Fail()\n", __FILE__, __LINE__, __FUNCTION__);
672   return 0;
673 }
674
675 static int  getMediaSizeID(char *media_name)
676 {
677   debug_msg("check media size \n");
678   debug_msg("%s:%d \t<<%s>>: \t\tTrace in()\n", __FILE__, __LINE__, __FUNCTION__);
679   int j;
680
681   for(j = 0; mediaSizeData[j].value != END_ARRAY; j++){
682     if(strncmp(mediaSizeData[j].rsc_name,media_name, strlen(mediaSizeData[j].rsc_name)) == 0){
683     eps_toupper(media_name);
684     debug_msg("media_name = %s \n", media_name);
685     if(strcmp(media_name, "USER")==0){
686       debug_msg("%s:%d \t<<%s>>: \t\tTrace out USER mode\n", __FILE__, __LINE__, __FUNCTION__);
687       return 0;
688     }
689       debug_msg("%s:%d \t<<%s>>: \t\tTrace out OK [%d]()\n", __FILE__, __LINE__, __FUNCTION__, mediaSizeData[j].value);
690       return mediaSizeData[j].value;
691     }
692   }
693   return 0;
694 }
695
696 int do_printJob(FILE* pfp,cups_raster_t *ras, EPS_JOB_ATTRIB jobAttr)
697 {
698   debug_msg("%s:%d \t<<%s>>: \tTrace in \n", __FILE__, __LINE__, __FUNCTION__);
699   cups_page_header_t header;
700   int width_pixel, height_pixel;
701   char cBuff[256];
702   int bytes_per_line;
703   int byte_par_pixel;
704   
705   double print_area_x, print_area_y;
706   char *image_raw;
707   int leftMargin, topMargin;
708
709   int read_size = 0;
710
711   int err = 0;
712   int i, j;
713   EPS_INT32 heightPixels = 0; 
714
715    int page_count = 0; 
716 #if (HAVE_PPM)
717   char ppmfilename[30];
718   FILE *fp;
719   
720 #endif
721
722   if (jobAttr.colorMode == EPS_CM_COLOR) {
723       byte_par_pixel = 3;
724   }else{ 
725       byte_par_pixel = 1;
726   }
727
728   while (cupsRasterReadHeader (ras, &header) > 0 && !cancel_flg)
729   {
730     debug_msg("%s:%d \t<<%s>>: \tStart Cupsraster read header \n", __FILE__, __LINE__, __FUNCTION__);
731     int image_bytes;
732     char *image_raw;
733     int write_size = 0;
734     char tmpbuf[256];
735     heightPixels = 0;
736     if (page_count == 0)
737     {
738       width_pixel = header.cupsWidth;
739       height_pixel = header.cupsHeight;
740       epsInitLib();
741       epsInitJob();
742       err = SetupJobAttrib(&jobAttr);
743       if (err)
744       {
745         err_fatal ("Error occurred in \"SetupJobAttrib\".");  /* exit */
746       }
747       printJob.paperWidth = width_pixel;
748       printJob.paperHeight = height_pixel;
749
750       epsInitVariable();
751       switch(printJob.attr.inputResolution)
752       {
753       case EPS_IR_300X300:
754         printJob.topMargin      = 50;
755           printJob.leftMargin     = 50;
756         break;
757         case EPS_IR_600X600:
758           printJob.topMargin      = 100;
759           printJob.leftMargin     = 100;
760         break;
761         default:
762           printJob.topMargin      = 200;
763           printJob.leftMargin     = 200;
764         break;
765       }
766
767       leftMargin = printJob.leftMargin;
768       topMargin = printJob.topMargin;
769   
770       printJob.printableAreaHeight  = (EPS_UINT32)height_pixel;// - (printJob.topMargin + printJob.leftMargin);
771         printJob.printableAreaWidth = (EPS_UINT32)width_pixel;// - (printJob.topMargin + printJob.leftMargin);
772       err = pageAllocBuffer();
773       if(err)
774       {
775         debug_msg("%s:%d \t<<%s>>: \t\t Error occurred in \"pageAllocBuffer\".", __FILE__, __LINE__, __FUNCTION__); /* exit */
776         err_fatal ("Error occurred in \"pageAllocBuffer\"."); /* exit */
777       }else
778       {
779         debug_msg("%s:%d \t<<%s>>: \t\t pageAllocBuffer() success\n", __FILE__, __LINE__, __FUNCTION__);
780       }
781
782       EPS_PRINTER_INN curPrinter;
783       memset(&curPrinter, 0, sizeof(curPrinter));
784       if(jobAttr.colorMode == EPS_CM_COLOR){
785         curPrinter.language = EPS_LANG_ESCPAGE_COLOR;
786       }else{
787         curPrinter.language = EPS_LANG_ESCPAGE;
788       }
789   
790       printJob.printer = &curPrinter;
791       err = pageStartJob();
792       if (err)
793       {
794         err_fatal ("Error occurred in \"pageStartJob\".");  /* exit */
795       }else
796       {
797         debug_msg("%s:%d \t<<%s>>: \t\t pageStartJob() success",  __FILE__, __LINE__, __FUNCTION__);
798       }
799     }
800     err = pageStartPage();
801     if (err)
802     {
803       debug_msg("%s:%d \t<<%s>>: \t\t Error occurred in \"pageStartPage\": %d\n", __FILE__, __LINE__, __FUNCTION__, err);
804       err_fatal ("Error occurred in \"pageStartPage\".");  /* exit */
805     }else
806     {
807       debug_msg("%s:%d \t<<%s>>: \t\t pageStartPage() success",  __FILE__, __LINE__, __FUNCTION__);
808     }
809     printJob.verticalOffset = 0;
810     print_area_x = printJob.printableAreaWidth;
811     print_area_y = printJob.printableAreaHeight;
812   
813     bytes_per_line = (width_pixel * byte_par_pixel);
814   
815     int band_out_len = printJob.printableAreaWidth*byte_par_pixel;
816     //band_out_len += band_out_len % 2;
817     band_out_len = ((band_out_len +7)/8)*8;
818     char *bandPrint = mem_new0(char, band_out_len*band_line);
819   
820     image_raw = mem_new0 (char, bytes_per_line);
821   
822     char *grayBuff;
823     if(byte_par_pixel == 1)
824     {
825       grayBuff = mem_new0 (char, band_out_len*band_line*3);
826     }
827   
828     char *pPrint;
829
830     long int total_bytes = 0;
831     read_size = 0;
832     image_bytes = WIDTH_BYTES(header.cupsBytesPerLine)*8;
833     #if (HAVE_PPM)
834       
835     sprintf(ppmfilename, "/tmp/al-m2010-page%d.ppm", page_count);
836     fp = fopen(ppmfilename, "w");
837     if(jobAttr.colorMode == EPS_CM_COLOR){
838       fprintf(fp, "P3\n");
839     }else{
840       fprintf(fp, "P2\n");
841     }
842     fprintf(fp, "%u\n",(int)printJob.printableAreaWidth);
843     fprintf(fp, "%u\n",(int)header.cupsHeight);
844     fprintf(fp, "255\n");
845     fclose(fp);
846     
847     #endif  
848     page_count++;
849     debug_msg("%s:%d \t<<%s>>: \t\timage_bytes = %d\n", __FILE__, __LINE__, __FUNCTION__, image_bytes);
850     image_raw = (char *)calloc (sizeof (char), image_bytes);
851     int i = 0;
852     int y_count = 0;
853     debug_msg("duplex = %d, page_count = %d\n", jobAttr.duplex, page_count);
854     
855     for (i = 0; i < header.cupsHeight && !cancel_flg; i ++)
856       {
857         debug_msg("%s:%d \t<<%s>>: \tStart print line [%d]\n", __FILE__, __LINE__, __FUNCTION__, i);
858         int numread = cupsRasterReadPixels (ras, (unsigned char*)image_raw, header.cupsBytesPerLine);
859         if (!numread)
860         {
861           fprintf (stderr, "cupsRasterReadPixels");
862           debug_msg("%s:%d \t<<%s>>: \t\tcupsRasterReadPixels error\n", __FILE__, __LINE__, __FUNCTION__);
863           return 1;
864         }
865         debug_msg("%s:%d \t<<%s>>: \t numread = %d\n", __FILE__, __LINE__, __FUNCTION__, numread);
866         memcpy (bandPrint + (heightPixels%band_line) * band_out_len, image_raw, band_out_len );
867         heightPixels ++;
868         //y_count++;
869         debug_msg("header.cupsBytesPerLine = %d\n", header.cupsBytesPerLine);
870         if (i < printJob.paperHeight)
871         {
872           if(byte_par_pixel == 1)
873           {
874             pPrint = grayBuff;
875             int j = 0;
876             for(j = 0; j < band_out_len*band_line; j++)
877             {
878               grayBuff[j*3] = bandPrint[j];
879               grayBuff[j*3+1] = bandPrint[j];
880               grayBuff[j*3+2] = bandPrint[j];
881             }
882             debug_msg("byte_par_pixel = %d\n", byte_par_pixel);
883             err = PrintBand (grayBuff, band_out_len*band_line*3, &heightPixels);
884             #if (HAVE_PPM)
885             int k = 0;
886             fp = fopen(ppmfilename, "a+");
887             for(k=0; k<(int)header.cupsBytesPerLine; k++)
888             {   
889               fprintf(fp, "%u ", (unsigned char)bandPrint[k]);
890             }
891             fprintf(fp, "\n");
892             fclose(fp);
893             #endif
894           }else
895           {
896             debug_msg("byte_par_pixel = %d\n", byte_par_pixel);
897             err = PrintBand (bandPrint, band_out_len, &heightPixels);
898             #if (HAVE_PPM)
899             int k = 0;
900             fp = fopen(ppmfilename, "a+");
901             for(k=0; k<(int)header.cupsBytesPerLine; k++)
902             {   
903               fprintf(fp, "%u ", (unsigned char)bandPrint[k]);
904             }
905             fprintf(fp, "\n");
906             fclose(fp);
907             #endif
908           }
909           heightPixels = 0;
910           //err = PrintBand (image_raw, band_out_len, &heightPixels);
911           if (err)
912           {
913             if(err == EPS_OUT_OF_BOUNDS)
914             {
915               debug_msg("EPS_OUT_OF_BOUNDS\n");
916               //break;
917             }
918             debug_msg("PrintBand error: %d\n", err);
919             err_fatal ("Error occurred in \"OUT_FUNC\"."); /* exit */
920           }else
921           {
922             debug_msg("PrintBand success\n");
923           }
924           debug_msg("%s:%d \t<<%s>>: \tCompleted print line [%d]\n", __FILE__, __LINE__, __FUNCTION__, i);
925         }
926       }
927     debug_msg("pageEndPage(%d) call\n", page_count);
928     err = pageEndPage();
929     if (err)
930     {
931       debug_msg("pageEndPage() error: %d\n", err);
932       err_fatal ("Error occurred in \"PEND_FUNC\"."); /* exit */
933     }else
934     {
935       debug_msg("pageEndPage() success\n");
936     }
937     free (image_raw);
938     free(bandPrint);
939     bandPrint = NULL;
940   }
941   
942   err = pageEndJob();
943   if(err){
944     err_fatal ("Error occurred in \"pageEndJob\"."); /* exit */
945   }else{
946     debug_msg("pageEndJob() success\n");
947   }
948   return 0;
949 }