]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/Thumb.cc
:ThumbColorMap): unused now, remove.
[evince.git] / pdf / xpdf / Thumb.cc
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-indent-level: 8; c-basic-offset: 8 -*- */
2 /* 
3  *  Copyright (C) 2003 Remi Cohen-Scali
4  *
5  *  Author:
6  *    Remi Cohen-Scali <Remi@Cohen-Scali.com>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <aconf.h>
24 #include <string.h>
25
26 #ifdef USE_GCC_PRAGMAS
27 #pragma implementation
28 #endif
29
30 #include <gpdf-g-switch.h>
31 #  include <glib.h>
32 #include <gpdf-g-switch.h>
33 #include "gmem.h"
34 #include "Object.h"
35 #include "Gfx.h"
36 #include "GfxState.h"
37 #include "Thumb.h"
38
39 /*
40  * Thumb
41  */
42
43 Thumb::Thumb(XRef *xrefA, Object *obj) :
44   xref(xrefA),
45   str(NULL)
46 {
47         Object obj1, obj2;
48         Dict *dict;
49         GfxColorSpace *colorSpace;
50
51         do {
52                 /* Get stream dict */
53                 dict = obj->streamGetDict ();
54                 str = obj->getStream(); 
55                 
56                 /* Get width */
57                 dict->lookup ("Width", &obj1);
58                 if (obj1.isNull ())
59                 {
60                         obj1.free ();
61                         dict->lookup ("W", &obj1);
62                 }
63                 if (!obj1.isInt ()) {
64                         fprintf (stderr, "Error: Invalid Width object %s\n",
65                                 obj1.getTypeName ());
66                         obj1.free ();
67                         break;
68                 }
69                 width = obj1.getInt ();
70                 obj1.free ();
71                 
72                 /* Get heigth */
73                 dict->lookup ("Height", &obj1);
74                 if (obj1.isNull ()) 
75                 {
76                         obj1.free ();
77                         dict->lookup ("H", &obj1);
78                 }
79                 if (!obj1.isInt ()) {
80                         fprintf (stderr, "Error: Invalid Height object %s\n",
81                                 obj1.getTypeName ());
82                         obj1.free ();
83                         break;
84                 }
85                 height = obj1.getInt ();
86                 obj1.free ();
87                 
88                 /* bit depth */
89                 dict->lookup ("BitsPerComponent", &obj1);
90                 if (obj1.isNull ())
91                 {
92                         obj1.free ();
93                         dict->lookup ("BPC", &obj1);
94                 }
95                 if (!obj1.isInt ()) {
96                         fprintf (stderr, "Error: Invalid BitsPerComponent object %s\n",
97                                 obj1.getTypeName ());
98                         obj1.free ();
99                         break;
100                 }
101                 bits = obj1.getInt ();
102                 obj1.free ();
103                 
104                 /* Get color space */
105                 dict->lookup ("ColorSpace", &obj1);
106                 if (obj1.isNull ()) 
107                 {
108                         obj1.free ();
109                         dict->lookup ("CS", &obj1);
110                 }
111                 colorSpace = GfxColorSpace::parse(&obj1);
112                 obj1.free();
113                 if (!colorSpace) {
114                         fprintf (stderr, "Error: Cannot parse color space\n");
115                         break;
116                 }
117
118                 dict->lookup("Decode", &obj1);
119                 if (obj1.isNull()) {
120                         obj1.free();
121                         dict->lookup("D", &obj1);
122                 }
123                 colorMap = new GfxImageColorMap(bits, &obj1, colorSpace);
124                 obj1.free();
125                 if (!colorMap->isOk()) {
126                         fprintf (stderr, "Error: invalid colormap\n");
127                         delete colorMap;
128                         colorMap = NULL;
129                 }
130
131                 dict->lookup ("Length", &obj1);
132                 if (!obj1.isInt ()) {
133                         fprintf (stderr, "Error: Invalid Length Object %s\n",
134                                 obj1.getTypeName ());
135                         obj1.free ();
136                         break;
137                 }
138                 length = obj1.getInt ();
139                 obj1.free ();
140
141                 str->addFilters(obj);
142         }
143         while (0);      
144 }
145
146 unsigned char *
147 Thumb::getPixbufData()
148 {
149         ImageStream *imgstr;
150         unsigned char *pixbufdata;
151         unsigned int pixbufdatasize;
152         int row, col;
153         unsigned char *p;
154
155         /* RGB Pixbuf data */
156         pixbufdatasize = width * height * 3;
157         if (colorMap) {
158                 pixbufdata =(unsigned char *)g_malloc(pixbufdatasize);
159         } else {
160                 pixbufdata =(unsigned char *)g_malloc0(pixbufdatasize);
161                 return pixbufdata;
162         }
163
164         p = pixbufdata;
165
166         imgstr = new ImageStream(str, width, colorMap->getNumPixelComps(), colorMap->getBits());
167         imgstr->reset();
168         for (row = 0; row < height; ++row) {
169             for (col = 0; col < width; ++col) {
170                 Guchar pix[gfxColorMaxComps];
171                 GfxRGB rgb;
172
173                 imgstr->getPixel(pix);
174                 colorMap->getRGB(pix, &rgb);
175
176                 *p++ = (guchar)(rgb.r * 255.99999);
177                 *p++ = (guchar)(rgb.g * 255.99999);
178                 *p++ = (guchar)(rgb.b * 255.99999);
179             }
180         }
181         delete imgstr;
182
183         return pixbufdata;
184 }
185
186 Thumb::~Thumb() {
187         delete colorMap;
188         delete str;
189 }
190