cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP AAA L M M % |
| 7 | % P P A A L MM MM % |
| 8 | % PPPP AAAAA L M M M % |
| 9 | % P A A L M M % |
| 10 | % P A A LLLLL M M % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Palm Pixmap. % |
| 14 | % % |
| 15 | % % |
| 16 | % Software Design % |
| 17 | % Christopher R. Hawks % |
| 18 | % December 2001 % |
| 19 | % % |
| 20 | % Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization % |
| 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 24 | % obtain a copy of the License at % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | % % |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 | % |
| 37 | % Based on pnmtopalm by Bill Janssen and ppmtobmp by Ian Goldberg. |
| 38 | % |
| 39 | */ |
| 40 | |
| 41 | /* |
| 42 | Include declarations. |
| 43 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/studio.h" |
| 45 | #include "MagickCore/blob.h" |
| 46 | #include "MagickCore/blob-private.h" |
| 47 | #include "MagickCore/cache.h" |
| 48 | #include "MagickCore/color.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colormap-private.h" |
| 51 | #include "MagickCore/color-private.h" |
| 52 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 53 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 54 | #include "MagickCore/constitute.h" |
| 55 | #include "MagickCore/exception.h" |
| 56 | #include "MagickCore/histogram.h" |
| 57 | #include "MagickCore/image.h" |
| 58 | #include "MagickCore/image-private.h" |
| 59 | #include "MagickCore/list.h" |
| 60 | #include "MagickCore/magick.h" |
| 61 | #include "MagickCore/memory_.h" |
| 62 | #include "MagickCore/monitor.h" |
| 63 | #include "MagickCore/monitor-private.h" |
| 64 | #include "MagickCore/paint.h" |
| 65 | #include "MagickCore/pixel-accessor.h" |
| 66 | #include "MagickCore/property.h" |
| 67 | #include "MagickCore/quantize.h" |
| 68 | #include "MagickCore/quantum-private.h" |
| 69 | #include "MagickCore/static.h" |
| 70 | #include "MagickCore/string_.h" |
| 71 | #include "MagickCore/module.h" |
| 72 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | Define declarations. |
| 76 | */ |
| 77 | #define PALM_IS_COMPRESSED_FLAG 0x8000 |
| 78 | #define PALM_HAS_COLORMAP_FLAG 0x4000 |
| 79 | #define PALM_HAS_FOUR_BYTE_FIELD 0x0200 |
| 80 | #define PALM_HAS_TRANSPARENCY_FLAG 0x2000 |
| 81 | #define PALM_IS_INDIRECT 0x1000 |
| 82 | #define PALM_IS_FOR_SCREEN 0x0800 |
| 83 | #define PALM_IS_DIRECT_COLOR 0x0400 |
| 84 | #define PALM_COMPRESSION_SCANLINE 0x00 |
| 85 | #define PALM_COMPRESSION_RLE 0x01 |
| 86 | #define PALM_COMPRESSION_NONE 0xFF |
| 87 | |
| 88 | /* |
| 89 | The 256 color system palette for Palm Computing Devices. |
| 90 | */ |
| 91 | static unsigned char |
| 92 | PalmPalette[256][3] = |
| 93 | { |
| 94 | {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255}, |
| 95 | {255, 51,255}, {255, 0,255}, {255, 255,204}, {255, 204,204}, |
| 96 | {255, 153,204}, {255, 102,204}, {255, 51,204}, {255, 0,204}, |
| 97 | {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153}, |
| 98 | {255, 51,153}, {255, 0,153}, {204, 255,255}, {204, 204,255}, |
| 99 | {204, 153,255}, {204, 102,255}, {204, 51,255}, {204, 0,255}, |
| 100 | {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204}, |
| 101 | {204, 51,204}, {204, 0,204}, {204, 255,153}, {204, 204,153}, |
| 102 | {204, 153,153}, {204, 102,153}, {204, 51,153}, {204, 0,153}, |
| 103 | {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255}, |
| 104 | {153, 51,255}, {153, 0,255}, {153, 255,204}, {153, 204,204}, |
| 105 | {153, 153,204}, {153, 102,204}, {153, 51,204}, {153, 0,204}, |
| 106 | {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153}, |
| 107 | {153, 51,153}, {153, 0,153}, {102, 255,255}, {102, 204,255}, |
| 108 | {102, 153,255}, {102, 102,255}, {102, 51,255}, {102, 0,255}, |
| 109 | {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204}, |
| 110 | {102, 51,204}, {102, 0,204}, {102, 255,153}, {102, 204,153}, |
| 111 | {102, 153,153}, {102, 102,153}, {102, 51,153}, {102, 0,153}, |
| 112 | { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255}, |
| 113 | { 51, 51,255}, { 51, 0,255}, { 51, 255,204}, { 51, 204,204}, |
| 114 | { 51, 153,204}, { 51, 102,204}, { 51, 51,204}, { 51, 0,204}, |
| 115 | { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153}, |
| 116 | { 51, 51,153}, { 51, 0,153}, { 0, 255,255}, { 0, 204,255}, |
| 117 | { 0, 153,255}, { 0, 102,255}, { 0, 51,255}, { 0, 0,255}, |
| 118 | { 0, 255,204}, { 0, 204,204}, { 0, 153,204}, { 0, 102,204}, |
| 119 | { 0, 51,204}, { 0, 0,204}, { 0, 255,153}, { 0, 204,153}, |
| 120 | { 0, 153,153}, { 0, 102,153}, { 0, 51,153}, { 0, 0,153}, |
| 121 | {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102}, |
| 122 | {255, 51,102}, {255, 0,102}, {255, 255, 51}, {255, 204, 51}, |
| 123 | {255, 153, 51}, {255, 102, 51}, {255, 51, 51}, {255, 0, 51}, |
| 124 | {255, 255, 0}, {255, 204, 0}, {255, 153, 0}, {255, 102, 0}, |
| 125 | {255, 51, 0}, {255, 0, 0}, {204, 255,102}, {204, 204,102}, |
| 126 | {204, 153,102}, {204, 102,102}, {204, 51,102}, {204, 0,102}, |
| 127 | {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51}, |
| 128 | {204, 51, 51}, {204, 0, 51}, {204, 255, 0}, {204, 204, 0}, |
| 129 | {204, 153, 0}, {204, 102, 0}, {204, 51, 0}, {204, 0, 0}, |
| 130 | {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102}, |
| 131 | {153, 51,102}, {153, 0,102}, {153, 255, 51}, {153, 204, 51}, |
| 132 | {153, 153, 51}, {153, 102, 51}, {153, 51, 51}, {153, 0, 51}, |
| 133 | {153, 255, 0}, {153, 204, 0}, {153, 153, 0}, {153, 102, 0}, |
| 134 | {153, 51, 0}, {153, 0, 0}, {102, 255,102}, {102, 204,102}, |
| 135 | {102, 153,102}, {102, 102,102}, {102, 51,102}, {102, 0,102}, |
| 136 | {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51}, |
| 137 | {102, 51, 51}, {102, 0, 51}, {102, 255, 0}, {102, 204, 0}, |
| 138 | {102, 153, 0}, {102, 102, 0}, {102, 51, 0}, {102, 0, 0}, |
| 139 | { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102}, |
| 140 | { 51, 51,102}, { 51, 0,102}, { 51, 255, 51}, { 51, 204, 51}, |
| 141 | { 51, 153, 51}, { 51, 102, 51}, { 51, 51, 51}, { 51, 0, 51}, |
| 142 | { 51, 255, 0}, { 51, 204, 0}, { 51, 153, 0}, { 51, 102, 0}, |
| 143 | { 51, 51, 0}, { 51, 0, 0}, { 0, 255,102}, { 0, 204,102}, |
| 144 | { 0, 153,102}, { 0, 102,102}, { 0, 51,102}, { 0, 0,102}, |
| 145 | { 0, 255, 51}, { 0, 204, 51}, { 0, 153, 51}, { 0, 102, 51}, |
| 146 | { 0, 51, 51}, { 0, 0, 51}, { 0, 255, 0}, { 0, 204, 0}, |
| 147 | { 0, 153, 0}, { 0, 102, 0}, { 0, 51, 0}, { 17, 17, 17}, |
| 148 | { 34, 34, 34}, { 68, 68, 68}, { 85, 85, 85}, {119, 119,119}, |
| 149 | {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221}, |
| 150 | {238, 238,238}, {192, 192,192}, {128, 0, 0}, {128, 0,128}, |
| 151 | { 0, 128, 0}, { 0, 128,128}, { 0, 0, 0}, { 0, 0, 0}, |
| 152 | { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, |
| 153 | { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, |
| 154 | { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, |
| 155 | { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, |
| 156 | { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, |
| 157 | { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0} |
| 158 | }; |
| 159 | |
| 160 | /* |
| 161 | Forward declarations. |
| 162 | */ |
| 163 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 164 | WritePALMImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 168 | % % |
| 169 | % % |
| 170 | % % |
| 171 | % F i n d C o l o r % |
| 172 | % % |
| 173 | % % |
| 174 | % % |
| 175 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 176 | % |
| 177 | % FindColor() returns the index of the matching entry from PalmPalette for a |
| 178 | % given PixelPacket. |
| 179 | % |
| 180 | % The format of the FindColor method is: |
| 181 | % |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 182 | % int FindColor(const Image *image,PixelPacket *pixel) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 183 | % |
| 184 | % A description of each parameter follows: |
| 185 | % |
| 186 | % o int: the index of the matching color or -1 if not found/ |
| 187 | % |
| 188 | % o pixel: a pointer to the PixelPacket to be matched. |
| 189 | % |
| 190 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 191 | static int FindColor(const Image *image,PixelPacket *packet) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 193 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | i; |
| 195 | |
| 196 | for (i=0; i < 256; i++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 197 | if (ScaleQuantumToChar(packet->red) == PalmPalette[i][0] && |
| 198 | ScaleQuantumToChar(packet->green) == PalmPalette[i][1] && |
| 199 | ScaleQuantumToChar(packet->blue) == PalmPalette[i][2]) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 200 | return(i); |
| 201 | return(-1); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 206 | % % |
| 207 | % % |
| 208 | % % |
| 209 | % R e a d P A L M I m a g e % |
| 210 | % % |
| 211 | % % |
| 212 | % % |
| 213 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 214 | % |
| 215 | % ReadPALMImage() reads an image of raw bites in LSB order and returns it. It |
| 216 | % allocates the memory necessary for the new Image structure and returns a |
| 217 | % pointer to the new image. |
| 218 | % |
| 219 | % The format of the ReadPALMImage method is: |
| 220 | % |
| 221 | % Image *ReadPALMImage(const ImageInfo *image_info, |
| 222 | % ExceptionInfo *exception) |
| 223 | % |
| 224 | % A description of each parameter follows: |
| 225 | % |
| 226 | % o image_info: Specifies a pointer to an ImageInfo structure. |
| 227 | % |
| 228 | % o exception: return any errors or warnings in this structure. |
| 229 | % |
| 230 | */ |
| 231 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 232 | static inline size_t MagickMax(const size_t x,const size_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 233 | { |
| 234 | if (x > y) |
| 235 | return(x); |
| 236 | return(y); |
| 237 | } |
| 238 | |
| 239 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
| 240 | { |
| 241 | if (x < y) |
| 242 | return(x); |
| 243 | return(y); |
| 244 | } |
| 245 | |
| 246 | static Image *ReadPALMImage(const ImageInfo *image_info, |
| 247 | ExceptionInfo *exception) |
| 248 | { |
| 249 | Image |
| 250 | *image; |
| 251 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 252 | MagickBooleanType |
| 253 | status; |
| 254 | |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 255 | MagickOffsetType |
| 256 | totalOffset, |
| 257 | seekNextDepth; |
| 258 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 259 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 260 | transpix; |
| 261 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 262 | Quantum |
| 263 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 265 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 266 | i, |
| 267 | x; |
| 268 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 269 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 270 | *q; |
| 271 | |
| 272 | ssize_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 273 | count, |
| 274 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 275 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 276 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | bytes_per_row, |
| 278 | flags, |
| 279 | bits_per_pixel, |
| 280 | version, |
| 281 | nextDepthOffset, |
| 282 | transparentIndex, |
| 283 | compressionType, |
| 284 | byte, |
| 285 | mask, |
| 286 | redbits, |
| 287 | greenbits, |
| 288 | bluebits, |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 289 | one, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 290 | pad, |
| 291 | size, |
| 292 | bit; |
| 293 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 294 | unsigned char |
| 295 | *lastrow, |
| 296 | *one_row, |
| 297 | *ptr; |
| 298 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 299 | unsigned short |
| 300 | color16; |
| 301 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | /* |
| 303 | Open image file. |
| 304 | */ |
| 305 | assert(image_info != (const ImageInfo *) NULL); |
| 306 | assert(image_info->signature == MagickSignature); |
| 307 | if (image_info->debug != MagickFalse) |
| 308 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 309 | image_info->filename); |
| 310 | assert(exception != (ExceptionInfo *) NULL); |
| 311 | assert(exception->signature == MagickSignature); |
| 312 | image=AcquireImage(image_info); |
| 313 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 314 | if (status == MagickFalse) |
| 315 | { |
| 316 | (void) DestroyImageList(image); |
| 317 | return((Image *) NULL); |
| 318 | } |
| 319 | totalOffset=0; |
| 320 | do |
| 321 | { |
| 322 | image->columns=ReadBlobMSBShort(image); |
| 323 | image->rows=ReadBlobMSBShort(image); |
| 324 | if (EOFBlob(image) != MagickFalse) |
| 325 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | if ((image->columns == 0) || (image->rows == 0)) |
| 327 | ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); |
| 328 | bytes_per_row=ReadBlobMSBShort(image); |
| 329 | flags=ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 330 | bits_per_pixel=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 331 | if (bits_per_pixel > 16) |
| 332 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 333 | version=(size_t) ReadBlobByte(image); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 334 | (void) version; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 335 | nextDepthOffset=(size_t) ReadBlobMSBShort(image); |
| 336 | transparentIndex=(size_t) ReadBlobByte(image); |
| 337 | compressionType=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | pad=ReadBlobMSBShort(image); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 339 | (void) pad; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 340 | /* |
| 341 | Initialize image colormap. |
| 342 | */ |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 343 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 344 | if ((bits_per_pixel < 16) && |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 345 | (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 347 | GetPixelInfo(image,&transpix); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 348 | if (bits_per_pixel == 16) /* Direct Color */ |
| 349 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 350 | redbits=(size_t) ReadBlobByte(image); /* # of bits of red */ |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 351 | (void) redbits; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 352 | greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */ |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 353 | (void) greenbits; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 354 | bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */ |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 355 | (void) bluebits; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 356 | ReadBlobByte(image); /* reserved by Palm */ |
| 357 | ReadBlobByte(image); /* reserved by Palm */ |
| 358 | transpix.red=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31); |
| 359 | transpix.green=(MagickRealType) (QuantumRange*ReadBlobByte(image)/63); |
| 360 | transpix.blue=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31); |
| 361 | } |
| 362 | if (bits_per_pixel == 8) |
| 363 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 364 | Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 365 | index; |
| 366 | |
| 367 | if (flags & PALM_HAS_COLORMAP_FLAG) |
| 368 | { |
| 369 | count=(ssize_t) ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 370 | for (i=0; i < (ssize_t) count; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | { |
| 372 | ReadBlobByte(image); |
| 373 | index=ConstrainColormapIndex(image,255-i); |
| 374 | image->colormap[(int) index].red= |
| 375 | ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 376 | image->colormap[(int) index].green= |
| 377 | ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 378 | image->colormap[(int) index].blue= |
| 379 | ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 380 | } |
| 381 | } |
| 382 | else |
| 383 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 384 | for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 385 | { |
| 386 | index=ConstrainColormapIndex(image,255-i); |
| 387 | image->colormap[(int) index].red= |
| 388 | ScaleCharToQuantum(PalmPalette[i][0]); |
| 389 | image->colormap[(int) index].green= |
| 390 | ScaleCharToQuantum(PalmPalette[i][1]); |
| 391 | image->colormap[(int) index].blue= |
| 392 | ScaleCharToQuantum(PalmPalette[i][2]); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | if (flags & PALM_IS_COMPRESSED_FLAG) |
| 397 | size=ReadBlobMSBShort(image); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 398 | (void) size; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 399 | image->storage_class=DirectClass; |
| 400 | if (bits_per_pixel < 16) |
| 401 | { |
| 402 | image->storage_class=PseudoClass; |
| 403 | image->depth=8; |
| 404 | } |
| 405 | one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row, |
| 406 | sizeof(*one_row)); |
| 407 | if (one_row == (unsigned char *) NULL) |
| 408 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 409 | lastrow=(unsigned char *) NULL; |
| 410 | if (compressionType == PALM_COMPRESSION_SCANLINE) { |
| 411 | lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row, |
| 412 | sizeof(*lastrow)); |
| 413 | if (lastrow == (unsigned char *) NULL) |
| 414 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 415 | } |
| 416 | mask=(1l << bits_per_pixel)-1; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 417 | for (y = 0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 418 | { |
| 419 | if ((flags & PALM_IS_COMPRESSED_FLAG) == 0) |
| 420 | { |
| 421 | /* TODO move out of loop! */ |
| 422 | image->compression=NoCompression; |
| 423 | count=ReadBlob(image,bytes_per_row,one_row); |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | if (compressionType == PALM_COMPRESSION_RLE) |
| 428 | { |
| 429 | /* TODO move out of loop! */ |
| 430 | image->compression=RLECompression; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 431 | for (i=0; i < (ssize_t) bytes_per_row; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 432 | { |
| 433 | count=(ssize_t) ReadBlobByte(image); |
| 434 | count=MagickMin(count,(ssize_t) bytes_per_row-i); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 435 | byte=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 436 | (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count); |
| 437 | i+=count; |
| 438 | } |
| 439 | } |
| 440 | else |
| 441 | if (compressionType == PALM_COMPRESSION_SCANLINE) |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 442 | { |
| 443 | size_t |
| 444 | one; |
| 445 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | /* TODO move out of loop! */ |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 447 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 448 | image->compression=FaxCompression; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 449 | for (i=0; i < (ssize_t) bytes_per_row; i+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 450 | { |
| 451 | count=(ssize_t) ReadBlobByte(image); |
| 452 | byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8); |
| 453 | for (bit=0; bit < byte; bit++) |
| 454 | { |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 455 | if ((y == 0) || (count & (one << (7 - bit)))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 456 | one_row[i+bit]=(unsigned char) ReadBlobByte(image); |
| 457 | else |
| 458 | one_row[i+bit]=lastrow[i+bit]; |
| 459 | } |
| 460 | } |
| 461 | (void) CopyMagickMemory(lastrow, one_row, bytes_per_row); |
| 462 | } |
| 463 | } |
| 464 | ptr=one_row; |
| 465 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 466 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 467 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 468 | if (bits_per_pixel == 16) |
| 469 | { |
| 470 | if (image->columns > (2*bytes_per_row)) |
| 471 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 472 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 473 | { |
| 474 | color16=(*ptr++ << 8); |
| 475 | color16|=(*ptr++); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 476 | SetPixelRed(image,(QuantumRange*((color16 >> 11) & 0x1f))/0x1f,q); |
| 477 | SetPixelGreen(image,(QuantumRange*((color16 >> 5) & 0x3f))/0x3f,q); |
| 478 | SetPixelBlue(image,(QuantumRange*((color16 >> 0) & 0x1f))/0x1f,q); |
| 479 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 480 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | bit=8-bits_per_pixel; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 486 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | { |
| 488 | if ((size_t) (ptr-one_row) >= bytes_per_row) |
| 489 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 490 | index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit)); |
| 491 | SetPixelIndex(image,index,q); |
| 492 | SetPixelPacket(image,image->colormap+(ssize_t) index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 493 | if (bit) |
| 494 | bit-=bits_per_pixel; |
| 495 | else |
| 496 | { |
| 497 | ptr++; |
| 498 | bit=8-bits_per_pixel; |
| 499 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 500 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 501 | } |
| 502 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 503 | break; |
| 504 | } |
| 505 | if (image->previous == (Image *) NULL) |
| 506 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 507 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 508 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | if (status == MagickFalse) |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | if (flags & PALM_HAS_TRANSPARENCY_FLAG) |
| 514 | { |
| 515 | if (bits_per_pixel != 16) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 516 | SetPixelInfoPacket(image,image->colormap+(mask-transparentIndex), |
| 517 | &transpix); |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 518 | (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha, |
| 519 | MagickFalse,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 520 | } |
| 521 | one_row=(unsigned char *) RelinquishMagickMemory(one_row); |
| 522 | if (compressionType == PALM_COMPRESSION_SCANLINE) |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 523 | lastrow=(unsigned char *) RelinquishMagickMemory(lastrow); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | /* |
| 525 | Proceed to next image. Copied from coders/pnm.c |
| 526 | */ |
| 527 | if (image_info->number_scenes != 0) |
| 528 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 529 | break; |
| 530 | if (nextDepthOffset != 0) |
| 531 | { |
| 532 | /* |
| 533 | Skip to next image. |
| 534 | */ |
| 535 | totalOffset+=(MagickOffsetType) (nextDepthOffset*4); |
| 536 | if (totalOffset >= (MagickOffsetType) GetBlobSize(image)) |
| 537 | { |
| 538 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET); |
| 543 | } |
| 544 | if (seekNextDepth != totalOffset) |
| 545 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 546 | /* |
| 547 | Allocate next image structure. Copied from coders/pnm.c |
| 548 | */ |
| 549 | AcquireNextImage(image_info,image); |
| 550 | if (GetNextImageInList(image) == (Image *) NULL) |
| 551 | { |
| 552 | (void) DestroyImageList(image); |
| 553 | return((Image *) NULL); |
| 554 | } |
| 555 | image=SyncNextImageInList(image); |
| 556 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 557 | GetBlobSize(image)); |
| 558 | if (status == MagickFalse) |
| 559 | break; |
| 560 | } |
| 561 | } while (nextDepthOffset != 0); |
| 562 | (void) CloseBlob(image); |
| 563 | return(GetFirstImageInList(image)); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 568 | % % |
| 569 | % % |
| 570 | % % |
| 571 | % R e g i s t e r P A L M I m a g e % |
| 572 | % % |
| 573 | % % |
| 574 | % % |
| 575 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 576 | % |
| 577 | % RegisterPALMImage() adds properties for the PALM image format to the list of |
| 578 | % supported formats. The properties include the image format tag, a method to |
| 579 | % read and/or write the format, whether the format supports the saving of more |
| 580 | % than one frame to the same file or blob, whether the format supports native |
| 581 | % in-memory I/O, and a brief description of the format. |
| 582 | % |
| 583 | % The format of the RegisterPALMImage method is: |
| 584 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 585 | % size_t RegisterPALMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | % |
| 587 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 588 | ModuleExport size_t RegisterPALMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 589 | { |
| 590 | MagickInfo |
| 591 | *entry; |
| 592 | |
| 593 | entry=SetMagickInfo("PALM"); |
| 594 | entry->decoder=(DecodeImageHandler *) ReadPALMImage; |
| 595 | entry->encoder=(EncodeImageHandler *) WritePALMImage; |
| 596 | entry->seekable_stream=MagickTrue; |
| 597 | entry->description=ConstantString("Palm pixmap"); |
| 598 | entry->module=ConstantString("PALM"); |
| 599 | (void) RegisterMagickInfo(entry); |
| 600 | return(MagickImageCoderSignature); |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 605 | % % |
| 606 | % % |
| 607 | % % |
| 608 | % U n r e g i s t e r P A L M I m a g e % |
| 609 | % % |
| 610 | % % |
| 611 | % % |
| 612 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 613 | % |
| 614 | % UnregisterPALMImage() removes format registrations made by the PALM |
| 615 | % module from the list of supported formats. |
| 616 | % |
| 617 | % The format of the UnregisterPALMImage method is: |
| 618 | % |
| 619 | % UnregisterPALMImage(void) |
| 620 | % |
| 621 | */ |
| 622 | ModuleExport void UnregisterPALMImage(void) |
| 623 | { |
| 624 | (void) UnregisterMagickInfo("PALM"); |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 629 | % % |
| 630 | % % |
| 631 | % % |
| 632 | % W r i t e P A L M I m a g e % |
| 633 | % % |
| 634 | % % |
| 635 | % % |
| 636 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 637 | % |
| 638 | % WritePALMImage() writes an image of raw bits in LSB order to a file. |
| 639 | % |
| 640 | % The format of the WritePALMImage method is: |
| 641 | % |
| 642 | % MagickBooleanType WritePALMImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 643 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 644 | % |
| 645 | % A description of each parameter follows. |
| 646 | % |
| 647 | % o image_info: Specifies a pointer to an ImageInfo structure. |
| 648 | % |
| 649 | % o image: A pointer to a Image structure. |
| 650 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 651 | % o exception: return any errors or warnings in this structure. |
| 652 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 653 | */ |
| 654 | static MagickBooleanType WritePALMImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 655 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 656 | { |
| 657 | int |
| 658 | y; |
| 659 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 660 | MagickBooleanType |
| 661 | status; |
| 662 | |
| 663 | MagickOffsetType |
| 664 | currentOffset, |
| 665 | offset, |
| 666 | scene; |
| 667 | |
| 668 | MagickSizeType |
| 669 | cc; |
| 670 | |
| 671 | PixelPacket |
| 672 | transpix; |
| 673 | |
| 674 | QuantizeInfo |
| 675 | *quantize_info; |
| 676 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 677 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 678 | x; |
| 679 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 680 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 681 | *p; |
| 682 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 683 | register Quantum |
| 684 | *q; |
| 685 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 686 | size_t |
| 687 | count, |
| 688 | bits_per_pixel, |
| 689 | bytes_per_row, |
| 690 | nextDepthOffset, |
| 691 | one; |
| 692 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | unsigned char |
| 694 | bit, |
| 695 | byte, |
| 696 | color, |
| 697 | *lastrow, |
| 698 | *one_row, |
| 699 | *ptr, |
| 700 | version; |
| 701 | |
| 702 | unsigned int |
| 703 | transparentIndex; |
| 704 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 705 | unsigned short |
| 706 | color16, |
| 707 | flags; |
| 708 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 709 | /* |
| 710 | Open output image file. |
| 711 | */ |
| 712 | assert(image_info != (const ImageInfo *) NULL); |
| 713 | assert(image_info->signature == MagickSignature); |
| 714 | assert(image != (Image *) NULL); |
| 715 | assert(image->signature == MagickSignature); |
| 716 | if (image->debug != MagickFalse) |
| 717 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 718 | assert(exception != (ExceptionInfo *) NULL); |
| 719 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 720 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 721 | if (status == MagickFalse) |
| 722 | return(status); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | quantize_info=AcquireQuantizeInfo(image_info); |
| 724 | flags=0; |
| 725 | currentOffset=0; |
| 726 | transparentIndex=0; |
| 727 | transpix.red=0; |
| 728 | transpix.green=0; |
| 729 | transpix.blue=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 730 | transpix.alpha=0; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 731 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 732 | version=0; |
| 733 | scene=0; |
| 734 | do |
| 735 | { |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 736 | if (IsRGBColorspace(image->colorspace) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 737 | (void) TransformImageColorspace(image,RGBColorspace); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 738 | count=GetNumberColors(image,NULL,exception); |
cristy | a7a9dbc | 2010-05-30 00:23:21 +0000 | [diff] [blame] | 739 | for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 740 | if (image_info->depth > 100) |
| 741 | bits_per_pixel=image_info->depth-100; |
| 742 | if (bits_per_pixel < 16) |
| 743 | (void) TransformImageColorspace(image,image->colorspace); |
| 744 | if (bits_per_pixel < 8) |
| 745 | { |
| 746 | (void) TransformImageColorspace(image,GRAYColorspace); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 747 | (void) SetImageType(image,PaletteType,exception); |
| 748 | (void) SortColormapByIntensity(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | } |
| 750 | if ((image->storage_class == PseudoClass) && (image->colors > 256)) |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 751 | (void) SetImageStorageClass(image,DirectClass,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 752 | if (image->storage_class == PseudoClass) |
| 753 | flags|=PALM_HAS_COLORMAP_FLAG; |
| 754 | else |
| 755 | flags|=PALM_IS_DIRECT_COLOR; |
| 756 | (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */ |
| 757 | (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */ |
| 758 | bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/ |
| 759 | bits_per_pixel))*2; |
| 760 | (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row); |
| 761 | if ((image_info->compression == RLECompression) || |
| 762 | (image_info->compression == FaxCompression)) |
| 763 | flags|=PALM_IS_COMPRESSED_FLAG; |
| 764 | (void) WriteBlobMSBShort(image, flags); |
| 765 | (void) WriteBlobByte(image,(unsigned char) bits_per_pixel); |
| 766 | if (bits_per_pixel > 1) |
| 767 | version=1; |
| 768 | if ((image_info->compression == RLECompression) || |
| 769 | (image_info->compression == FaxCompression)) |
| 770 | version=2; |
| 771 | (void) WriteBlobByte(image,version); |
| 772 | (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */ |
| 773 | (void) WriteBlobByte(image,(unsigned char) transparentIndex); |
| 774 | if (image_info->compression == RLECompression) |
| 775 | (void) WriteBlobByte(image,PALM_COMPRESSION_RLE); |
| 776 | else |
| 777 | if (image_info->compression == FaxCompression) |
| 778 | (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE); |
| 779 | else |
| 780 | (void) WriteBlobByte(image,PALM_COMPRESSION_NONE); |
| 781 | (void) WriteBlobMSBShort(image,0); /* reserved */ |
| 782 | offset=16; |
| 783 | if (bits_per_pixel == 16) |
| 784 | { |
| 785 | (void) WriteBlobByte(image,5); /* # of bits of red */ |
| 786 | (void) WriteBlobByte(image,6); /* # of bits of green */ |
| 787 | (void) WriteBlobByte(image,5); /* # of bits of blue */ |
| 788 | (void) WriteBlobByte(image,0); /* reserved by Palm */ |
| 789 | (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */ |
| 790 | offset+=8; |
| 791 | } |
| 792 | if (bits_per_pixel == 8) |
| 793 | { |
| 794 | if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */ |
| 795 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 796 | quantize_info->dither=IsPaletteImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | quantize_info->number_colors=image->colors; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 798 | (void) QuantizeImage(quantize_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | (void) WriteBlobMSBShort(image,(unsigned short) image->colors); |
| 800 | for (count = 0; count < image->colors; count++) |
| 801 | { |
| 802 | (void) WriteBlobByte(image,(unsigned char) count); |
| 803 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
| 804 | image->colormap[count].red)); |
| 805 | (void) WriteBlobByte(image, |
| 806 | ScaleQuantumToChar(image->colormap[count].green)); |
| 807 | (void) WriteBlobByte(image, |
| 808 | ScaleQuantumToChar(image->colormap[count].blue)); |
| 809 | } |
| 810 | offset+=2+count*4; |
| 811 | } |
| 812 | else /* Map colors to Palm standard colormap */ |
| 813 | { |
| 814 | Image |
| 815 | *affinity_image; |
| 816 | |
| 817 | affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 818 | exception); |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 819 | (void) TransformImageColorspace(affinity_image, |
| 820 | affinity_image->colorspace); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 821 | (void) RemapImage(quantize_info,image,affinity_image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 822 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 823 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 824 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 825 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 826 | { |
| 827 | SetPixelIndex(image,FindColor(image,&image->colormap[(ssize_t) |
| 828 | GetPixelIndex(image,q)]),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 829 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 830 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 831 | } |
| 832 | affinity_image=DestroyImage(affinity_image); |
| 833 | } |
| 834 | } |
| 835 | if (flags & PALM_IS_COMPRESSED_FLAG) |
| 836 | (void) WriteBlobMSBShort(image,0); /* fill in size later */ |
| 837 | lastrow=(unsigned char *) NULL; |
| 838 | if (image_info->compression == FaxCompression) |
| 839 | lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row, |
| 840 | sizeof(*lastrow)); |
| 841 | /* TODO check whether memory really was acquired? */ |
| 842 | one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row, |
| 843 | sizeof(*one_row)); |
| 844 | if (one_row == (unsigned char *) NULL) |
| 845 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 846 | for (y=0; y < (int) image->rows; y++) |
| 847 | { |
| 848 | ptr=one_row; |
| 849 | (void) ResetMagickMemory(ptr,0,bytes_per_row); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 850 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 851 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 852 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | if (bits_per_pixel == 16) |
| 854 | { |
| 855 | for (x=0; x < (int) image->columns; x++) |
| 856 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 857 | color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/ |
| 858 | (size_t) QuantumRange) << 11) | (((63*(size_t) |
| 859 | GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) | |
| 860 | ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange)); |
| 861 | if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 862 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 863 | transpix.red=GetPixelRed(image,p); |
| 864 | transpix.green=GetPixelGreen(image,p); |
| 865 | transpix.blue=GetPixelBlue(image,p); |
| 866 | transpix.alpha=GetPixelAlpha(image,p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 867 | flags|=PALM_HAS_TRANSPARENCY_FLAG; |
| 868 | } |
| 869 | *ptr++=(unsigned char) ((color16 >> 8) & 0xff); |
| 870 | *ptr++=(unsigned char) (color16 & 0xff); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 871 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | byte=0x00; |
| 877 | bit=(unsigned char) (8-bits_per_pixel); |
| 878 | for (x=0; x < (int) image->columns; x++) |
| 879 | { |
| 880 | if (bits_per_pixel >= 8) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 881 | color=(unsigned char) GetPixelIndex(image,p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 882 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 883 | color=(unsigned char) (GetPixelIndex(image,p)* |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 884 | ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 885 | byte|=color << bit; |
| 886 | if (bit != 0) |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 887 | bit-=(unsigned char) bits_per_pixel; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 888 | else |
| 889 | { |
| 890 | *ptr++=byte; |
| 891 | byte=0x00; |
| 892 | bit=(unsigned char) (8-bits_per_pixel); |
| 893 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 894 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 895 | } |
| 896 | if ((image->columns % (8/bits_per_pixel)) != 0) |
| 897 | *ptr++=byte; |
| 898 | } |
| 899 | if (image_info->compression == RLECompression) |
| 900 | { |
| 901 | x=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 902 | while (x < (ssize_t) bytes_per_row) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 903 | { |
| 904 | byte=one_row[x]; |
| 905 | count=1; |
| 906 | while ((one_row[++x] == byte) && (count < 255) && |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 907 | (x < (ssize_t) bytes_per_row)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | count++; |
| 909 | (void) WriteBlobByte(image,(unsigned char) count); |
| 910 | (void) WriteBlobByte(image,(unsigned char) byte); |
| 911 | } |
| 912 | } |
| 913 | else |
| 914 | if (image_info->compression == FaxCompression) |
| 915 | { |
| 916 | char |
| 917 | tmpbuf[8], |
| 918 | *tptr; |
| 919 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 920 | for (x = 0; x < (ssize_t) bytes_per_row; x += 8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | { |
| 922 | tptr = tmpbuf; |
| 923 | for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++) |
| 924 | { |
| 925 | if ((y == 0) || (lastrow[x + bit] != one_row[x + bit])) |
| 926 | { |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 927 | byte |= (1 << (7 - bit)); |
| 928 | *tptr++ = (char) one_row[x + bit]; |
| 929 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | } |
| 931 | (void) WriteBlobByte(image, byte); |
| 932 | (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf); |
| 933 | } |
| 934 | (void) CopyMagickMemory(lastrow,one_row,bytes_per_row); |
| 935 | } |
| 936 | else |
| 937 | (void) WriteBlob(image,bytes_per_row,one_row); |
| 938 | } |
| 939 | if (flags & PALM_HAS_TRANSPARENCY_FLAG) |
| 940 | { |
| 941 | offset=SeekBlob(image,currentOffset+6,SEEK_SET); |
| 942 | (void) WriteBlobMSBShort(image,flags); |
| 943 | offset=SeekBlob(image,currentOffset+12,SEEK_SET); |
| 944 | (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */ |
| 945 | } |
| 946 | if (bits_per_pixel == 16) |
| 947 | { |
| 948 | offset=SeekBlob(image,currentOffset+20,SEEK_SET); |
| 949 | (void) WriteBlobByte(image,0); /* reserved by Palm */ |
| 950 | (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange)); |
| 951 | (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange)); |
| 952 | (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange)); |
| 953 | } |
| 954 | if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */ |
| 955 | { |
| 956 | offset=SeekBlob(image,currentOffset+offset,SEEK_SET); |
| 957 | (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)- |
| 958 | currentOffset-offset)); |
| 959 | } |
| 960 | if (one_row != (unsigned char *) NULL) |
| 961 | one_row=(unsigned char *) RelinquishMagickMemory(one_row); |
| 962 | if (lastrow != (unsigned char *) NULL) |
| 963 | lastrow=(unsigned char *) RelinquishMagickMemory(lastrow); |
| 964 | if (GetNextImageInList(image) == (Image *) NULL) |
| 965 | break; |
| 966 | /* padding to 4 byte word */ |
| 967 | for (cc = (GetBlobSize(image))%4; cc > 0; cc--) |
| 968 | { |
| 969 | (void) WriteBlobByte(image,0); |
| 970 | } |
| 971 | /* write nextDepthOffset and return to end of image */ |
| 972 | offset=SeekBlob(image,currentOffset+10,SEEK_SET); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 973 | nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 974 | (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset); |
| 975 | currentOffset=(MagickOffsetType) GetBlobSize(image); |
| 976 | offset=SeekBlob(image,currentOffset,SEEK_SET); |
| 977 | image=SyncNextImageInList(image); |
| 978 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 979 | GetImageListLength(image)); |
| 980 | if (status == MagickFalse) |
| 981 | break; |
| 982 | } while (image_info->adjoin != MagickFalse); |
| 983 | quantize_info=DestroyQuantizeInfo(quantize_info); |
| 984 | (void) CloseBlob(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 985 | return(MagickTrue); |
| 986 | } |