2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read URT RLE Image Format %
20 % Copyright 1999-2017 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 ThrowReaderException(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 ThrowReaderException(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 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
246 if ((image->columns == 0) || (image->columns >= 32768) ||
247 (image->rows == 0) || (image->rows >= 32768))
248 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
252 No background color-- initialize to black.
254 for (i=0; i < (ssize_t) number_planes; i++)
255 background_color[i]=0;
256 (void) ReadBlobByte(image);
261 Initialize background color.
264 for (i=0; i < (ssize_t) number_planes; i++)
265 *p++=(unsigned char) ReadBlobByte(image);
267 if ((number_planes & 0x01) == 0)
268 (void) ReadBlobByte(image);
269 if (EOFBlob(image) != MagickFalse)
270 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
271 if (image->alpha_trait != UndefinedPixelTrait)
273 number_pixels=(MagickSizeType) image->columns*image->rows;
274 if ((GetBlobSize(image) == 0) || ((((MagickSizeType) number_pixels*
275 number_planes*bits_per_pixel/8)/GetBlobSize(image)) > 254.0))
276 ThrowRLEException(CorruptImageError,"InsufficientImageDataInFile")
277 if (((MagickSizeType) number_colormaps*map_length) > GetBlobSize(image))
278 ThrowRLEException(CorruptImageError,"InsufficientImageDataInFile")
279 if (number_colormaps != 0)
282 Read image colormaps.
284 colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
285 3*map_length*sizeof(*colormap));
286 if (colormap == (unsigned char *) NULL)
287 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
289 for (i=0; i < (ssize_t) number_colormaps; i++)
290 for (x=0; x < (ssize_t) map_length; x++)
292 *p++=(unsigned char) ScaleQuantumToChar(ScaleShortToQuantum(
293 ReadBlobLSBShort(image)));
294 if (EOFBlob(image) != MagickFalse)
295 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
298 if ((flags & 0x08) != 0)
309 length=ReadBlobLSBShort(image);
312 comment=(char *) AcquireQuantumMemory(length,sizeof(*comment));
313 if (comment == (char *) NULL)
314 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
315 count=ReadBlob(image,length-1,(unsigned char *) comment);
316 comment[length-1]='\0';
317 (void) SetImageProperty(image,"comment",comment,exception);
318 comment=DestroyString(comment);
319 if ((length & 0x01) == 0)
320 (void) ReadBlobByte(image);
323 if (EOFBlob(image) != MagickFalse)
324 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
325 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
326 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
328 status=SetImageExtent(image,image->columns,image->rows,exception);
329 if (status == MagickFalse)
330 return(DestroyImageList(image));
334 number_planes_filled=(number_planes % 2 == 0) ? number_planes :
336 if ((number_pixels*number_planes_filled) != (size_t) (number_pixels*
337 number_planes_filled))
338 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
339 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
340 MagickMax(number_planes_filled,4)*sizeof(*pixels));
341 if (pixel_info == (MemoryInfo *) NULL)
342 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
343 pixel_info_length=image->columns*image->rows*
344 MagickMax(number_planes_filled,4);
345 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
346 (void) ResetMagickMemory(pixels,0,pixel_info_length);
347 if ((flags & 0x01) && !(flags & 0x02))
353 Set background color.
356 for (i=0; i < (ssize_t) number_pixels; i++)
358 if (image->alpha_trait == UndefinedPixelTrait)
359 for (j=0; j < (ssize_t) number_planes; j++)
360 *p++=background_color[j];
363 for (j=0; j < (ssize_t) (number_planes-1); j++)
364 *p++=background_color[j];
365 *p++=0; /* initialize matte channel */
370 Read runlength-encoded image.
375 opcode=ReadBlobByte(image);
377 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
380 switch (opcode & 0x3f)
384 operand=ReadBlobByte(image);
386 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
389 operand=ReadBlobLSBSignedShort(image);
391 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
399 operand=ReadBlobByte(image);
401 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
402 plane=(unsigned char) operand;
404 plane=(unsigned char) (number_planes-1);
410 operand=ReadBlobByte(image);
412 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
415 operand=ReadBlobLSBSignedShort(image);
417 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
424 operand=ReadBlobByte(image);
426 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
429 operand=ReadBlobLSBSignedShort(image);
431 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
433 offset=(ssize_t) (((image->rows-y-1)*image->columns*number_planes)+x*
434 number_planes+plane);
437 ((size_t) (offset+operand*number_planes) > pixel_info_length))
439 if (number_colormaps != 0)
440 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
441 pixel_info=RelinquishVirtualMemory(pixel_info);
442 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
445 for (i=0; i < (ssize_t) operand; i++)
447 pixel=(unsigned char) ReadBlobByte(image);
448 if ((y < (ssize_t) image->rows) &&
449 ((x+i) < (ssize_t) image->columns))
454 (void) ReadBlobByte(image);
460 operand=ReadBlobByte(image);
462 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
465 operand=ReadBlobLSBSignedShort(image);
467 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
469 pixel=(unsigned char) ReadBlobByte(image);
470 (void) ReadBlobByte(image);
471 offset=(ssize_t) (((image->rows-y-1)*image->columns*number_planes)+x*
472 number_planes+plane);
475 ((size_t) (offset+operand*number_planes) > pixel_info_length))
477 if (number_colormaps != 0)
478 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
479 pixel_info=RelinquishVirtualMemory(pixel_info);
480 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
483 for (i=0; i < (ssize_t) operand; i++)
485 if ((y < (ssize_t) image->rows) &&
486 ((x+i) < (ssize_t) image->columns))
496 opcode=ReadBlobByte(image);
498 ThrowRLEException(CorruptImageError,"UnexpectedEndOfFile");
499 } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
500 if (number_colormaps != 0)
506 Apply colormap affineation to image.
508 mask=(MagickStatusType) (map_length-1);
510 x=(ssize_t) number_planes;
511 if (number_colormaps == 1)
512 for (i=0; i < (ssize_t) number_pixels; i++)
514 ValidateColormapValue(image,(ssize_t) (*p & mask),&index,exception);
515 *p=colormap[(ssize_t) index];
519 if ((number_planes >= 3) && (number_colormaps >= 3))
520 for (i=0; i < (ssize_t) number_pixels; i++)
521 for (x=0; x < (ssize_t) number_planes; x++)
523 ValidateColormapValue(image,(ssize_t) (x*map_length+
524 (*p & mask)),&index,exception);
525 *p=colormap[(ssize_t) index];
528 if ((i < (ssize_t) number_pixels) || (x < (ssize_t) number_planes))
530 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
531 pixel_info=RelinquishVirtualMemory(pixel_info);
532 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
536 Initialize image structure.
538 if (number_planes >= 3)
541 Convert raster image to DirectClass pixel packets.
544 for (y=0; y < (ssize_t) image->rows; y++)
546 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
547 if (q == (Quantum *) NULL)
549 for (x=0; x < (ssize_t) image->columns; x++)
551 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
552 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
553 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
554 if (image->alpha_trait != UndefinedPixelTrait)
555 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
556 q+=GetPixelChannels(image);
558 if (SyncAuthenticPixels(image,exception) == MagickFalse)
560 if (image->previous == (Image *) NULL)
562 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
564 if (status == MagickFalse)
574 if (number_colormaps == 0)
576 if (AcquireImageColormap(image,map_length,exception) == MagickFalse)
577 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
579 if (number_colormaps == 1)
580 for (i=0; i < (ssize_t) image->colors; i++)
585 image->colormap[i].red=(MagickRealType)
586 ScaleCharToQuantum((unsigned char) i);
587 image->colormap[i].green=(MagickRealType)
588 ScaleCharToQuantum((unsigned char) i);
589 image->colormap[i].blue=(MagickRealType)
590 ScaleCharToQuantum((unsigned char) i);
593 if (number_colormaps > 1)
594 for (i=0; i < (ssize_t) image->colors; i++)
596 image->colormap[i].red=(MagickRealType)
597 ScaleCharToQuantum(*p);
598 image->colormap[i].green=(MagickRealType)
599 ScaleCharToQuantum(*(p+map_length));
600 image->colormap[i].blue=(MagickRealType)
601 ScaleCharToQuantum(*(p+map_length*2));
605 if (image->alpha_trait == UndefinedPixelTrait)
608 Convert raster image to PseudoClass pixel packets.
610 for (y=0; y < (ssize_t) image->rows; y++)
612 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
613 if (q == (Quantum *) NULL)
615 for (x=0; x < (ssize_t) image->columns; x++)
617 SetPixelIndex(image,(Quantum) *p++,q);
618 q+=GetPixelChannels(image);
620 if (SyncAuthenticPixels(image,exception) == MagickFalse)
622 if (image->previous == (Image *) NULL)
624 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
626 if (status == MagickFalse)
630 (void) SyncImage(image,exception);
635 Image has a matte channel-- promote to DirectClass.
637 for (y=0; y < (ssize_t) image->rows; y++)
639 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
640 if (q == (Quantum *) NULL)
642 for (x=0; x < (ssize_t) image->columns; x++)
644 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
645 SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t)
647 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
648 SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t)
650 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
651 SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t)
653 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
654 q+=GetPixelChannels(image);
656 if (x < (ssize_t) image->columns)
658 if (SyncAuthenticPixels(image,exception) == MagickFalse)
660 if (image->previous == (Image *) NULL)
662 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
664 if (status == MagickFalse)
668 image->colormap=(PixelInfo *) RelinquishMagickMemory(
670 image->storage_class=DirectClass;
674 if (number_colormaps != 0)
675 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
676 pixel_info=RelinquishVirtualMemory(pixel_info);
677 if (EOFBlob(image) != MagickFalse)
679 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
684 Proceed to next image.
686 if (image_info->number_scenes != 0)
687 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
689 (void) ReadBlobByte(image);
690 count=ReadBlob(image,2,(unsigned char *) magick);
691 if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
694 Allocate next image structure.
696 AcquireNextImage(image_info,image,exception);
697 if (GetNextImageInList(image) == (Image *) NULL)
699 image=DestroyImageList(image);
700 return((Image *) NULL);
702 image=SyncNextImageInList(image);
703 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
705 if (status == MagickFalse)
708 } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
709 (void) CloseBlob(image);
710 return(GetFirstImageInList(image));
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718 % R e g i s t e r R L E I m a g e %
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724 % RegisterRLEImage() adds attributes for the RLE image format to
725 % the list of supported formats. The attributes include the image format
726 % tag, a method to read and/or write the format, whether the format
727 % supports the saving of more than one frame to the same file or blob,
728 % whether the format supports native in-memory I/O, and a brief
729 % description of the format.
731 % The format of the RegisterRLEImage method is:
733 % size_t RegisterRLEImage(void)
736 ModuleExport size_t RegisterRLEImage(void)
741 entry=AcquireMagickInfo("RLE","RLE","Utah Run length encoded image");
742 entry->decoder=(DecodeImageHandler *) ReadRLEImage;
743 entry->magick=(IsImageFormatHandler *) IsRLE;
744 entry->flags|=CoderDecoderSeekableStreamFlag;
745 entry->flags^=CoderAdjoinFlag;
746 (void) RegisterMagickInfo(entry);
747 return(MagickImageCoderSignature);
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % U n r e g i s t e r R L E I m a g e %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 % UnregisterRLEImage() removes format registrations made by the
762 % RLE module from the list of supported formats.
764 % The format of the UnregisterRLEImage method is:
766 % UnregisterRLEImage(void)
769 ModuleExport void UnregisterRLEImage(void)
771 (void) UnregisterMagickInfo("RLE");