author | llornkcor <llornkcor> | 2003-03-02 19:31:31 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2003-03-02 19:31:31 (UTC) |
commit | ab5f21907fb482d35f8aebe4505e954d3c22f3c7 (patch) (unidiff) | |
tree | c85adac27fa84a25ef49bdb23bf050c43979617d | |
parent | 6657040ae54afb2f2378f4db7bfc9e616e6d7b83 (diff) | |
download | opie-ab5f21907fb482d35f8aebe4505e954d3c22f3c7.zip opie-ab5f21907fb482d35f8aebe4505e954d3c22f3c7.tar.gz opie-ab5f21907fb482d35f8aebe4505e954d3c22f3c7.tar.bz2 |
patch sneaked by me
-rw-r--r-- | noncore/apps/opie-reader/Palm2QImage.cpp | 74 |
1 files changed, 46 insertions, 28 deletions
diff --git a/noncore/apps/opie-reader/Palm2QImage.cpp b/noncore/apps/opie-reader/Palm2QImage.cpp index ef88cc5..9603877 100644 --- a/noncore/apps/opie-reader/Palm2QImage.cpp +++ b/noncore/apps/opie-reader/Palm2QImage.cpp | |||
@@ -1,29 +1,31 @@ | |||
1 | /* -*- mode: c; indent-tabs-mode: nil; -*- */ | 1 | /* -*- mode: c; indent-tabs-mode: nil; -*- */ |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <unistd.h> /* for link */ | 5 | #ifndef WINDOWS |
6 | #include <unistd.h> /* for link */ | ||
7 | #endif | ||
6 | #include <sys/types.h> | 8 | #include <sys/types.h> |
7 | #include <sys/stat.h> | 9 | #include <sys/stat.h> |
8 | #include <stdarg.h> | 10 | #include <stdarg.h> |
9 | 11 | ||
10 | #include <qimage.h> | 12 | #include <qimage.h> |
11 | 13 | ||
12 | /***********************************************************************/ | 14 | /***********************************************************************/ |
13 | /***********************************************************************/ | 15 | /***********************************************************************/ |
14 | /***** *****/ | 16 | /***** *****/ |
15 | /***** Code to decode the Palm image format to JPEG *****/ | 17 | /***** Code to decode the Palm image format to JPEG *****/ |
16 | /***** *****/ | 18 | /***** *****/ |
17 | /***********************************************************************/ | 19 | /***********************************************************************/ |
18 | /***********************************************************************/ | 20 | /***********************************************************************/ |
19 | 21 | ||
20 | #define READ_BIGENDIAN_SHORT(p) (((p)[0] << 8)|((p)[1])) | 22 | #define READ_BIGENDIAN_SHORT(p) (((p)[0] << 8)|((p)[1])) |
21 | #define READ_BIGENDIAN_LONG(p) (((p)[0] << 24)|((p)[1] << 16)|((p)[2] << 8)|((p)[3])) | 23 | #define READ_BIGENDIAN_LONG(p) (((p)[0] << 24)|((p)[1] << 16)|((p)[2] << 8)|((p)[3])) |
22 | 24 | ||
23 | #define PALM_IS_COMPRESSED_FLAG 0x8000 | 25 | #define PALM_IS_COMPRESSED_FLAG 0x8000 |
24 | #define PALM_HAS_COLORMAP_FLAG 0x4000 | 26 | #define PALM_HAS_COLORMAP_FLAG 0x4000 |
25 | #define PALM_HAS_TRANSPARENCY_FLAG 0x2000 | 27 | #define PALM_HAS_TRANSPARENCY_FLAG 0x2000 |
26 | #define PALM_DIRECT_COLOR_FLAG 0x0400 | 28 | #define PALM_DIRECT_COLOR_FLAG 0x0400 |
27 | #define PALM_4_BYTE_FIELD_FLAG 0x0200 | 29 | #define PALM_4_BYTE_FIELD_FLAG 0x0200 |
28 | 30 | ||
29 | #define PALM_COMPRESSION_SCANLINE 0x00 | 31 | #define PALM_COMPRESSION_SCANLINE 0x00 |
@@ -118,173 +120,189 @@ static ColorMapEntry Palm4BitColormap[] = { | |||
118 | 120 | ||
119 | QImage* Palm2QImage | 121 | QImage* Palm2QImage |
120 | (unsigned char *image_bytes_in, int byte_count_in) | 122 | (unsigned char *image_bytes_in, int byte_count_in) |
121 | { | 123 | { |
122 | unsigned int width, height, bytes_per_row, flags, next_depth_offset; | 124 | unsigned int width, height, bytes_per_row, flags, next_depth_offset; |
123 | unsigned int bits_per_pixel, version, transparent_index, compression_type, i, j, inval, inbit, mask, incount; | 125 | unsigned int bits_per_pixel, version, transparent_index, compression_type, i, j, inval, inbit, mask, incount; |
124 | unsigned int palm_red_bits, palm_green_bits, palm_blue_bits; | 126 | unsigned int palm_red_bits, palm_green_bits, palm_blue_bits; |
125 | unsigned char *palm_ptr, *x_ptr, *imagedata, *inbyte, *rowbuf, *lastrow, | 127 | unsigned char *palm_ptr, *x_ptr, *imagedata, *inbyte, *rowbuf, *lastrow, |
126 | *imagedatastart, *palmimage; | 128 | *imagedatastart, *palmimage; |
127 | ColorMapEntry *colormap; | 129 | ColorMapEntry *colormap; |
128 | 130 | ||
129 | palmimage = image_bytes_in; | 131 | palmimage = image_bytes_in; |
130 | width = READ_BIGENDIAN_SHORT(palmimage + 0); | 132 | width = READ_BIGENDIAN_SHORT(palmimage + 0); |
131 | height = READ_BIGENDIAN_SHORT(palmimage + 2); | 133 | height = READ_BIGENDIAN_SHORT(palmimage + 2); |
132 | bytes_per_row = READ_BIGENDIAN_SHORT(palmimage + 4); | 134 | bytes_per_row = READ_BIGENDIAN_SHORT(palmimage + 4); |
133 | flags = READ_BIGENDIAN_SHORT(palmimage + 6); | 135 | flags = READ_BIGENDIAN_SHORT(palmimage + 6); |
134 | bits_per_pixel = palmimage[8]; | 136 | bits_per_pixel = palmimage[8]; |
135 | version = palmimage[9]; | 137 | version = palmimage[9]; |
136 | next_depth_offset = READ_BIGENDIAN_SHORT(palmimage + 10); | 138 | next_depth_offset = READ_BIGENDIAN_SHORT(palmimage + 10); |
137 | transparent_index = palmimage[12]; | 139 | transparent_index = palmimage[12]; |
138 | compression_type = palmimage[13]; | 140 | compression_type = palmimage[13]; |
139 | /* bytes 14 and 15 are reserved by Palm and always 0 */ | 141 | /* bytes 14 and 15 are reserved by Palm and always 0 */ |
140 | 142 | ||
141 | #if 0 | 143 | #if 0 |
142 | qDebug ("Palm image is %dx%d, %d bpp, version %d, flags 0x%x, compression %d", | 144 | // qDebug ("Palm image is %dx%d, %d bpp, version %d, flags 0x%x, compression %d", width, height, bits_per_pixel, version, flags, compression_type); |
143 | width, height, bits_per_pixel, version, flags, compression_type); | ||
144 | #endif | 145 | #endif |
145 | 146 | ||
146 | if (compression_type == PALM_COMPRESSION_PACKBITS) { | 147 | if (compression_type == PALM_COMPRESSION_PACKBITS) { |
147 | qDebug ("Image uses packbits compression; not yet supported"); | 148 | // qDebug ("Image uses packbits compression; not yet supported"); |
148 | return NULL; | 149 | return NULL; |
149 | } else if ((compression_type != PALM_COMPRESSION_NONE) && | 150 | } else if ((compression_type != PALM_COMPRESSION_NONE) && |
150 | (compression_type != PALM_COMPRESSION_RLE) && | 151 | (compression_type != PALM_COMPRESSION_RLE) && |
151 | (compression_type != PALM_COMPRESSION_SCANLINE)) { | 152 | (compression_type != PALM_COMPRESSION_SCANLINE)) { |
152 | qDebug ("Image uses unknown compression, code 0x%x", compression_type); | 153 | // qDebug ("Image uses unknown compression, code 0x%x", compression_type); |
153 | return NULL; | 154 | return NULL; |
154 | } | 155 | } |
155 | 156 | ||
156 | /* as of PalmOS 4.0, there are 6 different kinds of Palm pixmaps: | 157 | /* as of PalmOS 4.0, there are 6 different kinds of Palm pixmaps: |
157 | 158 | ||
158 | 1, 2, or 4 bit grayscale | 159 | 1, 2, or 4 bit grayscale |
159 | 8-bit StaticColor using the Palm standard colormap | 160 | 8-bit StaticColor using the Palm standard colormap |
160 | 8-bit PseudoColor using a user-specified colormap | 161 | 8-bit PseudoColor using a user-specified colormap |
161 | 16-bit DirectColor using 5 bits for red, 6 for green, and 5 for blue | 162 | 16-bit DirectColor using 5 bits for red, 6 for green, and 5 for blue |
162 | 163 | ||
163 | Each of these can be compressed with one of four compression schemes, | 164 | Each of these can be compressed with one of four compression schemes, |
164 | "RLE", "Scanline", "PackBits", or none. | 165 | "RLE", "Scanline", "PackBits", or none. |
165 | 166 | ||
166 | We begin by constructing the colormap. | 167 | We begin by constructing the colormap. |
167 | */ | 168 | */ |
168 | 169 | ||
169 | if (flags & PALM_HAS_COLORMAP_FLAG) { | 170 | if (flags & PALM_HAS_COLORMAP_FLAG) { |
170 | qDebug("Palm images with custom colormaps are not currently supported.\n"); | 171 | // qDebug("Palm images with custom colormaps are not currently supported.\n"); |
171 | return NULL; | 172 | return NULL; |
172 | } else if (bits_per_pixel == 1) { | 173 | } else if (bits_per_pixel == 1) { |
173 | colormap = Palm1BitColormap; | 174 | colormap = Palm1BitColormap; |
174 | imagedatastart = palmimage + 16; | 175 | imagedatastart = palmimage + 16; |
175 | } else if (bits_per_pixel == 2) { | 176 | } else if (bits_per_pixel == 2) { |
176 | colormap = Palm2BitColormap; | 177 | colormap = Palm2BitColormap; |
177 | imagedatastart = palmimage + 16; | 178 | imagedatastart = palmimage + 16; |
178 | } else if (bits_per_pixel == 4) { | 179 | } else if (bits_per_pixel == 4) { |
179 | colormap = Palm4BitColormap; | 180 | colormap = Palm4BitColormap; |
180 | imagedatastart = palmimage + 16; | 181 | imagedatastart = palmimage + 16; |
181 | } else if (bits_per_pixel == 8) { | 182 | } else if (bits_per_pixel == 8) { |
182 | colormap = Palm8BitColormap; | 183 | colormap = Palm8BitColormap; |
183 | imagedatastart = palmimage + 16; | 184 | imagedatastart = palmimage + 16; |
184 | } else if (bits_per_pixel == 16 && (flags & PALM_DIRECT_COLOR_FLAG)) { | 185 | } else if (bits_per_pixel == 16 && (flags & PALM_DIRECT_COLOR_FLAG)) { |
185 | colormap = NULL; | 186 | colormap = NULL; |
186 | palm_red_bits = palmimage[16]; | 187 | palm_red_bits = palmimage[16]; |
187 | palm_green_bits = palmimage[17]; | 188 | palm_green_bits = palmimage[17]; |
188 | palm_blue_bits = palmimage[18]; | 189 | palm_blue_bits = palmimage[18]; |
190 | // qDebug("Bits:%d, %d, %d", palm_red_bits, palm_green_bits, palm_blue_bits); | ||
189 | if (palm_blue_bits > 8 || palm_green_bits > 8 || palm_red_bits > 8) { | 191 | if (palm_blue_bits > 8 || palm_green_bits > 8 || palm_red_bits > 8) { |
190 | qDebug("Can't handle this format DirectColor image -- too wide in some color (%d:%d:%d)\n", | 192 | // qDebug("Can't handle this format DirectColor image -- too wide in some color (%d:%d:%d)\n", palm_red_bits, palm_green_bits, palm_blue_bits); |
191 | palm_red_bits, palm_green_bits, palm_blue_bits); | ||
192 | return NULL; | 193 | return NULL; |
193 | } | 194 | } |
194 | if (bits_per_pixel > (8 * sizeof(unsigned long))) { | 195 | if (bits_per_pixel > (8 * sizeof(unsigned long))) { |
195 | qDebug ("Can't handle this format DirectColor image -- too many bits per pixel (%d)\n", | 196 | // qDebug ("Can't handle this format DirectColor image -- too many bits per pixel (%d)\n", bits_per_pixel); |
196 | bits_per_pixel); | ||
197 | return NULL; | 197 | return NULL; |
198 | } | 198 | } |
199 | imagedatastart = palmimage + 24; | 199 | imagedatastart = palmimage + 24; |
200 | } else { | 200 | } else { |
201 | qDebug("Unknown bits-per-pixel of %d encountered.\n", bits_per_pixel); | 201 | // qDebug("Unknown bits-per-pixel of %d encountered.\n", bits_per_pixel); |
202 | return NULL; | 202 | return NULL; |
203 | } | 203 | } |
204 | 204 | ||
205 | QImage* qimage = new QImage(width, height, 16); | 205 | #ifdef WINDOWS |
206 | QImage* qimage = new QImage(width, height, 32); | ||
207 | #else | ||
208 | QImage* qimage = new QImage(width, height, 16); | ||
209 | #endif | ||
206 | 210 | ||
207 | /* row by row, uncompress the Palm image and copy it to the JPEG buffer */ | 211 | /* row by row, uncompress the Palm image and copy it to the JPEG buffer */ |
208 | rowbuf = new unsigned char[bytes_per_row * width]; | 212 | rowbuf = new unsigned char[bytes_per_row * width]; |
209 | lastrow = new unsigned char[bytes_per_row * width]; | 213 | lastrow = new unsigned char[bytes_per_row * width]; |
210 | 214 | ||
211 | for (i=0, palm_ptr = imagedatastart , x_ptr = imagedata; i < height; ++i) { | 215 | for (i=0, palm_ptr = imagedatastart , x_ptr = imagedata; i < height; ++i) { |
216 | // qDebug("inval:%x palm_ptr:%x x_ptr:%x bpr:%x", inval, palm_ptr, x_ptr, bytes_per_row); | ||
212 | 217 | ||
213 | /* first, uncompress the Palm image */ | 218 | /* first, uncompress the Palm image */ |
214 | if ((flags & PALM_IS_COMPRESSED_FLAG) && (compression_type == PALM_COMPRESSION_RLE)) { | 219 | if ((flags & PALM_IS_COMPRESSED_FLAG) && (compression_type == PALM_COMPRESSION_RLE)) { |
215 | for (j = 0; j < bytes_per_row; ) { | 220 | for (j = 0; j < bytes_per_row; ) { |
216 | incount = *palm_ptr++; | 221 | incount = *palm_ptr++; |
217 | inval = *palm_ptr++; | 222 | inval = *palm_ptr++; |
218 | memset(rowbuf + j, inval, incount); | 223 | memset(rowbuf + j, inval, incount); |
219 | j += incount; | 224 | j += incount; |
220 | } | 225 | } |
221 | } else if ((flags & PALM_IS_COMPRESSED_FLAG) && (compression_type == PALM_COMPRESSION_SCANLINE)) { | 226 | } else if ((flags & PALM_IS_COMPRESSED_FLAG) && (compression_type == PALM_COMPRESSION_SCANLINE)) { |
222 | for (j = 0; j < bytes_per_row; j += 8) { | 227 | for (j = 0; j < bytes_per_row; j += 8) { |
223 | incount = *palm_ptr++; | 228 | incount = *palm_ptr++; |
224 | inval = ((bytes_per_row - j) < 8) ? (bytes_per_row - j) : 8; | 229 | inval = ((bytes_per_row - j) < 8) ? (bytes_per_row - j) : 8; |
225 | for (inbit = 0; inbit < inval; inbit += 1) { | 230 | for (inbit = 0; inbit < inval; inbit += 1) { |
226 | if (incount & (1 << (7 - inbit))) | 231 | if (incount & (1 << (7 - inbit))) |
227 | rowbuf[j + inbit] = *palm_ptr++; | 232 | rowbuf[j + inbit] = *palm_ptr++; |
228 | else | 233 | else |
229 | rowbuf[j + inbit] = lastrow[j + inbit]; | 234 | rowbuf[j + inbit] = lastrow[j + inbit]; |
230 | } | 235 | } |
231 | } | 236 | } |
232 | memcpy (lastrow, rowbuf, bytes_per_row); | 237 | memcpy (lastrow, rowbuf, bytes_per_row); |
233 | } else if (((flags & PALM_IS_COMPRESSED_FLAG) && | 238 | } else if (((flags & PALM_IS_COMPRESSED_FLAG) && |
234 | (compression_type == PALM_COMPRESSION_NONE)) || | 239 | (compression_type == PALM_COMPRESSION_NONE)) || |
235 | (flags && PALM_IS_COMPRESSED_FLAG) == 0) { | 240 | ((flags & PALM_IS_COMPRESSED_FLAG) == 0)) |
241 | { | ||
242 | memcpy (rowbuf, palm_ptr, bytes_per_row); | ||
243 | palm_ptr += bytes_per_row; | ||
244 | } | ||
245 | else { | ||
246 | qDebug("Case 4"); | ||
247 | qDebug("Is compressed:%s", ((flags & PALM_IS_COMPRESSED_FLAG) == 0) ? "false" : "true"); | ||
248 | qDebug("Has colourmap:%s", ((flags & PALM_HAS_COLORMAP_FLAG) == 0) ? "false" : "true"); | ||
249 | qDebug("Has transparency:%s", ((flags & PALM_HAS_TRANSPARENCY_FLAG) == 0) ? "false" : "true"); | ||
250 | qDebug("Direct colour:%s", ((flags & PALM_DIRECT_COLOR_FLAG) == 0) ? "false" : "true"); | ||
251 | qDebug("four byte field:%s", ((flags & PALM_4_BYTE_FIELD_FLAG) == 0) ? "false" : "true"); | ||
236 | memcpy (rowbuf, palm_ptr, bytes_per_row); | 252 | memcpy (rowbuf, palm_ptr, bytes_per_row); |
237 | palm_ptr += bytes_per_row; | 253 | palm_ptr += bytes_per_row; |
238 | } | 254 | } |
239 | |||
240 | /* next, write it to the GDK bitmap */ | 255 | /* next, write it to the GDK bitmap */ |
241 | if (colormap) { | 256 | if (colormap) { |
242 | mask = (1 << bits_per_pixel) - 1; | 257 | mask = (1 << bits_per_pixel) - 1; |
243 | for (inbit = 8 - bits_per_pixel, inbyte = rowbuf, j = 0; j < width; ++j) { | 258 | for (inbit = 8 - bits_per_pixel, inbyte = rowbuf, j = 0; j < width; ++j) { |
244 | inval = ((*inbyte) & (mask << inbit)) >> inbit; | 259 | inval = ((*inbyte) & (mask << inbit)) >> inbit; |
245 | /* correct for oddity of the 8-bit color Palm pixmap... */ | 260 | /* correct for oddity of the 8-bit color Palm pixmap... */ |
246 | if ((bits_per_pixel == 8) && (inval == 0xFF)) inval = 231; | 261 | if ((bits_per_pixel == 8) && (inval == 0xFF)) inval = 231; |
247 | /* now lookup the correct color and set the pixel in the GTK bitmap */ | 262 | /* now lookup the correct color and set the pixel in the GTK bitmap */ |
248 | QRgb colour = qRgb(colormap[inval].red, colormap[inval].green, colormap[inval].blue); | 263 | QRgb colour = qRgb(colormap[inval].red, colormap[inval].green, colormap[inval].blue); |
249 | qimage->setPixel(j, i, colour); | 264 | qimage->setPixel(j, i, colour); |
250 | if (!inbit) { | 265 | if (!inbit) { |
251 | ++inbyte; | 266 | ++inbyte; |
252 | inbit = 8 - bits_per_pixel; | 267 | inbit = 8 - bits_per_pixel; |
253 | } else { | 268 | } else { |
254 | inbit -= bits_per_pixel; | 269 | inbit -= bits_per_pixel; |
255 | } | 270 | } |
256 | } | 271 | } |
257 | } else if (!colormap && | 272 | } else if (!colormap && |
258 | bits_per_pixel == 16) { | 273 | bits_per_pixel == 16) { |
259 | for (inbyte = rowbuf, j = 0; j < width; ++j) { | 274 | for (inbyte = rowbuf, j = 0; j < width; ++j) { |
260 | inval = (inbyte[0] << 8) | inbyte[1]; | 275 | inval = ((unsigned short)inbyte[0] << (unsigned short)8) | inbyte[1]; |
261 | #if 0 | 276 | |
262 | qDebug ("pixel is %d,%d (%02x:%02x:%02x)", | 277 | /* |
278 | qDebug ("pixel is %d,%d (%d:%d:%d)", | ||
263 | j, i, | 279 | j, i, |
264 | (inval >> (bits_per_pixel - palm_red_bits)) & ((1 << palm_red_bits) - 1), | 280 | ((inval >> (bits_per_pixel - palm_red_bits)) & ((1 << palm_red_bits) - 1)) << (8-palm_red_bits), |
265 | (inval >> palm_blue_bits) & ((1 << palm_green_bits) - 1), | 281 | ((inval >> palm_blue_bits) & ((1 << palm_green_bits) - 1)) << (8-palm_green_bits), |
266 | (inval >> 0) & ((1 << palm_blue_bits) - 1)); | 282 | ((inval >> 0) & ((1 << palm_blue_bits) - 1)) << (8-palm_blue_bits)); |
267 | #endif | 283 | */ |
268 | QRgb colour = qRgb( | 284 | QRgb colour = qRgb( |
269 | (inval >> (bits_per_pixel - palm_red_bits)) & ((1 << palm_red_bits) - 1), | 285 | ((inval >> (bits_per_pixel - palm_red_bits)) & ((1 << palm_red_bits) - 1)) << (8-palm_red_bits), |
270 | (inval >> palm_blue_bits) & ((1 << palm_green_bits) - 1), | 286 | ((inval >> palm_blue_bits) & ((1 << palm_green_bits) - 1)) << (8-palm_green_bits), |
271 | (inval >> 0) & ((1 << palm_blue_bits) - 1)); | 287 | ((inval >> 0) & ((1 << palm_blue_bits) - 1)) << (8-palm_blue_bits)); |
272 | qimage->setPixel(j, i, colour); | 288 | qimage->setPixel(j, i, colour); |
273 | inbyte += 2; | 289 | inbyte += 2; |
274 | } | 290 | } |
275 | } | 291 | } |
276 | } | 292 | } |
277 | 293 | ||
278 | delete [] rowbuf; | 294 | delete [] rowbuf; |
279 | delete [] lastrow; | 295 | delete [] lastrow; |
280 | 296 | ||
281 | return qimage; | 297 | return qimage; |
282 | } | 298 | } |
283 | 299 | ||
284 | QPixmap* hRule(int w, int h, unsigned char r, unsigned char g, unsigned char b) | 300 | QImage* hRule(int w, int h, unsigned char r, unsigned char g, unsigned char b) |
285 | { | 301 | { |
286 | // qDebug("hrule [%d, %d]", w, h); | 302 | //// qDebug("hrule [%d, %d]", w, h); |
287 | QPixmap* qimage = new QPixmap(w, h); | 303 | QPixmap* qimage = new QPixmap(w, h); |
288 | qimage->fill(QColor(r,g,b)); | 304 | qimage->fill(QColor(r,g,b)); |
289 | return qimage; | 305 | QImage* ret = new QImage(qimage->convertToImage()); |
306 | delete qimage; | ||
307 | return ret; | ||
290 | } | 308 | } |