]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/Thumb.cc
:Thumb): Added a flag for keeping Thumb data validity and the method to
[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   ok_flag(0)
47 {
48         Object obj1, obj2;
49         Dict *dict;
50         GfxColorSpace *colorSpace;
51
52         do {
53                 /* Get stream dict */
54                 dict = obj->streamGetDict ();
55                 str = obj->getStream(); 
56                 
57                 /* Get width */
58                 dict->lookup ("Width", &obj1);
59                 if (obj1.isNull ())
60                 {
61                         obj1.free ();
62                         dict->lookup ("W", &obj1);
63                 }
64                 if (!obj1.isInt ()) {
65                         fprintf (stderr, "Error: Invalid Width object %s\n",
66                                 obj1.getTypeName ());
67                         obj1.free ();
68                         break;
69                 }
70                 width = obj1.getInt ();
71                 obj1.free ();
72                 
73                 /* Get heigth */
74                 dict->lookup ("Height", &obj1);
75                 if (obj1.isNull ()) 
76                 {
77                         obj1.free ();
78                         dict->lookup ("H", &obj1);
79                 }
80                 if (!obj1.isInt ()) {
81                         fprintf (stderr, "Error: Invalid Height object %s\n",
82                                 obj1.getTypeName ());
83                         obj1.free ();
84                         break;
85                 }
86                 height = obj1.getInt ();
87                 obj1.free ();
88                 
89                 /* bit depth */
90                 dict->lookup ("BitsPerComponent", &obj1);
91                 if (obj1.isNull ())
92                 {
93                         obj1.free ();
94                         dict->lookup ("BPC", &obj1);
95                 }
96                 if (!obj1.isInt ()) {
97                         fprintf (stderr, "Error: Invalid BitsPerComponent object %s\n",
98                                 obj1.getTypeName ());
99                         obj1.free ();
100                         break;
101                 }
102                 bits = obj1.getInt ();
103                 obj1.free ();
104                 
105                 /* Get color space */
106                 dict->lookup ("ColorSpace", &obj1);
107                 if (obj1.isNull ()) 
108                 {
109                         obj1.free ();
110                         dict->lookup ("CS", &obj1);
111                 }
112                 colorSpace = GfxColorSpace::parse(&obj1);
113                 obj1.free();
114                 if (!colorSpace) {
115                         fprintf (stderr, "Error: Cannot parse color space\n");
116                         break;
117                 }
118
119                 dict->lookup("Decode", &obj1);
120                 if (obj1.isNull()) {
121                         obj1.free();
122                         dict->lookup("D", &obj1);
123                 }
124                 colorMap = new GfxImageColorMap(bits, &obj1, colorSpace);
125                 obj1.free();
126                 if (!colorMap->isOk()) {
127                         fprintf (stderr, "Error: invalid colormap\n");
128                         delete colorMap;
129                         colorMap = NULL;
130                 }
131
132                 dict->lookup ("Length", &obj1);
133                 if (!obj1.isInt ()) {
134                         fprintf (stderr, "Error: Invalid Length Object %s\n",
135                                 obj1.getTypeName ());
136                         obj1.free ();
137                         break;
138                 }
139                 length = obj1.getInt ();
140                 obj1.free ();
141
142                 str->addFilters(obj);
143
144                 ok_flag = 1; 
145         }
146         while (0);      
147 }
148
149 unsigned char *
150 Thumb::getPixbufData()
151 {
152         ImageStream *imgstr;
153         unsigned char *pixbufdata;
154         unsigned int pixbufdatasize;
155         int row, col;
156         unsigned char *p;
157
158         /* RGB Pixbuf data */
159         pixbufdatasize = width * height * 3;
160         if (colorMap) {
161                 pixbufdata =(unsigned char *)g_malloc(pixbufdatasize);
162         } else {
163                 pixbufdata =(unsigned char *)g_malloc0(pixbufdatasize);
164                 return pixbufdata;
165         }
166
167         p = pixbufdata;
168
169         imgstr = new ImageStream(str, width, colorMap->getNumPixelComps(), colorMap->getBits());
170         imgstr->reset();
171         for (row = 0; row < height; ++row) {
172             for (col = 0; col < width; ++col) {
173                 Guchar pix[gfxColorMaxComps];
174                 GfxRGB rgb;
175
176                 imgstr->getPixel(pix);
177                 colorMap->getRGB(pix, &rgb);
178
179                 *p++ = (guchar)(rgb.r * 255.99999);
180                 *p++ = (guchar)(rgb.g * 255.99999);
181                 *p++ = (guchar)(rgb.b * 255.99999);
182             }
183         }
184         delete imgstr;
185
186         return pixbufdata;
187 }
188
189 Thumb::~Thumb() {
190         delete colorMap;
191         delete str;
192 }
193