2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read URT RLE Image Format %
20 % Copyright 1999-2018 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 % https://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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/colormap-private.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/pixel.h"
59 #include "MagickCore/property.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % IsRLE() returns MagickTrue if the image format type, identified by the
77 % magick string, is RLE.
79 % The format of the ReadRLEImage method is:
81 % MagickBooleanType IsRLE(const unsigned char *magick,const size_t length)
83 % A description of each parameter follows:
85 % o magick: compare image format pattern against these bytes.
87 % o length: Specifies the length of the magick string.
91 static MagickBooleanType IsRLE(const unsigned char *magick,const size_t length)
95 if (memcmp(magick,"\122\314",2) == 0)
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % R e a d R L E I m a g e %
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % ReadRLEImage() reads a run-length encoded Utah Raster Toolkit
112 % image file and returns it. It allocates the memory necessary for the new
113 % Image structure and returns a pointer to the new image.
115 % The format of the ReadRLEImage method is:
117 % Image *ReadRLEImage(const ImageInfo *image_info,ExceptionInfo *exception)
119 % A description of each parameter follows:
121 % o image_info: the image info.
123 % o exception: return any errors or warnings in this structure.
127 static Image *ReadRLEImage(const ImageInfo *image_info,ExceptionInfo *exception)
129 #define SkipLinesOp 0x01
130 #define SetColorOp 0x02
131 #define SkipPixelsOp 0x03
132 #define ByteDataOp 0x05
133 #define RunDataOp 0x06
135 #define ThrowRLEException(exception,message) \
137 if (colormap != (unsigned char *) NULL) \
138 colormap=(unsigned char *) RelinquishMagickMemory(colormap); \
139 if (pixel_info != (MemoryInfo *) NULL) \
140 pixel_info=RelinquishVirtualMemory(pixel_info); \
141 ThrowReaderException((exception),(message)); \
176 register unsigned char
184 number_planes_filled,
194 background_color[256],
203 assert(image_info != (const ImageInfo *) NULL);
204 assert(image_info->signature == MagickCoreSignature);
205 if (image_info->debug != MagickFalse)
206 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
207 image_info->filename);
208 assert(exception != (ExceptionInfo *) NULL);
209 assert(exception->signature == MagickCoreSignature);
210 image=AcquireImage(image_info,exception);
211 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
212 if (status == MagickFalse)
213 return(DestroyImageList(image));
215 Determine if this a RLE file.
217 colormap=(unsigned char *) NULL;
218 pixel_info=(MemoryInfo *) NULL;
219 count=ReadBlob(image,2,(unsigned char *) magick);
220 if ((count != 2) || (memcmp(magick,"\122\314",2) != 0))
221 ThrowRLEException(CorruptImageError,"ImproperImageHeader");
227 image->page.x=(ssize_t) ReadBlobLSBShort(image);
228 image->page.y=(ssize_t) ReadBlobLSBShort(image);
229 image->columns=ReadBlobLSBShort(image);
230 image->rows=ReadBlobLSBShort(image);
231 flags=(MagickStatusType) ReadBlobByte(image);
232 image->alpha_trait=flags & 0x04 ? BlendPixelTrait : UndefinedPixelTrait;
233 number_planes=(size_t) ReadBlobByte(image);
234 bits_per_pixel=(size_t) ReadBlobByte(image);
235 number_colormaps=(size_t) ReadBlobByte(image);
236 map_length=(unsigned char) ReadBlobByte(image);
237 if (map_length >= 22)
238 ThrowRLEException(CorruptImageError,"ImproperImageHeader");
239 if (EOFBlob(image) != MagickFalse)
240 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
242 map_length=one << map_length;
243 if ((number_planes == 0) || (number_planes == 2) || ((flags & 0x04) &&
244 ((number_planes <= 2) || number_planes > 254)) || (bits_per_pixel != 8))
245 ThrowRLEException(CorruptImageError,"ImproperImageHeader");
246 if (number_planes > 4)
247 ThrowRLEException(CorruptImageError,"ImproperImageHeader");
248 if ((image->columns == 0) || (image->columns >= 32768) ||
249 (image->rows == 0) || (image->rows >= 32768))
250 ThrowRLEException(CorruptImageError,"ImproperImageHeader");
254 No background color-- initialize to black.
256 for (i=0; i < (ssize_t) number_planes; i++)
257 background_color[i]=0;
258 (void) ReadBlobByte(image);
263 Initialize background color.
266 for (i=0; i < (ssize_t) number_planes; i++)
267 *p++=(unsigned char) ReadBlobByte(image);
269 if ((number_planes & 0x01) == 0)
270 (void) ReadBlobByte(image);
271 if (EOFBlob(image) != MagickFalse)
272 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
273 if (image->alpha_trait != UndefinedPixelTrait)
275 number_pixels=(MagickSizeType) image->columns*image->rows;
276 if ((GetBlobSize(image) == 0) || ((((MagickSizeType) number_pixels*
277 number_planes*bits_per_pixel/8)/GetBlobSize(image)) > 254))
278 ThrowRLEException(CorruptImageError,"InsufficientImageDataInFile")
279 if (((MagickSizeType) number_colormaps*map_length) > GetBlobSize(image))
280 ThrowRLEException(CorruptImageError,"InsufficientImageDataInFile")
281 if (number_colormaps != 0)
284 Read image colormaps.
286 colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
287 3*map_length*sizeof(*colormap));
288 if (colormap == (unsigned char *) NULL)
289 ThrowRLEException(ResourceLimitError,"MemoryAllocationFailed");
290 (void) memset(colormap,0,number_colormaps*3*map_length*
293 for (i=0; i < (ssize_t) number_colormaps; i++)
294 for (x=0; x < (ssize_t) map_length; x++)
296 *p++=(unsigned char) ScaleQuantumToChar(ScaleShortToQuantum(
297 ReadBlobLSBShort(image)));
298 if (EOFBlob(image) != MagickFalse)
299 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
302 if ((flags & 0x08) != 0)
313 length=ReadBlobLSBShort(image);
316 comment=(char *) AcquireQuantumMemory(length,sizeof(*comment));
317 if (comment == (char *) NULL)
318 ThrowRLEException(ResourceLimitError,"MemoryAllocationFailed");
319 count=ReadBlob(image,length-1,(unsigned char *) comment);
320 if (count != (ssize_t) (length-1))
322 comment=DestroyString(comment);
323 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
325 comment[length-1]='\0';
326 (void) SetImageProperty(image,"comment",comment,exception);
327 comment=DestroyString(comment);
328 if ((length & 0x01) == 0)
329 (void) ReadBlobByte(image);
332 if (EOFBlob(image) != MagickFalse)
333 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
334 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
335 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
337 status=SetImageExtent(image,image->columns,image->rows,exception);
338 if (status == MagickFalse)
340 if (colormap != (unsigned char *) NULL)
341 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
342 if (pixel_info != (MemoryInfo *) NULL)
343 pixel_info=RelinquishVirtualMemory(pixel_info);
344 return(DestroyImageList(image));
349 number_planes_filled=(number_planes % 2 == 0) ? number_planes :
351 if ((number_pixels*number_planes_filled) != (size_t) (number_pixels*
352 number_planes_filled))
353 ThrowRLEException(ResourceLimitError,"MemoryAllocationFailed");
354 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
355 MagickMax(number_planes_filled,4)*sizeof(*pixels));
356 if (pixel_info == (MemoryInfo *) NULL)
357 ThrowRLEException(ResourceLimitError,"MemoryAllocationFailed");
358 pixel_info_length=image->columns*image->rows*
359 MagickMax(number_planes_filled,4);
360 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
361 (void) memset(pixels,0,pixel_info_length);
362 if ((flags & 0x01) && !(flags & 0x02))
368 Set background color.
371 for (i=0; i < (ssize_t) number_pixels; i++)
373 if (image->alpha_trait == UndefinedPixelTrait)
374 for (j=0; j < (ssize_t) number_planes; j++)
375 *p++=background_color[j];
378 for (j=0; j < (ssize_t) (number_planes-1); j++)
379 *p++=background_color[j];
380 *p++=0; /* initialize matte channel */
385 Read runlength-encoded image.
390 opcode=ReadBlobByte(image);
393 if (number_colormaps != 0)
394 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
395 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
399 switch (opcode & 0x3f)
403 operand=ReadBlobByte(image);
405 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
408 operand=ReadBlobLSBSignedShort(image);
410 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
418 operand=ReadBlobByte(image);
420 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
421 plane=(unsigned char) operand;
423 plane=(unsigned char) (number_planes-1);
429 operand=ReadBlobByte(image);
431 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
434 operand=ReadBlobLSBSignedShort(image);
436 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
443 operand=ReadBlobByte(image);
445 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
448 operand=ReadBlobLSBSignedShort(image);
450 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
452 offset=(ssize_t) (((image->rows-y-1)*image->columns*number_planes)+x*
453 number_planes+plane);
456 ((size_t) (offset+operand*number_planes) > pixel_info_length))
457 ThrowRLEException(CorruptImageError,"UnableToReadImageData");
459 for (i=0; i < (ssize_t) operand; i++)
461 pixel=(unsigned char) ReadBlobByte(image);
462 if ((y < (ssize_t) image->rows) &&
463 ((x+i) < (ssize_t) image->columns))
468 (void) ReadBlobByte(image);
474 operand=ReadBlobByte(image);
476 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
479 operand=ReadBlobLSBSignedShort(image);
481 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
483 pixel=(unsigned char) ReadBlobByte(image);
484 (void) ReadBlobByte(image);
485 offset=(ssize_t) (((image->rows-y-1)*image->columns*number_planes)+x*
486 number_planes+plane);
489 ((size_t) (offset+operand*number_planes) > pixel_info_length))
490 ThrowRLEException(CorruptImageError,"UnableToReadImageData");
492 for (i=0; i < (ssize_t) operand; i++)
494 if ((y < (ssize_t) image->rows) &&
495 ((x+i) < (ssize_t) image->columns))
505 opcode=ReadBlobByte(image);
507 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
508 } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
509 if (number_colormaps != 0)
515 Apply colormap affineation to image.
517 mask=(MagickStatusType) (map_length-1);
519 x=(ssize_t) number_planes;
520 if (number_colormaps == 1)
521 for (i=0; i < (ssize_t) number_pixels; i++)
523 ValidateColormapValue(image,(ssize_t) (*p & mask),&index,exception);
524 *p=colormap[(ssize_t) index];
528 if ((number_planes >= 3) && (number_colormaps >= 3))
529 for (i=0; i < (ssize_t) number_pixels; i++)
530 for (x=0; x < (ssize_t) number_planes; x++)
532 ValidateColormapValue(image,(ssize_t) (x*map_length+
533 (*p & mask)),&index,exception);
534 *p=colormap[(ssize_t) index];
537 if ((i < (ssize_t) number_pixels) || (x < (ssize_t) number_planes))
538 ThrowRLEException(CorruptImageError,"UnableToReadImageData");
541 Initialize image structure.
543 if (number_planes >= 3)
546 Convert raster image to DirectClass pixel packets.
549 for (y=0; y < (ssize_t) image->rows; y++)
551 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
552 if (q == (Quantum *) NULL)
554 for (x=0; x < (ssize_t) image->columns; x++)
556 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
557 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
558 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
559 if (image->alpha_trait != UndefinedPixelTrait)
560 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
561 q+=GetPixelChannels(image);
563 if (SyncAuthenticPixels(image,exception) == MagickFalse)
565 if (image->previous == (Image *) NULL)
567 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
569 if (status == MagickFalse)
579 if (number_colormaps == 0)
581 if (AcquireImageColormap(image,map_length,exception) == MagickFalse)
582 ThrowRLEException(ResourceLimitError,"MemoryAllocationFailed");
584 if (number_colormaps == 1)
585 for (i=0; i < (ssize_t) image->colors; i++)
590 image->colormap[i].red=(MagickRealType)
591 ScaleCharToQuantum((unsigned char) i);
592 image->colormap[i].green=(MagickRealType)
593 ScaleCharToQuantum((unsigned char) i);
594 image->colormap[i].blue=(MagickRealType)
595 ScaleCharToQuantum((unsigned char) i);
598 if (number_colormaps > 1)
599 for (i=0; i < (ssize_t) image->colors; i++)
601 image->colormap[i].red=(MagickRealType)
602 ScaleCharToQuantum(*p);
603 image->colormap[i].green=(MagickRealType)
604 ScaleCharToQuantum(*(p+map_length));
605 image->colormap[i].blue=(MagickRealType)
606 ScaleCharToQuantum(*(p+map_length*2));
610 if (image->alpha_trait == UndefinedPixelTrait)
613 Convert raster image to PseudoClass pixel packets.
615 for (y=0; y < (ssize_t) image->rows; y++)
617 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
618 if (q == (Quantum *) NULL)
620 for (x=0; x < (ssize_t) image->columns; x++)
622 SetPixelIndex(image,(Quantum) *p++,q);
623 q+=GetPixelChannels(image);
625 if (SyncAuthenticPixels(image,exception) == MagickFalse)
627 if (image->previous == (Image *) NULL)
629 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
631 if (status == MagickFalse)
635 (void) SyncImage(image,exception);
640 Image has a matte channel-- promote to DirectClass.
642 for (y=0; y < (ssize_t) image->rows; y++)
644 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
645 if (q == (Quantum *) NULL)
647 for (x=0; x < (ssize_t) image->columns; x++)
649 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
650 SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t)
652 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
653 SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t)
655 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
656 SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t)
658 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
659 q+=GetPixelChannels(image);
661 if (x < (ssize_t) image->columns)
663 if (SyncAuthenticPixels(image,exception) == MagickFalse)
665 if (image->previous == (Image *) NULL)
667 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
669 if (status == MagickFalse)
673 image->colormap=(PixelInfo *) RelinquishMagickMemory(
675 image->storage_class=DirectClass;
679 if (number_colormaps != 0)
680 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
681 pixel_info=RelinquishVirtualMemory(pixel_info);
682 if (EOFBlob(image) != MagickFalse)
684 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
689 Proceed to next image.
691 if (image_info->number_scenes != 0)
692 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
694 (void) ReadBlobByte(image);
695 count=ReadBlob(image,2,(unsigned char *) magick);
696 if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
699 Allocate next image structure.
701 AcquireNextImage(image_info,image,exception);
702 if (GetNextImageInList(image) == (Image *) NULL)
707 image=SyncNextImageInList(image);
708 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
710 if (status == MagickFalse)
713 } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
714 (void) CloseBlob(image);
715 if (status == MagickFalse)
716 return(DestroyImageList(image));
717 return(GetFirstImageInList(image));
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 % R e g i s t e r R L E I m a g e %
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % RegisterRLEImage() adds attributes for the RLE image format to
732 % the list of supported formats. The attributes include the image format
733 % tag, a method to read and/or write the format, whether the format
734 % supports the saving of more than one frame to the same file or blob,
735 % whether the format supports native in-memory I/O, and a brief
736 % description of the format.
738 % The format of the RegisterRLEImage method is:
740 % size_t RegisterRLEImage(void)
743 ModuleExport size_t RegisterRLEImage(void)
748 entry=AcquireMagickInfo("RLE","RLE","Utah Run length encoded image");
749 entry->decoder=(DecodeImageHandler *) ReadRLEImage;
750 entry->magick=(IsImageFormatHandler *) IsRLE;
751 entry->flags|=CoderDecoderSeekableStreamFlag;
752 entry->flags^=CoderAdjoinFlag;
753 (void) RegisterMagickInfo(entry);
754 return(MagickImageCoderSignature);
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % U n r e g i s t e r R L E I m a g e %
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % UnregisterRLEImage() removes format registrations made by the
769 % RLE module from the list of supported formats.
771 % The format of the UnregisterRLEImage method is:
773 % UnregisterRLEImage(void)
776 ModuleExport void UnregisterRLEImage(void)
778 (void) UnregisterMagickInfo("RLE");