cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP IIIII X X % |
| 7 | % P P I X X % |
| 8 | % PPPP I X % |
| 9 | % P I X X % |
| 10 | % P IIIII X X % |
| 11 | % % |
| 12 | % % |
| 13 | % Read Alias/Wavefront RLE Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 % |
| 24 | % obtain a copy of the License at % |
| 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 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/blob.h" |
| 44 | #include "MagickCore/blob-private.h" |
| 45 | #include "MagickCore/cache.h" |
| 46 | #include "MagickCore/colormap.h" |
| 47 | #include "MagickCore/exception.h" |
| 48 | #include "MagickCore/exception-private.h" |
| 49 | #include "MagickCore/image.h" |
| 50 | #include "MagickCore/image-private.h" |
| 51 | #include "MagickCore/list.h" |
| 52 | #include "MagickCore/magick.h" |
| 53 | #include "MagickCore/memory_.h" |
| 54 | #include "MagickCore/monitor.h" |
| 55 | #include "MagickCore/monitor-private.h" |
| 56 | #include "MagickCore/pixel-accessor.h" |
| 57 | #include "MagickCore/quantum-private.h" |
| 58 | #include "MagickCore/static.h" |
| 59 | #include "MagickCore/string_.h" |
| 60 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 64 | % % |
| 65 | % % |
| 66 | % % |
| 67 | % R e a d P I X I m a g e % |
| 68 | % % |
| 69 | % % |
| 70 | % % |
| 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | % |
| 73 | % ReadPIXImage() reads a Alias/Wavefront RLE image file and returns it. |
| 74 | % It allocates the memory necessary for the new Image structure and returns a |
| 75 | % pointer to the new image. |
| 76 | % |
| 77 | % The format of the ReadPIXImage method is: |
| 78 | % |
| 79 | % Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 80 | % |
| 81 | % A description of each parameter follows: |
| 82 | % |
| 83 | % o image_info: the image info. |
| 84 | % |
| 85 | % o exception: return any errors or warnings in this structure. |
| 86 | % |
| 87 | % |
| 88 | */ |
| 89 | static Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 90 | { |
| 91 | Image |
| 92 | *image; |
| 93 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 94 | MagickBooleanType |
| 95 | status; |
| 96 | |
| 97 | Quantum |
| 98 | blue, |
| 99 | green, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 100 | index, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 101 | red; |
| 102 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 103 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 104 | x; |
| 105 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 106 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 107 | *q; |
| 108 | |
| 109 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | bits_per_pixel, |
| 111 | height, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 112 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 113 | width; |
| 114 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 115 | ssize_t |
| 116 | y; |
| 117 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 118 | /* |
| 119 | Open image file. |
| 120 | */ |
| 121 | assert(image_info != (const ImageInfo *) NULL); |
| 122 | assert(image_info->signature == MagickSignature); |
| 123 | if (image_info->debug != MagickFalse) |
| 124 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 125 | image_info->filename); |
| 126 | assert(exception != (ExceptionInfo *) NULL); |
| 127 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 128 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 130 | if (status == MagickFalse) |
| 131 | { |
| 132 | image=DestroyImageList(image); |
| 133 | return((Image *) NULL); |
| 134 | } |
| 135 | /* |
| 136 | Read PIX image. |
| 137 | */ |
| 138 | width=ReadBlobMSBShort(image); |
| 139 | height=ReadBlobMSBShort(image); |
| 140 | (void) ReadBlobMSBShort(image); /* x-offset */ |
| 141 | (void) ReadBlobMSBShort(image); /* y-offset */ |
| 142 | bits_per_pixel=ReadBlobMSBShort(image); |
| 143 | if ((width == 0UL) || (height == 0UL) || ((bits_per_pixel != 8) && |
| 144 | (bits_per_pixel != 24))) |
| 145 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 146 | do |
| 147 | { |
| 148 | /* |
| 149 | Initialize image structure. |
| 150 | */ |
| 151 | image->columns=width; |
| 152 | image->rows=height; |
| 153 | if (bits_per_pixel == 8) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 154 | if (AcquireImageColormap(image,256,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 156 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 157 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 158 | break; |
| 159 | /* |
| 160 | Convert PIX raster image to pixel packets. |
| 161 | */ |
| 162 | red=(Quantum) 0; |
| 163 | green=(Quantum) 0; |
| 164 | blue=(Quantum) 0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 165 | index=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 166 | length=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 167 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 168 | { |
| 169 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 170 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 172 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | { |
| 174 | if (length == 0) |
| 175 | { |
| 176 | length=(size_t) ReadBlobByte(image); |
| 177 | if (bits_per_pixel == 8) |
| 178 | index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 179 | else |
| 180 | { |
| 181 | blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 182 | green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 183 | red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image)); |
| 184 | } |
| 185 | } |
| 186 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 187 | SetPixelIndex(image,index,q); |
| 188 | SetPixelBlue(image,blue,q); |
| 189 | SetPixelGreen(image,green,q); |
| 190 | SetPixelRed(image,red,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 191 | length--; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 192 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 193 | } |
| 194 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 195 | break; |
| 196 | if (image->previous == (Image *) NULL) |
| 197 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 198 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 199 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 200 | if (status == MagickFalse) |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | if (image->storage_class == PseudoClass) |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 205 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 206 | if (EOFBlob(image) != MagickFalse) |
| 207 | { |
| 208 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 209 | image->filename); |
| 210 | break; |
| 211 | } |
| 212 | /* |
| 213 | Proceed to next image. |
| 214 | */ |
| 215 | if (image_info->number_scenes != 0) |
| 216 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 217 | break; |
| 218 | width=ReadBlobMSBLong(image); |
| 219 | height=ReadBlobMSBLong(image); |
| 220 | (void) ReadBlobMSBShort(image); |
| 221 | (void) ReadBlobMSBShort(image); |
| 222 | bits_per_pixel=ReadBlobMSBShort(image); |
| 223 | status=(width != 0UL) && (height == 0UL) && ((bits_per_pixel == 8) || |
| 224 | (bits_per_pixel == 24)) ? MagickTrue : MagickFalse; |
| 225 | if (status == MagickTrue) |
| 226 | { |
| 227 | /* |
| 228 | Allocate next image structure. |
| 229 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 230 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 231 | if (GetNextImageInList(image) == (Image *) NULL) |
| 232 | { |
| 233 | image=DestroyImageList(image); |
| 234 | return((Image *) NULL); |
| 235 | } |
| 236 | image=SyncNextImageInList(image); |
| 237 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 238 | GetBlobSize(image)); |
| 239 | if (status == MagickFalse) |
| 240 | break; |
| 241 | } |
| 242 | } while (status == MagickTrue); |
| 243 | (void) CloseBlob(image); |
| 244 | return(GetFirstImageInList(image)); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 249 | % % |
| 250 | % % |
| 251 | % % |
| 252 | % R e g i s t e r P I X I m a g e % |
| 253 | % % |
| 254 | % % |
| 255 | % % |
| 256 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 257 | % |
| 258 | % RegisterPIXImage() adds attributes for the PIX image format to |
| 259 | % the list of supported formats. The attributes include the image format |
| 260 | % tag, a method to read and/or write the format, whether the format |
| 261 | % supports the saving of more than one frame to the same file or blob, |
| 262 | % whether the format supports native in-memory I/O, and a brief |
| 263 | % description of the format. |
| 264 | % |
| 265 | % The format of the RegisterPIXImage method is: |
| 266 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 267 | % size_t RegisterPIXImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 268 | % |
| 269 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 270 | ModuleExport size_t RegisterPIXImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | { |
| 272 | MagickInfo |
| 273 | *entry; |
| 274 | |
| 275 | entry=SetMagickInfo("PIX"); |
| 276 | entry->decoder=(DecodeImageHandler *) ReadPIXImage; |
| 277 | entry->description=ConstantString("Alias/Wavefront RLE image format"); |
| 278 | entry->module=ConstantString("PIX"); |
| 279 | (void) RegisterMagickInfo(entry); |
| 280 | return(MagickImageCoderSignature); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 285 | % % |
| 286 | % % |
| 287 | % % |
| 288 | % U n r e g i s t e r P I X I m a g e % |
| 289 | % % |
| 290 | % % |
| 291 | % % |
| 292 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 293 | % |
| 294 | % UnregisterPIXImage() removes format registrations made by the |
| 295 | % PIX module from the list of supported formats. |
| 296 | % |
| 297 | % The format of the UnregisterPIXImage method is: |
| 298 | % |
| 299 | % UnregisterPIXImage(void) |
| 300 | % |
| 301 | */ |
| 302 | ModuleExport void UnregisterPIXImage(void) |
| 303 | { |
| 304 | (void) UnregisterMagickInfo("PIX"); |
| 305 | } |