Tizen 2.1 base
[platform/upstream/epson-inkjet-printer-escpr.git] / src / filter.c
1 /*
2  * Epson Inkjet Printer Driver (ESC/P-R) for Linux
3  * Copyright (C) 2002-2005 AVASYS CORPORATION.
4  * Copyright (C) Seiko Epson Corporation 2002-2012.
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
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24 #define HAVE_PPM (0)
25
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33
34
35
36 #include "epson-escpr-def.h"
37 #include "epson-escpr-media.h"
38 #include "epson-protocol.h"
39 #include "epson-escpr-pvt.h"
40 #include "epson-escpr-api.h"
41 #include "epson-escpr-services.h"
42 #include "epson-escpr-mem.h"
43
44 #include "err.h"
45 #include "mem.h"
46 #include "str.h"
47 #include "csv.h"
48 #include "debug.h"
49 #include "libprtX.h"
50 #include "optBase.h"
51 #include "linux_cmn.h"
52
53
54
55 #define PATH_MAX 256
56 #define NAME_MAX 41
57
58 #define WIDTH_BYTES(bits) (((bits) + 31) / 32 * 4)
59
60 #define PIPSLITE_FILTER_VERSION "* epson-escpr is a part of " PACKAGE_STRING
61
62 #define PIPSLITE_FILTER_USAGE "Usage: $ epson-escpr model width_pixel height_pixel Ink PageSize Quality"
63
64 typedef struct rtp_filter_option {
65         char model[NAME_MAX];
66         char model_low[NAME_MAX];
67         char ink[NAME_MAX];
68         char media[NAME_MAX];
69         char quality[NAME_MAX];
70         char duplex[NAME_MAX];
71 } filter_option_t;
72
73
74 FILE* outfp = NULL;
75
76 /* static functions */
77 static int set_pips_parameter (filter_option_t *, ESCPR_OPT *, ESCPR_PRINT_QUALITY *);
78 static int get_page_size (const char *, const char *);
79 static int getMediaTypeID(char *);
80 static ESCPR_BYTE4 print_spool_fnc(void* , const ESCPR_UBYTE1*, ESCPR_UBYTE4);
81
82 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
83 const int band_line = 1;
84 extern EPS_INT32    libStatus;                  /*  Library (epsInitDriver) status      */
85 extern EPS_PRINT_JOB   printJob;
86 extern EPS_UINT32   sendDataBufSize;
87 extern EPS_UINT8*   sendDataBuf;    /* buffer of SendCommand(save) input                */
88 extern EPS_UINT32   tmpLineBufSize;
89 extern EPS_UINT8*   tmpLineBuf;
90
91 extern EPS_CMN_FUNC epsCmnFnc;
92 EPS_JOB_FUNCS           jobFnc;
93
94 extern EPS_INT32 tonerSave;
95 extern EPS_INT32 back_type;
96 extern EPS_INT32 lWidth;
97 extern EPS_INT32 lHeight;
98 extern EPS_INT32 areaWidth;
99 extern EPS_INT32 areaHeight;
100
101 #ifndef ESCPR_HEADER_LENGTH     
102 #define ESCPR_HEADER_LENGTH            10    /* ESC + CLASS + ParamLen + CmdName */     
103 #endif
104
105 #ifndef ESCPR_SEND_DATA_LENGTH
106 #define ESCPR_SEND_DATA_LENGTH          7
107 #endif
108
109 EPS_JOB_ATTRIB     jobAttr;
110
111 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
112
113
114
115
116 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
117 EPS_ERR_CODE epsInitLib(){
118         EPS_CMN_FUNC cmnFuncPtrs;
119         memset(&cmnFuncPtrs, 0, sizeof(EPS_CMN_FUNC));
120         
121         cmnFuncPtrs.version = EPS_CMNFUNC_VER_CUR;
122         cmnFuncPtrs.findCallback = NULL;
123         cmnFuncPtrs.memAlloc = &epsmpMemAlloc;
124         cmnFuncPtrs.memFree = &epsmpMemFree;
125         cmnFuncPtrs.sleep = &epsmpSleep;
126         cmnFuncPtrs.getTime = &epsmpGetTime;
127         cmnFuncPtrs.getLocalTime = &epsmpGetLocalTime;
128         cmnFuncPtrs.lockSync = &epsmpLockSync;
129         cmnFuncPtrs.unlockSync = &epsmpUnlockSync;
130         cmnFuncPtrs.stateCallback = NULL; /* current version unused */
131
132         memcpy((void*)(&epsCmnFnc), (void*)&cmnFuncPtrs, sizeof(EPS_CMN_FUNC));
133 }
134
135 EPS_ERR_CODE epsInitJob(){
136         
137         memset(&printJob, 0, sizeof(EPS_PRINT_JOB));
138         printJob.printer = (EPS_PRINTER_INN*) malloc(sizeof(EPS_PRINTER_INN));
139         memset(printJob.printer, 0, sizeof(EPS_PRINTER_INN));
140         
141         printJob.jobStatus  = EPS_STATUS_NOT_INITIALIZED;
142         printJob.pageStatus = EPS_STATUS_NOT_INITIALIZED;
143         printJob.findStatus = EPS_STATUS_NOT_INITIALIZED;
144         printJob.bComm          = TRUE; 
145         printJob.platform   = 0x04; /* '0x04 = linux' is default */ 
146
147         libStatus   = EPS_STATUS_NOT_INITIALIZED;
148         sendDataBufSize = 0;
149         sendDataBuf = NULL;
150         tmpLineBufSize = 0;
151         tmpLineBuf  = NULL;
152
153 /*** Initialize continue buffer                                                         */
154         printJob.contData.sendData = NULL;
155         printJob.contData.sendDataSize = 0;
156
157         /* DEL printJob.additional = EPS_ADDDATA_NONE;
158         printJob.qrcode.bits = NULL;
159         printJob.qrcode.cellNum = 0;
160         printJob.qrcode.dpc = 0; */
161         obsClear();
162
163 /*** Set "Endian-ness" for the current cpu                                              */
164         memInspectEndian();
165         
166 /*** Set Communication Mode                                                             */
167         //printJob.commMode = commMode;
168         
169 /*** Change ESC/P-R Lib Status                                                          */
170         libStatus = EPS_STATUS_INITIALIZED;
171         EPS_PRINTER_INN*    printer = printJob.printer;
172         printer->pmData.state = EPS_PM_STATE_NOT_FILTERED;
173         printer->language = EPS_LANG_ESCPR;
174         return EPS_ERR_NONE;
175 }
176
177
178 EPS_ERR_CODE epsInitVariable(){
179         sendDataBufSize = (EPS_INT32)(ESCPR_HEADER_LENGTH    +
180                                                                         ESCPR_SEND_DATA_LENGTH +
181                                                                         (printJob.printableAreaWidth * printJob.bpp));
182         sendDataBuf = (EPS_UINT8*)EPS_ALLOC(sendDataBufSize);
183         if(sendDataBuf == NULL){
184                 sendDataBufSize = 0;
185                 EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION );
186         }
187         
188         memset(sendDataBuf, 0xFF, (EPS_UINT32)sendDataBufSize);
189         /*** Allocate buffer for RLE complession                                        */
190         tmpLineBufSize = (EPS_INT32)(printJob.printableAreaWidth * printJob.bpp) + 256; /* 256 is temp buffer */
191         tmpLineBuf = (EPS_UINT8*)EPS_ALLOC(tmpLineBufSize);
192         if(tmpLineBuf == NULL){
193                 tmpLineBufSize = 0;
194                 EPS_RETURN( EPS_ERR_MEMORY_ALLOCATION );
195         }
196         memset(tmpLineBuf, 0xFF, (EPS_UINT32)tmpLineBufSize);
197         return EPS_ERR_NONE;
198 }
199
200
201 EPS_ERR_CODE epsFilterEndPage(EPS_BOOL bNextPage){
202         EPS_ERR_CODE    retStatus= EPS_ERR_NONE;
203         
204 #ifdef GCOMSW_CMD_ESCPAGE_S
205         debug_msg("GCOMSW_CMD_ESCPAGE defined\n");
206                 retStatus = pageEndPage();
207 #else
208                 retStatus = EPS_ERR_LANGUAGE_NOT_SUPPORTED;
209 #endif
210
211         return retStatus;
212
213 }
214
215 void eps_toupper(char *str){
216         int i = 0;
217         int len;
218         if(str == NULL || strlen(str) == 0){
219                 return;
220         }
221         len = strlen(str);
222         for(i = 0; i < len; i++){
223                 str[i] = toupper(str[i]);
224         }
225         return;
226 }
227
228 static int  getMediaSizeID(char *media_name){
229   int j;
230         debug_msg("media name = %s\n", media_name);
231   for(j = 0; mediaSizeData[j].value != END_ARRAY; j++){
232     if((strlen(mediaSizeData[j].rsc_name) == strlen(media_name)) && strncmp(mediaSizeData[j].rsc_name,media_name, strlen(mediaSizeData[j].rsc_name)) == 0){
233         debug_msg("mediaSizeData[%d].rsc_name: %s, x_name = %s\n", j, mediaSizeData[j].rsc_name, mediaSizeData[j].x_name);
234       return mediaSizeData[j].value;
235     }
236   }
237   return 0;
238 }
239
240 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
241
242
243 /* Useage: epson-escpr model width_pixel height_pixel Ink PageSize Quality */
244 int
245 main (int argc, char *argv[])
246 {
247         filter_option_t fopt;
248         char libfile[PATH_MAX + 1]; 
249
250         long width_pixel, height_pixel;
251         long HWResolution;
252         long bytes_per_line;
253         int byte_par_pixel;
254         double x_mag, y_mag;
255         double print_area_x, print_area_y;
256         char *image_raw;
257         unsigned char *band;
258
259         long read_size = 0;
260         int band_line;
261
262         int err = 0;
263         int i, j;
264
265         /* 2004.04.15 added for 'cancel page' */
266         int cancel = 0;
267         
268         /* 2005.11.28 added  */
269         char *paper;
270         char *bin_id;
271         char *point;
272
273         /* library options */
274         ESCPR_OPT printOpt;
275         ESCPR_PRINT_QUALITY printQuality;
276         //ESCPR_PRINT_JOB printJob;
277         ESCPR_BANDBMP bandBmp;
278         ESCPR_RECT bandRect;
279         
280 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
281         memset (&jobAttr, 0, sizeof(jobAttr));
282 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
283
284 /* attach point */
285 #ifdef USE_DEBUGGER
286         int flag = 1;
287         while (flag) sleep (3);
288 #endif /* USE_DEBUGGER */
289
290         DEBUG_START;
291         err_init (argv[0]);
292
293         if (argc != 9)
294         {
295                 for ( i = 1; i < argc; i++ ) {
296                         if ( (0 == strncmp(argv[i], "-v", (strlen("-v")+1)) )
297                                 || (0 == strncmp(argv[i], "--version", (strlen("--version")+1)) )
298                                 ) {
299                                 fprintf(stderr, "%s\n", PIPSLITE_FILTER_VERSION);
300                                 return 0;
301                         } else if ( (0 == strncmp(argv[i], "-u", (strlen("-u")+1)) )
302                                 || (0 == strncmp(argv[i], "--usage", (strlen("--usage")+1)) )
303                                 ) {
304                                 fprintf(stderr, "%s\n", PIPSLITE_FILTER_USAGE);
305                                 return 0;
306                         } else if ( (0 == strncmp(argv[i], "-h", (strlen("-h")+1)) )
307                                 || (0 == strncmp(argv[i], "--help", (strlen("--help")+1)) )
308                                 ) {
309                                 fprintf(stderr, "%s\n%s\n", PIPSLITE_FILTER_VERSION, PIPSLITE_FILTER_USAGE);
310                                 return 0;
311                         }
312                 }
313                 fprintf (stderr, "%s\n", PIPSLITE_FILTER_USAGE);
314                 return 1;
315         }
316
317         /* set filter options */
318         memset (&fopt, 0, sizeof (filter_option_t));
319         memset (&printOpt, 0, sizeof (ESCPR_OPT));
320         memset (&printQuality, 0, sizeof(ESCPR_PRINT_QUALITY));
321         //memset (&printJob, 0, sizeof(ESCPR_PRINT_JOB));
322         memset (&bandBmp, 0, sizeof(ESCPR_BANDBMP));
323         memset (&bandRect, 0, sizeof(ESCPR_RECT));
324
325         strncpy (fopt.model, argv[1], NAME_MAX);
326         for (i = 0; fopt.model[i] != '\0' && i < NAME_MAX - 1; i ++)
327                 fopt.model_low[i] = tolower (fopt.model[i]);
328         fopt.model_low[i] = '\0';
329
330         width_pixel = atol (argv[2]);
331         height_pixel = atol (argv[3]);
332         HWResolution = atol (argv[4]);
333
334         strncpy (fopt.ink, argv[5], NAME_MAX);
335         strncpy (fopt.media, argv[6], NAME_MAX);
336         strncpy (fopt.quality, argv[7], NAME_MAX);
337         strncpy (fopt.duplex, argv[8], NAME_MAX);
338         
339         debug_msg("all para\n");
340         for(i = 0; i< argc; i++){
341                 debug_msg("argv[%d] = %s\n", i, argv[i]);
342         }
343
344         outfp = stdout;
345         printOpt.fpspoolfunc = (ESCPR_FPSPOOLFUNC)print_spool_fnc;
346         
347         if (set_pips_parameter (&fopt, &printOpt, &printQuality))
348                 err_fatal ("Cannot get option of PIPS."); /* exit */
349         debug_msg("after setpip, duplex = %d\n", jobAttr.duplex);
350         point = fopt.media;
351         if(point[0]=='T')
352         {
353                 paper=str_clone(++point,strlen(fopt.media)-1);
354                 bin_id="BORDERLESS";
355                 jobAttr.printLayout = EPS_MLID_BORDERLESS;
356                 debug_msg("borderless\n");
357         }
358         else
359         {
360                 paper=str_clone(point,strlen(fopt.media));
361                 bin_id="BORDER";
362                 jobAttr.printLayout = EPS_MLID_BORDERS;
363                 debug_msg("border\n");
364         }
365         
366         eps_toupper(fopt.media);
367         //jobAttr.mediaSizeIdx = getMediaSizeID(fopt.media);
368         jobAttr.mediaSizeIdx = getMediaSizeID(paper);
369
370         switch(HWResolution){
371         case 360:
372                 //printJob.InResolution = ESCPR_IR_3636;
373                 jobAttr.inputResolution = EPS_IR_360X360;
374                 break;
375         case 720:
376                 //printJob.InResolution = ESCPR_IR_7272;
377                 jobAttr.inputResolution = EPS_IR_720X720;
378                 break;
379         case 300:
380                 //printJob.InResolution = ESCPR_IR_3030;
381                 jobAttr.inputResolution = EPS_IR_300X300;
382                 break;
383         case 600:
384                 //printJob.InResolution = ESCPR_IR_6060;
385                 jobAttr.inputResolution = EPS_IR_600X600;
386                 break;                                                  
387         }
388
389         
390         //if (get_page_size (&printJob, paper, bin_id))
391         if (get_page_size (paper, bin_id))
392                 err_fatal ("Cannot get page size of PIPS.");
393
394         band_line = 1;
395
396         if (strcmp (fopt.ink, "COLOR") == 0)
397                 byte_par_pixel = 3;
398         else
399                 byte_par_pixel = 1;
400         
401 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
402         epsInitLib();
403         epsInitJob();
404         debug_msg("before setupjobattr, duplex = %d\n", jobAttr.duplex);
405         debug_msg("mediasizeIDx = %d\n",        jobAttr.mediaSizeIdx);
406         err = SetupJobAttrib(&jobAttr);
407         if (err){
408                 debug_msg("Error occurred in \"SetupJobAttrib\": %d\n", err);
409                 err_fatal ("Error occurred in \"SetupJobAttrib\".");    /* exit */
410         }
411         //printJob.paperWidth = width_pixel;
412         //printJob.paperHeight = height_pixel;
413
414         epsInitVariable();
415         
416         debug_msg("begin pageAllocBuffer()\n");
417         err = pageAllocBuffer();
418         if(err){
419                 debug_msg ("Error occurred in \"pageAllocBuffer\".");   /* exit */
420                 err_fatal ("Error occurred in \"pageAllocBuffer\".");   /* exit */
421         }else{
422                 debug_msg ("pageAllocBuffer() success\n");
423         }
424
425
426         EPS_PRINTER_INN curPrinter;
427         memset(&curPrinter, 0, sizeof(curPrinter));
428         curPrinter.language = EPS_LANG_ESCPR;
429         curPrinter.protocol = EPS_PROTOCOL_LPR;
430
431         printJob.printer = &curPrinter;
432
433         prtSetupJobFunctions(printJob.printer, &jobFnc);
434         debug_msg("call SendStartJob function\n");
435
436         err = SendStartJob(FALSE);
437         if (err){
438                 debug_msg("Error occurred in \"pageStartJob\": %d\n", err);
439                 err_fatal ("Error occurred in \"pageStartJob\".");      /* exit */
440         }else{
441                 debug_msg("pageStartJob() success");
442         }
443         
444         printJob.jobStatus = EPS_STATUS_ESTABLISHED;
445         int printHeight = 0;
446 ///////////////////////////////////////////////////////////////////////////////////////////////////////////     
447
448
449
450
451         print_area_x = printJob.printableAreaWidth;
452         print_area_y = printJob.printableAreaHeight;
453
454         /* setup band struct */
455         bandBmp.WidthBytes = WIDTH_BYTES (print_area_x * 3 * 8 );
456
457         band = (unsigned char *)mem_new (char, bandBmp.WidthBytes * band_line);
458         memset (band, 0xff, bandBmp.WidthBytes * band_line);
459         bandBmp.Bits = band;
460
461         bandRect.left = 0;
462         bandRect.right = printJob.printableAreaWidth;
463         bandRect.top = 0;
464         bandRect.bottom = 0;
465
466         /* debug */
467         DEBUG_JOB_STRUCT (printJob);
468         DEBUG_QUALITY_STRUCT (printQuality);
469 #if 0
470         err = escprInitJob (&printOpt, &printQuality, &printJob);
471         if (err)
472                 err_fatal ("Error occurred in \"INIT_FUNC\"."); /* exit */
473 #endif  
474
475         x_mag = (double)print_area_x / width_pixel;
476         y_mag = (double)print_area_y / height_pixel;
477         band_line = 1;
478         bytes_per_line = WIDTH_BYTES(width_pixel * byte_par_pixel * 8 );
479         debug_msg("bytes_per_line = %d\n", bytes_per_line);
480         image_raw = mem_new0 (char, bytes_per_line);
481         //epsStartJob(&jobAttr);
482         int page_count = 0;
483         while ((read_size = read (STDIN_FILENO, image_raw, bytes_per_line)) > 0)
484         {
485                 debug_msg("step 100.0\n");
486                 long x_count, y_count;
487                 int band_line_count;
488 #if (HAVE_PPM)
489                 char ppmfilename[30];
490                 FILE *fp;
491 #endif
492                 y_count = 0;
493                 band_line_count = 0;
494                 bandRect.top = 0;
495
496                 //err = escprInitPage ();
497                 err = epsStartPage(NULL);
498 #if (HAVE_PPM)
499                 sprintf(ppmfilename, "/tmp/px-b750f-page%d.ppm", page_count);
500                 fp = fopen(ppmfilename, "w");
501                 fprintf(fp, "P3\n");
502                 fprintf(fp, "%d\n",bytes_per_line/byte_par_pixel);
503                 fprintf(fp, "%d\n",(int)print_area_y);
504                 fprintf(fp, "255\n");
505                 fclose(fp);
506 #endif
507                 page_count++;
508                 if (err)
509                         err_fatal ("Error occurred in \"PINIT_FUNC\"."); /* exit */
510                 debug_msg("step 100.1\n");
511                 debug_msg("ppm image width = %d; height = %d\n", bytes_per_line/3, (int)print_area_y);
512                 printJob.verticalOffset = 0;
513                 debug_msg("start page %d ======================\n", page_count);
514                 if (jobAttr.duplex ==  EPS_DUPLEX_SHORT && ((page_count % 2) == 0))
515                 {
516                         char* pagebuf = malloc((int)(print_area_y) * (bandBmp.WidthBytes));
517                         char* startpage = pagebuf;
518                         long int posbuf = 0;
519                         //debug_msg("height pixel = %d\n", height_pixel);
520                         //debug_msg("height pixel [%d]* byte_per_line [%d]=%d// Widthbyte = %d\n", (int)height_pixel, bytes_per_line, (int)(height_pixel)*(bytes_per_line), bandBmp.WidthBytes);
521                         for (i = 0; i < print_area_y; i ++)
522                         {
523                                 char *line;
524                                 //debug_msg("step 10\n");
525                                 line = (char *)(band + (bandBmp.WidthBytes * band_line_count));
526                                 while ((0 == y_count) || ((i > (y_mag * y_count) - 1) && (y_count < height_pixel))) {
527                                         while ((0 == err) && (read_size < bytes_per_line)) {
528                                                 size_t rsize;
529                                                 rsize = read(STDIN_FILENO, image_raw + read_size, bytes_per_line - read_size);
530                                                 //debug_msg("step 11\n");
531                                                 if (rsize <= 0) {
532                                                         if ((errno != EINTR) && (errno != EAGAIN) && (errno != EIO)) {
533                                                                 err = -1;
534                                                                 cancel = 1;
535                                                                 goto quit;
536                                                                 /* not reached          */
537                                                                 break;
538                                                         }
539                                                         usleep(50000);
540                                                 } else {
541                                                         read_size += rsize;
542                                                 }
543                                         }
544                                         if( read_size == bytes_per_line ){
545                                                 y_count++;
546                                                 read_size = 0;
547                                         }
548                                 }
549                                 read_size = 0;
550                                 /* convert input raster data to output byte data. */
551                                 int copybyte = 1;
552                                 if ( 1 == byte_par_pixel )
553                                         copybyte = 3;
554                                 if (x_mag == 1)
555                                 {
556                                         //debug_msg("step 12\n");
557                                         if ( 1 == byte_par_pixel )
558                                         {
559                                                 int k;
560                                                 for ( j = 0; j < print_area_x; j++ )
561                                                 {
562                                                         for ( k = 0; k < copybyte ; k++ )
563                                                                 line[j * 3 + k] = image_raw[j];
564                                                 }
565                                         }
566                                         else
567                                                 memcpy (line, image_raw, bytes_per_line);
568                                 }
569                                 else
570                                 {
571                                         //debug_msg("step 13\n");
572                                         x_count = 0;
573                                         for (j = 0; j < print_area_x; j ++)
574                                         {
575                                                 int k;
576                                                 while ( x_count == 0 || j > x_mag * x_count)
577                                                         x_count ++;
578                                                 for ( k = 0; k < copybyte ; k++ )
579                                                         memcpy (line + (j * 3 ) + k,
580                                                         image_raw + ((x_count - 1) * byte_par_pixel),
581                                                         byte_par_pixel);
582                                         }
583                                 }
584                                 //debug_msg("step 14\n");
585                                 band_line_count ++;
586                                 if (band_line_count >= band_line)
587                                 {
588                                         bandRect.bottom = bandRect.top + band_line_count;
589                                         printHeight = band_line_count;
590                                         printHeight = 1;
591                                         memcpy(pagebuf, bandBmp.Bits, bandBmp.WidthBytes);
592 #if (HAVE_PPM)
593                                         fp = fopen(ppmfilename, "a+");
594                                         int i=0;        
595                                         for(i=0; i<bytes_per_line/byte_par_pixel*3; i++){               
596                                                 fprintf(fp, "%u ", (unsigned char)pagebuf[i]);
597                                         }
598                                         fprintf(fp, "\n");
599                                         fclose(fp);
600 #endif
601                                         pagebuf+= bandBmp.WidthBytes;
602                                         posbuf+=bandBmp.WidthBytes;
603                                         //D_PrintBand (bandBmp.Bits, bandBmp.WidthBytes, &printHeight);
604                                         band_line_count -= printHeight;
605                                         bandBmp.Bits += band_line_count;
606                                         
607                                         bandRect.top = bandRect.bottom;
608                                 }
609                         }
610                         //debug_msg("step 15\n");
611                         if (band_line_count > 0)
612                         {
613                                 bandRect.bottom = bandRect.top + band_line_count;
614                                 printHeight = 1;
615                                 memcpy(pagebuf, bandBmp.Bits, bandBmp.WidthBytes);
616 #if (HAVE_PPM)
617                                 fp = fopen(ppmfilename, "a+");
618                                 int i=0;        
619                                 for(i=0; i<bytes_per_line/byte_par_pixel*3; i++){               
620                                         fprintf(fp, "%u ", (unsigned char)pagebuf[i]);
621                                 }
622                                 fprintf(fp, "\n");
623                                 fclose(fp);
624 #endif
625                                 pagebuf+= bandBmp.WidthBytes;
626                                 posbuf+= bandBmp.WidthBytes;
627                                 band_line_count -= printHeight;
628                                 bandBmp.Bits += band_line_count;
629                         }
630                         //debug_msg("band line count = %d\n", band_line_count);
631                         //debug_msg("width byte = %d\n", bandBmp.WidthBytes);
632                         //debug_msg("posbuf = %d\n", posbuf);
633                 
634                         int revert = 0;
635                         int pos = posbuf - bandBmp.WidthBytes ;
636                         int revert_line;
637                         char *rever_buf = malloc(bandBmp.WidthBytes);
638                         //debug_msg("byte_par_pixel = %d\n", byte_par_pixel);
639                         for (revert = print_area_y; revert > 0; revert--)
640                         {
641                                 if (3 != byte_par_pixel)
642                                 {
643                                         int k = 0;
644                                         for (k = bandBmp.WidthBytes/3; k >= 0; k--)
645                                         {
646                                                 memcpy(rever_buf + k*3, startpage + pos + (bandBmp.WidthBytes) - k*3, 3);
647                                         }
648                                 }
649                                 else
650                                 {
651                                         int k = 0;
652                                         for (k = bandBmp.WidthBytes/3; k >= 0; k--)
653                                         {
654                                                 memcpy(rever_buf + k*3, startpage + pos + (bandBmp.WidthBytes - 6) - k*3, 3);
655 //                                              if (k == 0)
656 //                                              {
657 //                                                      memcpy(rever_buf + k*3, startpage + pos + bandBmp.WidthBytes + 3, 3);
658 //                                              }
659                                         }
660                                         //debug_msg("k = %d\n", k);
661                                 }
662                                 //debug_msg("step 100\n");
663                                 PrintBand (rever_buf, bandBmp.WidthBytes, &printHeight);
664                                 pos -= bandBmp.WidthBytes;
665                                 //debug_msg("printHeight = %d\n", printHeight);
666                                 //debug_msg("WidthByte = %d\n", bandBmp.WidthBytes);
667                         }
668                         debug_msg("free rever\n");
669                         if (rever_buf != NULL)
670                         {
671                                 free(rever_buf);
672                                 rever_buf = NULL;
673                         }
674                         debug_msg("free page\n");
675                         if (pagebuf != NULL)
676                         {
677                                 pagebuf = startpage;
678                                 free (pagebuf);
679                                 pagebuf = NULL;
680                         }
681                         debug_msg("free page sucessfull\n");
682                         err = epsEndPage(FALSE);
683
684                         if(err)
685                                 err_fatal ("Error occurred in \"PEND_FUNC\"."); /* exit */
686                 }
687                 else
688                 {
689                         for (i = 0; i < print_area_y; i ++)
690                         {
691                                 char *line;
692         
693                                 line = (char *)(band + (bandBmp.WidthBytes * band_line_count));
694                                 while ((0 == y_count) || ((i > (y_mag * y_count) - 1) && (y_count < height_pixel))) {
695                                         while ((0 == err) && (read_size < bytes_per_line)) {
696                                                 size_t rsize;
697                                                 
698                                                 rsize = read(STDIN_FILENO, image_raw + read_size, bytes_per_line - read_size);
699                                                 /* 2009.03.17 epson-escpr-1.0.0 */
700                                                 /* if user cancels job from CUPS Web Admin, */
701                                                 /* epson-escpr-wrapper exit normally, */
702                                                 /* and read() return rsize = 0 */
703                                                 if (rsize <= 0) {
704                                                         if ((errno != EINTR) && (errno != EAGAIN) && (errno != EIO)) {
705                                                                 err = -1;
706                                                                 /* 2004.04.15           */
707                                                                 /* error then quit      */ 
708                                                                 /* don't care err = -1  */
709                                                                 cancel = 1;
710                                                                 goto quit;
711                                                                 /* not reached          */
712                                                                 break;
713                                                         }
714                                                         usleep(50000);
715                                                 } else {
716                                                         read_size += rsize;
717                                                 }
718                                         }
719         
720                                         /* 2004.04.01                                              */
721                                         /* for "skip reading"                                      */
722                                         /* modified y_count count up condition and clear read_size */
723                                         if( read_size == bytes_per_line ){
724                                                 y_count++;
725                                                 /* need to clear read_size               */
726                                                 /* not clear, data still remains on pipe */
727                                                 read_size = 0;
728                                         }
729                                 }
730                                 read_size = 0;
731                                 
732                                 /* convert input raster data to output byte data. */
733                                 {
734                                         int copybyte = 1;
735         
736                                         if ( 1 == byte_par_pixel )
737                                                 copybyte = 3;
738         
739                                         if (x_mag == 1)
740                                         {
741         
742                                                 if ( 1 == byte_par_pixel )
743                                                 {
744                                                         int k;
745         
746                                                         for ( j = 0; j < print_area_x; j++ )
747                                                         {
748                                                                 for ( k = 0; k < copybyte ; k++ )
749                                                                         line[j * 3 + k] = image_raw[j];
750                                                         }
751                                                 }
752                                                 else
753                                                         memcpy (line, image_raw, bytes_per_line);
754                                                 
755                                         }
756                                         else
757                                         {
758                                                 x_count = 0;
759                                                 
760                                                 for (j = 0; j < print_area_x; j ++)
761                                                 {
762                                                         int k;
763         
764                                                         while ( x_count == 0 || j > x_mag * x_count)
765                                                                 x_count ++;
766                                                         
767                                                         for ( k = 0; k < copybyte ; k++ )
768                                                                 memcpy (line + (j * 3 ) + k,
769                                                                 image_raw + ((x_count - 1) * byte_par_pixel),
770                                                                 byte_par_pixel);
771                                                 }
772                                         }
773                                 }
774                                 
775                                 band_line_count ++;
776                                 
777                                 if (band_line_count >= band_line)
778                                 {
779                                         bandRect.bottom = bandRect.top + band_line_count;
780                                         #if 0
781                                         if (escprBandOut (&bandBmp, &bandRect))
782                                                 err_fatal ("Error occurred in \"OUT_FUNC\"."); /* exit */
783                                         #endif
784                                         printHeight = band_line_count;
785                                         PrintBand (bandBmp.Bits, bandBmp.WidthBytes, &printHeight);
786 #if (HAVE_PPM)
787                                         fp = fopen(ppmfilename, "a+");
788                                         int i=0;        
789                                         for(i=0; i<bytes_per_line/byte_par_pixel*3; i++){               
790                                                 fprintf(fp, "%u ", (unsigned char)bandBmp.Bits[i]);
791                                         }
792                                         fprintf(fp, "\n");
793                                         fclose(fp);
794 #endif
795                                         debug_msg("printHeight = %d\n", printHeight);
796                                         debug_msg("WidthByte = %d\n", bandBmp.WidthBytes);
797                                         band_line_count -= printHeight;
798                                         bandBmp.Bits += band_line_count;
799                                         bandRect.top = bandRect.bottom;
800                                 }
801                                 
802                         }
803                         
804                         if (band_line_count > 0)
805                         {
806                                 bandRect.bottom = bandRect.top + band_line_count;
807                                 
808                                 //if (PrintBand (&bandBmp, bandBmp.WidthBytes))
809                                 err = PrintBand (bandBmp.Bits, bandBmp.WidthBytes, &printHeight);
810                                 debug_msg("printHeight = %d\n", printHeight);
811                                 if(err)
812                                         err_fatal ("Error occurred in \"OUT_FUNC\"."); /* exit */
813 #if (HAVE_PPM)
814                                 fp = fopen(ppmfilename, "a+");
815                                 int i=0;        
816                                 for(i=0; i<bytes_per_line/byte_par_pixel*3; i++){               
817                                         fprintf(fp, "%u ", (unsigned char)bandBmp.Bits[i]);
818                                 }
819                                 fprintf(fp, "\n");
820                                 fclose(fp);
821 #endif
822                                 band_line_count -= printHeight;
823                                 bandBmp.Bits += band_line_count;
824                         }
825                         
826                         //if (escprTerminatePage (1ESCPR_END_PAGE))
827                         err = epsEndPage(FALSE);
828                         if(err)
829                                 err_fatal ("Error occurred in \"PEND_FUNC\"."); /* exit */
830                 }
831         }
832         
833         DEDBUG_END;
834
835 /* 2004.04.15 for 'error' */    
836 quit:;
837         if( cancel ){
838                 //escprTerminatePage (ESCPR_END_PAGE);
839                 err = epsEndPage(FALSE);
840         }
841      
842         //if (escprDestroyJob ())
843         err = epsEndJob();
844         if(err)
845                 err_fatal ("Error occurred in \"END_FUNC\"."); /* exit */
846
847         /* free alloced memory */
848         mem_free(image_raw);
849         mem_free(band);
850         mem_free(paper);
851         debug_msg("PRINT SUCCESS\n");
852         return 0;
853 }
854
855
856 static int
857 set_pips_parameter (filter_option_t *filter_opt_p, ESCPR_OPT *printOpt, ESCPR_PRINT_QUALITY *printQuality)
858 {
859         char *mediaType;
860         char *quality;
861         char *ink;
862         char *duplex;
863
864         if (strlen (filter_opt_p->media) == 0
865             || strlen (filter_opt_p->ink) == 0
866             || strlen (filter_opt_p->quality) == 0)
867                 return 1;
868
869         /* pickup MediaType & Quality from input */
870         quality = strrchr(filter_opt_p->quality, '_');
871
872         if(strlen(quality) == 0)
873                 return 1;
874
875         mediaType = str_clone (filter_opt_p->quality, strlen(filter_opt_p->quality) - strlen(quality));
876
877         /* Media Type ID */
878         printQuality->MediaTypeID = getMediaTypeID(mediaType); 
879         jobAttr.mediaTypeIdx = getMediaTypeID(mediaType);
880         debug_msg(" mediaType = %s\n", mediaType);
881
882         /* Print Quality */
883         if(strcmp(quality, "_DRAFT") == 0){
884                 printQuality->PrintQuality = ESCPR_PQ_DRAFT;
885                 jobAttr.printQuality = EPS_MQID_DRAFT; 
886         }else if(strcmp(quality, "_NORMAL") == 0){
887                 printQuality->PrintQuality = ESCPR_PQ_NORMAL;
888                 jobAttr.printQuality = EPS_MQID_NORMAL; 
889         }else{
890                 printQuality->PrintQuality = ESCPR_PQ_HIGH;               
891                 jobAttr.printQuality = EPS_MQID_DRAFT; 
892         }
893
894         /* Ink */
895         ink = str_clone (filter_opt_p->ink, strlen (filter_opt_p->ink));
896         if (strcmp (ink, "COLOR") == 0){ 
897                 printQuality->ColorMono = ESCPR_CM_COLOR; 
898                 jobAttr.colorMode =  EPS_CM_COLOR;
899         }
900         else{ 
901                 printQuality->ColorMono = ESCPR_CM_MONOCHROME;
902                 jobAttr.colorMode =  EPS_CM_MONOCHROME; 
903         }
904
905         /* Duplex */
906         duplex = str_clone (filter_opt_p->duplex, strlen (filter_opt_p->duplex));
907         if (strcmp (duplex, "None") == 0){ 
908                 debug_msg("DUPLEX NONE\n");
909                 jobAttr.duplex =  EPS_DUPLEX_NONE;
910         }
911         if (strcmp (duplex, "DuplexNoTumble") == 0){ 
912                 debug_msg("DUPLEX LONG\n");
913                 jobAttr.duplex =  EPS_DUPLEX_LONG;
914         }
915         if (strcmp (duplex, "DuplexTumble") == 0){ 
916                 debug_msg("DUPLEX SHORT\n");
917                 jobAttr.duplex =  EPS_DUPLEX_SHORT;
918         }
919         /* Others */
920         printQuality->Brightness = 0;
921         printQuality->Contrast = 0;
922         printQuality->Saturation = 0;
923         printQuality->ColorPlane = ESCPR_CP_FULLCOLOR; /* default for epson-escpr */
924         printQuality->PaletteSize = 0;                 /* cause ColorPlane is FULLCOLOR */
925         printQuality->PaletteData = NULL;
926         
927         jobAttr.brightness = 0;
928         jobAttr.contrast = 0;
929         jobAttr.saturation = 0;
930         jobAttr.apfAutoCorrect = EPS_APF_ACT_STANDARD;
931         jobAttr.sharpness = 0;
932         jobAttr.colorPlane = ESCPR_CP_FULLCOLOR;
933         jobAttr.paletteSize = 0;
934         jobAttr.paletteData = NULL;
935         jobAttr.copies = 1;
936         jobAttr.feedDirection = EPS_FEEDDIR_PORTRAIT;      /* paper feed direction  hardcode */
937         jobAttr.paperSource = EPS_MPID_AUTO;
938         jobAttr.printDirection = 0;
939
940         /* free alloced memory */
941         mem_free(mediaType);
942         mem_free(ink);
943
944         return 0;
945 }
946
947
948 /* Get PageSize for PIPS */
949 static int
950 get_page_size (const char *paper, const char *bin_id)
951 {
952 #if 0
953         csvlist_t *csv_p;
954         const char *path = PAPER_PATH;
955         int pos;
956
957         long l_margin, r_margin, t_margin, b_margin;
958         
959         csv_p = csvlist_open (path);
960         if (!csv_p)
961                 err_fatal ("%s : List file is broken.", path);
962
963         pos = csvlist_search_keyword (csv_p, 0, paper);
964         if (pos < 0)
965                 err_fatal ("%s : Unknown PageSize.", paper);
966
967
968         //if(printJob->InResolution == ESCPR_IR_3636 || printJob->InResolution == ESCPR_IR_7272){
969         if(jobAttr.inputResolution == EPS_IR_360X360 || jobAttr.inputResolution == EPS_IR_720X720){
970                 printJob->PaperWidth = atol (csvlist_get_word (csv_p, pos + 2));
971                 printJob->PaperLength = atol (csvlist_get_word (csv_p, pos + 3));
972
973                 l_margin = atol (csvlist_get_word (csv_p, pos + 4));
974                 r_margin = atol (csvlist_get_word (csv_p, pos + 5));
975                 t_margin = atol (csvlist_get_word (csv_p, pos + 6));
976                 b_margin = atol (csvlist_get_word (csv_p, pos + 7));
977         }else{
978                 printJob->PaperWidth = atol (csvlist_get_word (csv_p, pos + 8));
979                 printJob->PaperLength = atol (csvlist_get_word (csv_p, pos + 9));
980
981                 l_margin = atol (csvlist_get_word (csv_p, pos + 10));
982                 r_margin = atol (csvlist_get_word (csv_p, pos + 11));
983                 t_margin = atol (csvlist_get_word (csv_p, pos + 12));
984                 b_margin = atol (csvlist_get_word (csv_p, pos + 13));
985
986         }
987
988
989         if (!strcmp (bin_id, "BORDER"))
990         {
991                 printJob->PrintableAreaWidth = printJob->PaperWidth - l_margin - r_margin;
992                 printJob->PrintableAreaLength = printJob->PaperLength - t_margin - b_margin;
993                 printJob->TopMargin = t_margin;
994                 printJob->LeftMargin = l_margin;
995         }
996
997         else if (!strcmp (bin_id, "BORDERLESS"))
998         {
999                 if(printJob->InResolution == ESCPR_IR_3636 || printJob->InResolution == ESCPR_IR_7272){
1000                         printJob->PrintableAreaWidth = printJob->PaperWidth + 72;
1001                         printJob->PrintableAreaLength = printJob->PaperLength + 112;
1002                         printJob->TopMargin = -42;
1003                         if ( printJob->PaperWidth < 4209 ) 
1004                                 printJob->LeftMargin = -36;
1005                         else
1006                                 printJob->LeftMargin = -48;
1007                 }else{
1008                         printJob->PrintableAreaWidth = printJob->PaperWidth + 60;
1009                         printJob->PrintableAreaLength = printJob->PaperLength + 93;
1010                         printJob->TopMargin = -35;
1011                         if ( printJob->PaperWidth < 3507 ) 
1012                                 printJob->LeftMargin = -30;
1013                         else
1014                                 printJob->LeftMargin = -40;
1015
1016                 }
1017
1018         }
1019
1020         else
1021         {
1022                 err_fatal ("%s : This sheet feeder is not supported.");
1023         }
1024
1025         printJob->PrintDirection =  0; 
1026
1027         /* free alloced memory */
1028         csvlist_close(csv_p);
1029 #endif
1030         return 0;
1031 }
1032
1033 static int  getMediaTypeID(char *rsc_name)
1034 {
1035   int j;
1036         debug_msg("mediatype name = %s\n", rsc_name);
1037   for(j = 0; mediaTypeData[j].value != END_ARRAY; j++)
1038     if(strcmp(mediaTypeData[j].rsc_name,rsc_name) == 0)
1039       {
1040                 debug_msg("mediatype found, idx = %d\n", mediaTypeData[j].value);
1041         return mediaTypeData[j].value;
1042           }
1043   return 0;
1044 }
1045
1046 ESCPR_BYTE4 print_spool_fnc(void* hParam, const ESCPR_UBYTE1* pBuf, ESCPR_UBYTE4 cbBuf) 
1047 {
1048         long int i;
1049         for (i = 0; i < cbBuf; i++)
1050                 putc(*(pBuf + i), outfp);
1051         return 1;
1052 }