2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read URT RLE Image Format %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colormap.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/pixel.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % IsRLE() returns MagickTrue if the image format type, identified by the
76 % magick string, is RLE.
78 % The format of the ReadRLEImage method is:
80 % MagickBooleanType IsRLE(const unsigned char *magick,const size_t length)
82 % A description of each parameter follows:
84 % o magick: compare image format pattern against these bytes.
86 % o length: Specifies the length of the magick string.
90 static MagickBooleanType IsRLE(const unsigned char *magick,const size_t length)
94 if (memcmp(magick,"\122\314",2) == 0)
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % R e a d R L E I m a g e %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % ReadRLEImage() reads a run-length encoded Utah Raster Toolkit
111 % image file and returns it. It allocates the memory necessary for the new
112 % Image structure and returns a pointer to the new image.
114 % The format of the ReadRLEImage method is:
116 % Image *ReadRLEImage(const ImageInfo *image_info,ExceptionInfo *exception)
118 % A description of each parameter follows:
120 % o image_info: the image info.
122 % o exception: return any errors or warnings in this structure.
126 static Image *ReadRLEImage(const ImageInfo *image_info,ExceptionInfo *exception)
128 #define SkipLinesOp 0x01
129 #define SetColorOp 0x02
130 #define SkipPixelsOp 0x03
131 #define ByteDataOp 0x05
132 #define RunDataOp 0x06
164 register unsigned char
179 background_color[256],
188 assert(image_info != (const ImageInfo *) NULL);
189 assert(image_info->signature == MagickSignature);
190 if (image_info->debug != MagickFalse)
191 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
192 image_info->filename);
193 assert(exception != (ExceptionInfo *) NULL);
194 assert(exception->signature == MagickSignature);
195 image=AcquireImage(image_info,exception);
196 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
197 if (status == MagickFalse)
199 image=DestroyImageList(image);
200 return((Image *) NULL);
203 Determine if this a RLE file.
205 count=ReadBlob(image,2,(unsigned char *) magick);
206 if ((count == 0) || (memcmp(magick,"\122\314",2) != 0))
207 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
213 (void) ReadBlobLSBShort(image);
214 (void) ReadBlobLSBShort(image);
215 image->columns=ReadBlobLSBShort(image);
216 image->rows=ReadBlobLSBShort(image);
217 flags=(MagickStatusType) ReadBlobByte(image);
218 image->alpha_trait=flags & 0x04 ? BlendPixelTrait : UndefinedPixelTrait;
219 number_planes=1UL*ReadBlobByte(image);
220 bits_per_pixel=1UL*ReadBlobByte(image);
221 number_colormaps=1UL*ReadBlobByte(image);
222 map_length=(unsigned char) ReadBlobByte(image);
223 if (map_length >= 64)
224 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
226 map_length=one << map_length;
227 if ((number_planes == 0) || (number_planes == 2) || (bits_per_pixel != 8) ||
228 (image->columns == 0))
229 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
233 No background color-- initialize to black.
235 for (i=0; i < (ssize_t) number_planes; i++)
236 background_color[i]=0;
237 (void) ReadBlobByte(image);
242 Initialize background color.
245 for (i=0; i < (ssize_t) number_planes; i++)
246 *p++=(unsigned char) ReadBlobByte(image);
248 if ((number_planes & 0x01) == 0)
249 (void) ReadBlobByte(image);
250 colormap=(unsigned char *) NULL;
251 if (number_colormaps != 0)
254 Read image colormaps.
256 colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
257 map_length*sizeof(*colormap));
258 if (colormap == (unsigned char *) NULL)
259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
261 for (i=0; i < (ssize_t) number_colormaps; i++)
262 for (x=0; x < (ssize_t) map_length; x++)
263 *p++=(unsigned char) ScaleShortToQuantum(ReadBlobLSBShort(image));
265 if ((flags & 0x08) != 0)
276 length=ReadBlobLSBShort(image);
279 comment=(char *) AcquireQuantumMemory(length,sizeof(*comment));
280 if (comment == (char *) NULL)
281 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
282 count=ReadBlob(image,length-1,(unsigned char *) comment);
283 comment[length-1]='\0';
284 (void) SetImageProperty(image,"comment",comment,exception);
285 comment=DestroyString(comment);
286 if ((length & 0x01) == 0)
287 (void) ReadBlobByte(image);
290 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
291 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
296 if (image->alpha_trait == BlendPixelTrait)
298 number_pixels=(MagickSizeType) image->columns*image->rows;
299 if ((number_pixels*number_planes) != (size_t) (number_pixels*number_planes))
300 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
301 pixel_info=AcquireVirtualMemory(image->columns,image->rows*number_planes*
303 if (pixel_info == (MemoryInfo *) NULL)
304 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
305 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
306 if ((flags & 0x01) && !(flags & 0x02))
312 Set background color.
315 for (i=0; i < (ssize_t) number_pixels; i++)
317 if (image->alpha_trait != BlendPixelTrait)
318 for (j=0; j < (ssize_t) number_planes; j++)
319 *p++=background_color[j];
322 for (j=0; j < (ssize_t) (number_planes-1); j++)
323 *p++=background_color[j];
324 *p++=0; /* initialize matte channel */
329 Read runlength-encoded image.
334 opcode=ReadBlobByte(image);
337 switch (opcode & 0x3f)
341 operand=ReadBlobByte(image);
343 operand=(int) ReadBlobLSBShort(image);
350 operand=ReadBlobByte(image);
351 plane=(unsigned char) operand;
353 plane=(unsigned char) (number_planes-1);
359 operand=ReadBlobByte(image);
361 operand=(int) ReadBlobLSBShort(image);
367 operand=ReadBlobByte(image);
369 operand=(int) ReadBlobLSBShort(image);
370 p=pixels+((image->rows-y-1)*image->columns*number_planes)+
371 x*number_planes+plane;
373 for (i=0; i < (ssize_t) operand; i++)
375 pixel=(unsigned char) ReadBlobByte(image);
376 if ((y < (ssize_t) image->rows) &&
377 ((x+i) < (ssize_t) image->columns))
382 (void) ReadBlobByte(image);
388 operand=ReadBlobByte(image);
390 operand=(int) ReadBlobLSBShort(image);
391 pixel=(unsigned char) ReadBlobByte(image);
392 (void) ReadBlobByte(image);
394 p=pixels+((image->rows-y-1)*image->columns*number_planes)+
395 x*number_planes+plane;
396 for (i=0; i < (ssize_t) operand; i++)
398 if ((y < (ssize_t) image->rows) &&
399 ((x+i) < (ssize_t) image->columns))
409 opcode=ReadBlobByte(image);
410 } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
411 if (number_colormaps != 0)
417 Apply colormap affineation to image.
419 mask=(MagickStatusType) (map_length-1);
421 if (number_colormaps == 1)
422 for (i=0; i < (ssize_t) number_pixels; i++)
424 *p=colormap[*p & mask];
428 if ((number_planes >= 3) && (number_colormaps >= 3))
429 for (i=0; i < (ssize_t) number_pixels; i++)
430 for (x=0; x < (ssize_t) number_planes; x++)
432 *p=colormap[x*map_length+(*p & mask)];
437 Initialize image structure.
439 if (number_planes >= 3)
442 Convert raster image to DirectClass pixel packets.
445 for (y=0; y < (ssize_t) image->rows; y++)
447 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
448 if (q == (Quantum *) NULL)
450 for (x=0; x < (ssize_t) image->columns; x++)
452 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
453 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
454 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
455 if (image->alpha_trait == BlendPixelTrait)
456 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
457 q+=GetPixelChannels(image);
459 if (SyncAuthenticPixels(image,exception) == MagickFalse)
461 if (image->previous == (Image *) NULL)
463 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
465 if (status == MagickFalse)
475 if (number_colormaps == 0)
477 if (AcquireImageColormap(image,map_length,exception) == MagickFalse)
478 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
480 if (number_colormaps == 1)
481 for (i=0; i < (ssize_t) image->colors; i++)
486 image->colormap[i].red=ScaleCharToQuantum((unsigned char) i);
487 image->colormap[i].green=ScaleCharToQuantum((unsigned char) i);
488 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) i);
491 if (number_colormaps > 1)
492 for (i=0; i < (ssize_t) image->colors; i++)
494 image->colormap[i].red=ScaleCharToQuantum(*p);
495 image->colormap[i].green=ScaleCharToQuantum(*(p+map_length));
496 image->colormap[i].blue=ScaleCharToQuantum(*(p+map_length*2));
500 if (image->alpha_trait != BlendPixelTrait)
503 Convert raster image to PseudoClass pixel packets.
505 for (y=0; y < (ssize_t) image->rows; y++)
507 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
508 if (q == (Quantum *) NULL)
510 for (x=0; x < (ssize_t) image->columns; x++)
512 SetPixelIndex(image,*p++,q);
513 q+=GetPixelChannels(image);
515 if (SyncAuthenticPixels(image,exception) == MagickFalse)
517 if (image->previous == (Image *) NULL)
519 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
521 if (status == MagickFalse)
525 (void) SyncImage(image,exception);
530 Image has a matte channel-- promote to DirectClass.
532 for (y=0; y < (ssize_t) image->rows; y++)
534 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
535 if (q == (Quantum *) NULL)
537 for (x=0; x < (ssize_t) image->columns; x++)
539 SetPixelRed(image,image->colormap[*p++].red,q);
540 SetPixelGreen(image,image->colormap[*p++].green,q);
541 SetPixelBlue(image,image->colormap[*p++].blue,q);
542 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
543 q+=GetPixelChannels(image);
545 if (SyncAuthenticPixels(image,exception) == MagickFalse)
547 if (image->previous == (Image *) NULL)
549 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
551 if (status == MagickFalse)
555 image->colormap=(PixelInfo *) RelinquishMagickMemory(
557 image->storage_class=DirectClass;
561 if (number_colormaps != 0)
562 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
563 pixel_info=RelinquishVirtualMemory(pixel_info);
564 if (EOFBlob(image) != MagickFalse)
566 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
571 Proceed to next image.
573 if (image_info->number_scenes != 0)
574 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
576 (void) ReadBlobByte(image);
577 count=ReadBlob(image,2,(unsigned char *) magick);
578 if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
581 Allocate next image structure.
583 AcquireNextImage(image_info,image,exception);
584 if (GetNextImageInList(image) == (Image *) NULL)
586 image=DestroyImageList(image);
587 return((Image *) NULL);
589 image=SyncNextImageInList(image);
590 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
592 if (status == MagickFalse)
595 } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
596 (void) CloseBlob(image);
597 return(GetFirstImageInList(image));
601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605 % R e g i s t e r R L E I m a g e %
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611 % RegisterRLEImage() adds attributes for the RLE image format to
612 % the list of supported formats. The attributes include the image format
613 % tag, a method to read and/or write the format, whether the format
614 % supports the saving of more than one frame to the same file or blob,
615 % whether the format supports native in-memory I/O, and a brief
616 % description of the format.
618 % The format of the RegisterRLEImage method is:
620 % size_t RegisterRLEImage(void)
623 ModuleExport size_t RegisterRLEImage(void)
628 entry=SetMagickInfo("RLE");
629 entry->decoder=(DecodeImageHandler *) ReadRLEImage;
630 entry->magick=(IsImageFormatHandler *) IsRLE;
631 entry->adjoin=MagickFalse;
632 entry->description=ConstantString("Utah Run length encoded image");
633 entry->module=ConstantString("RLE");
634 (void) RegisterMagickInfo(entry);
635 return(MagickImageCoderSignature);
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % U n r e g i s t e r R L E I m a g e %
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % UnregisterRLEImage() removes format registrations made by the
650 % RLE module from the list of supported formats.
652 % The format of the UnregisterRLEImage method is:
654 % UnregisterRLEImage(void)
657 ModuleExport void UnregisterRLEImage(void)
659 (void) UnregisterMagickInfo("RLE");