1 /*
2 * raster.cpp (C) 2006, Aurélien Croc (AP²C)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * $Id$
19 *
20 */
21 #include "raster.h"
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "error.h"
29
30 /*
31 * Constructeur - Destructeur
32 * Init - Uninit
33 */
Raster(const char * job,const char * user,const char * title,const char * copies,const char * options,const char * file)34 Raster::Raster(const char *job, const char *user, const char *title,
35 const char *copies, const char *options, const char *file)
36 {
37 _jobId = job;
38 _user = user;
39 _title = title;
40 _copies = copies;
41 _options = options;
42 _file = file;
43
44 _width = 0;
45 _height = 0;
46 _lineSize = 0;
47 _line = 0;
48 _page = 0;
49 _lineBuffer = NULL;
50 }
51
~Raster()52 Raster::~Raster()
53 {
54 unload();
55 }
56
57
58
59 /*
60 * Chargement de l'image
61 * Load the image
62 */
unload()63 void Raster::unload()
64 {
65 if (_lineBuffer)
66 delete[] _lineBuffer;
67 cupsRasterClose(_ras);
68 }
69
load()70 int Raster::load()
71 {
72 // Open the raster file if needed
73 if (_file && (_fd = open(_file, O_RDONLY)) == -1) {
74 fprintf(stderr, _("ERROR: Unable to open the raster file %s\n"), _file);
75 sleep(1);
76 return -1;
77 }
78
79 _ras = cupsRasterOpen(_fd, CUPS_RASTER_READ);
80 return 0;
81 }
82
loadPage(Printer * printer)83 int Raster::loadPage(Printer *printer)
84 {
85 if (!cupsRasterReadHeader(_ras, &_header)) {
86 DEBUG("Plus de page");
87 return 1;
88 }
89 _width = _header.cupsWidth;
90 _height = _header.cupsHeight;
91 _totalLines = _height;
92 _lineSize = _header.cupsBytesPerLine;
93 _line = 0;
94 _page++;
95
96 // Configure the printer
97 printer->setResolution(_header.HWResolution[0],_header.HWResolution[1]);
98 printer->setPageSizeX(_header.PageSize[0]);
99 printer->setPageSizeY(_header.PageSize[1]);
100 printer->setMarginX(_header.ImagingBoundingBox[0]);
101 printer->setMarginY(_header.ImagingBoundingBox[1]);
102 printer->setAreaX(_header.PageSize[0] - _header.ImagingBoundingBox[0]);
103 printer->setAreaY(_header.PageSize[1] - _header.ImagingBoundingBox[1]);
104 printer->setPrintableX(_header.ImagingBoundingBox[2] -
105 _header.ImagingBoundingBox[0]);
106 printer->setPrintableY(_header.ImagingBoundingBox[3] -
107 _header.ImagingBoundingBox[1]);
108
109 // Get some document informations
110 _color = _header.cupsColorSpace == CUPS_CSPACE_K ? false : true;
111 printer->setCompVersion(_header.cupsCompression);
112
113 if (_color) {
114 _totalLines = _totalLines * 4;
115 _lineSize = _lineSize >> 2;
116 }
117
118 return 0;
119 }
120
121
122
123 /*
124 * Lecture d'une ligne
125 * Read a line
126 */
readLine()127 int Raster::readLine()
128 {
129 if (!_ras)
130 return -1;
131 if (!_lineBuffer)
132 _lineBuffer = new unsigned char[_lineSize];
133
134 /*
135 * so that we can round up to bandHeight, we return an empty line
136 * after reading more than _height lines.
137 * -- Keith White
138 */
139 if (_line >= _totalLines) {
140 memset(_lineBuffer, 0x00, _lineSize);
141 return _lineSize;
142 }
143
144 if (cupsRasterReadPixels(_ras, _lineBuffer, _lineSize) < 1) {
145 ERROR(_("Raster::readLine: Cannot read image data"));
146 return -1;
147 }
148 _line++;
149 return _lineSize;
150 }
151
152 /* vim: set expandtab tabstop=4 shiftwidth=4 smarttab tw=80 cin enc=utf8: */
153
154