1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-indent-level: 8; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003 Remi Cohen-Scali
6 * Remi Cohen-Scali <Remi@Cohen-Scali.com>
8 * GPdf is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * GPdf is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 * License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
26 #ifdef USE_GCC_PRAGMAS
27 #pragma implementation
30 #include <gpdf-g-switch.h>
32 #include <gpdf-g-switch.h>
39 static GHashTable *cmhash = NULL;
44 ThumbColorMap::ThumbColorMap(int bitsA,
53 GfxIndexedColorSpace *iCS;
54 GfxSeparationColorSpace *sepCS;
61 maxPixel = (1 << bits) - 1;
64 if (!obj->isStream ()) {
65 printf ("Error: Invalid object of type %s\n",
69 str = obj->getStream();
71 streamDict = obj->streamGetDict ();
73 streamDict->lookupNF ("Filter", &obj1);
74 if (!obj1.isArray ()) {
75 printf ("Error: Invalid filter object of type %s\n",
80 str = str->addFilters(obj);
82 streamDict->lookup ("Length", &obj1);
85 printf ("Error: No Length object\n");
89 printf ("Error: Invalid Width object %s\n",
94 length = obj1.getInt ();
97 nComps = cs->getNComps();
99 if (cs->getMode () == csIndexed) {
100 iCS = (GfxIndexedColorSpace *)cs;
101 baseNComps = iCS->getBase ()->getNComps ();
103 if (iCS->getBase ()->getMode () == csDeviceGray) {
104 gray = (double *)gmalloc(sizeof(double) * (iCS->getIndexHigh () + 1));
105 for (n = 0; n <= iCS->getIndexHigh (); n++) {
106 double comp = (double)str->getChar();
107 //printf ("Gray pixel [%03d] = %02x\n", n, (int)comp);
108 gray[n] = comp / (double)iCS->getIndexHigh ();
111 else if (iCS->getBase ()->getMode () == csDeviceRGB) {
112 rgb = (GfxRGB *)gmalloc(sizeof(GfxRGB) * (iCS->getIndexHigh () + 1));
113 for (n = 0; n <= iCS->getIndexHigh (); n++) {
114 double comp_r = (double)str->getChar();
115 double comp_g = (double)str->getChar();
116 double comp_b = (double)str->getChar();
117 // printf ("RGB pixel [0x%02x] = (%02x,%02x,%02x)\n",
118 // n, (int)comp_r, (int)comp_g, (int)comp_b);
119 rgb[n].r = comp_r / (double)iCS->getIndexHigh ();
120 rgb[n].g = comp_g / (double)iCS->getIndexHigh ();
121 rgb[n].b = comp_b / (double)iCS->getIndexHigh ();
124 else if (iCS->getBase ()->getMode () == csDeviceCMYK) {
125 cmyk = (GfxCMYK *)gmalloc(sizeof(GfxCMYK) * (iCS->getIndexHigh () + 1));
126 for (n = 0; n <= iCS->getIndexHigh (); n++) {
127 double comp_c = (double)str->getChar();
128 double comp_m = (double)str->getChar();
129 double comp_y = (double)str->getChar();
130 double comp_k = (double)str->getChar();
131 //printf ("CMYK pixel [%03d] = (%02x,%02x,%02x,%02x)\n",
132 // n, (int)comp_c, (int)comp_m, (int)comp_y, (int)comp_k);
133 cmyk[n].c = comp_c / (double)iCS->getIndexHigh ();
134 cmyk[n].m = comp_m / (double)iCS->getIndexHigh ();
135 cmyk[n].y = comp_y / (double)iCS->getIndexHigh ();
136 cmyk[n].k = comp_k / (double)iCS->getIndexHigh ();
140 else if (cs->getMode () == csSeparation) {
141 sepCS = (GfxSeparationColorSpace *)cs;
142 /* FIXME: still to do */
151 ThumbColorMap::lookupColorMap(XRef *xref, int bits, Object *obj, GfxColorSpace *cs)
158 cmhash = g_hash_table_new(NULL, g_int_equal);
160 key = g_strdup_printf ("%d %d R", obj->getRefNum (), obj->getRefGen ());
162 if (!(cm = (ThumbColorMap *)g_hash_table_lookup (cmhash, &key))) {
163 cm = new ThumbColorMap(bits, obj->fetch(xref, &obj1), cs);
165 g_hash_table_insert(cmhash, &key, cm);
174 ThumbColorMap::getGray(Guchar *x, double *outgray)
180 ThumbColorMap::getRGB(Guchar *x, GfxRGB *outrgb)
182 outrgb->r = rgb[*x].r;
183 outrgb->g = rgb[*x].g;
184 outrgb->b = rgb[*x].b;
188 ThumbColorMap::getCMYK(Guchar *x, GfxCMYK *outcmyk)
190 outcmyk->c = cmyk[*x].c;
191 outcmyk->m = cmyk[*x].m;
192 outcmyk->y = cmyk[*x].y;
193 outcmyk->k = cmyk[*x].k;
196 ThumbColorMap::~ThumbColorMap()
206 Thumb::Thumb(XRef *xrefA, Object *obj) :
214 /* Get stream dict */
215 dict = obj->streamGetDict ();
216 str = obj->getStream();
219 dict->lookup ("Width", &obj1);
223 dict->lookup ("W", &obj1);
225 if (!obj1.isInt ()) {
226 printf ("Error: Invalid Width object %s\n",
227 obj1.getTypeName ());
231 width = obj1.getInt ();
235 dict->lookup ("Height", &obj1);
239 dict->lookup ("H", &obj1);
241 if (!obj1.isInt ()) {
242 printf ("Error: Invalid Height object %s\n",
243 obj1.getTypeName ());
247 height = obj1.getInt ();
251 dict->lookup ("BitsPerComponent", &obj1);
255 dict->lookup ("BPC", &obj1);
257 if (!obj1.isInt ()) {
258 printf ("Error: Invalid BitsPerComponent object %s\n",
259 obj1.getTypeName ());
263 bits = obj1.getInt ();
266 /* Get color space */
267 dict->lookup ("ColorSpace", &obj1);
271 dict->lookup ("CS", &obj1);
273 if (!(gfxCS = GfxColorSpace::parse (&obj1)))
275 printf ("Error: Cannot parse color space\n");
279 if (gfxCS->getMode () == csIndexed)
280 thumbCM = ThumbColorMap::lookupColorMap (xref, bits, obj1.arrayGetNF(3, &obj2), gfxCS);
281 else if (gfxCS->getMode () == csSeparation)
282 printf ("Not yet implemented\n");
285 dict->lookup ("Length", &obj1);
286 if (!obj1.isInt ()) {
287 printf ("Error: Invalid Length Object %s\n",
288 obj1.getTypeName ());
292 length = obj1.getInt ();
295 str->addFilters(obj);
301 Thumb::getPixbufData()
304 unsigned char *pixbufdata;
305 unsigned int pixbufdatasize;
309 /* RGB Pixbuf data */
310 pixbufdatasize = width * height * 3;
311 pixbufdata =(unsigned char *)g_malloc(pixbufdatasize);
314 imgstr = new ImageStream(str, width, thumbCM->getNumPixelComps(), thumbCM->getBits());
316 for (row = 0; row < height; ++row) {
317 for (col = 0; col < width; ++col) {
318 Guchar pix[gfxColorMaxComps];
321 imgstr->getPixel(pix);
322 thumbCM->getRGB(pix, &rgb);
324 *p++ = (guchar)(rgb.r * 255.99999);
325 *p++ = (guchar)(rgb.g * 255.99999);
326 *p++ = (guchar)(rgb.b * 255.99999);