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);
223 map_length=one << (unsigned char) ReadBlobByte(image);
224 if ((number_planes == 0) || (number_planes == 2) || (bits_per_pixel != 8) ||
225 (image->columns == 0))
226 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
230 No background color-- initialize to black.
232 for (i=0; i < (ssize_t) number_planes; i++)
233 background_color[i]=0;
234 (void) ReadBlobByte(image);
239 Initialize background color.
242 for (i=0; i < (ssize_t) number_planes; i++)
243 *p++=(unsigned char) ReadBlobByte(image);
245 if ((number_planes & 0x01) == 0)
246 (void) ReadBlobByte(image);
247 colormap=(unsigned char *) NULL;
248 if (number_colormaps != 0)
251 Read image colormaps.
253 colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
254 map_length*sizeof(*colormap));
255 if (colormap == (unsigned char *) NULL)
256 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
258 for (i=0; i < (ssize_t) number_colormaps; i++)
259 for (x=0; x < (ssize_t) map_length; x++)
260 *p++=(unsigned char) ScaleShortToQuantum(ReadBlobLSBShort(image));
262 if ((flags & 0x08) != 0)
273 length=ReadBlobLSBShort(image);
276 comment=(char *) AcquireQuantumMemory(length,sizeof(*comment));
277 if (comment == (char *) NULL)
278 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
279 count=ReadBlob(image,length-1,(unsigned char *) comment);
280 comment[length-1]='\0';
281 (void) SetImageProperty(image,"comment",comment,exception);
282 comment=DestroyString(comment);
283 if ((length & 0x01) == 0)
284 (void) ReadBlobByte(image);
287 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
288 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
293 if (image->alpha_trait == BlendPixelTrait)
295 number_pixels=(MagickSizeType) image->columns*image->rows;
296 if ((number_pixels*number_planes) != (size_t) (number_pixels*number_planes))
297 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
298 pixel_info=AcquireVirtualMemory(image->columns,image->rows*number_planes*
300 if (pixel_info == (MemoryInfo *) NULL)
301 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
302 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
303 if ((flags & 0x01) && !(flags & 0x02))
309 Set background color.
312 for (i=0; i < (ssize_t) number_pixels; i++)
314 if (image->alpha_trait != BlendPixelTrait)
315 for (j=0; j < (ssize_t) number_planes; j++)
316 *p++=background_color[j];
319 for (j=0; j < (ssize_t) (number_planes-1); j++)
320 *p++=background_color[j];
321 *p++=0; /* initialize matte channel */
326 Read runlength-encoded image.
331 opcode=ReadBlobByte(image);
334 switch (opcode & 0x3f)
338 operand=ReadBlobByte(image);
340 operand=(int) ReadBlobLSBShort(image);
347 operand=ReadBlobByte(image);
348 plane=(unsigned char) operand;
350 plane=(unsigned char) (number_planes-1);
356 operand=ReadBlobByte(image);
358 operand=(int) ReadBlobLSBShort(image);
364 operand=ReadBlobByte(image);
366 operand=(int) ReadBlobLSBShort(image);
367 p=pixels+((image->rows-y-1)*image->columns*number_planes)+
368 x*number_planes+plane;
370 for (i=0; i < (ssize_t) operand; i++)
372 pixel=(unsigned char) ReadBlobByte(image);
373 if ((y < (ssize_t) image->rows) &&
374 ((x+i) < (ssize_t) image->columns))
379 (void) ReadBlobByte(image);
385 operand=ReadBlobByte(image);
387 operand=(int) ReadBlobLSBShort(image);
388 pixel=(unsigned char) ReadBlobByte(image);
389 (void) ReadBlobByte(image);
391 p=pixels+((image->rows-y-1)*image->columns*number_planes)+
392 x*number_planes+plane;
393 for (i=0; i < (ssize_t) operand; i++)
395 if ((y < (ssize_t) image->rows) &&
396 ((x+i) < (ssize_t) image->columns))
406 opcode=ReadBlobByte(image);
407 } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
408 if (number_colormaps != 0)
414 Apply colormap affineation to image.
416 mask=(MagickStatusType) (map_length-1);
418 if (number_colormaps == 1)
419 for (i=0; i < (ssize_t) number_pixels; i++)
421 *p=colormap[*p & mask];
425 if ((number_planes >= 3) && (number_colormaps >= 3))
426 for (i=0; i < (ssize_t) number_pixels; i++)
427 for (x=0; x < (ssize_t) number_planes; x++)
429 *p=colormap[x*map_length+(*p & mask)];
434 Initialize image structure.
436 if (number_planes >= 3)
439 Convert raster image to DirectClass pixel packets.
442 for (y=0; y < (ssize_t) image->rows; y++)
444 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
445 if (q == (Quantum *) NULL)
447 for (x=0; x < (ssize_t) image->columns; x++)
449 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
450 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
451 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
452 if (image->alpha_trait == BlendPixelTrait)
453 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
454 q+=GetPixelChannels(image);
456 if (SyncAuthenticPixels(image,exception) == MagickFalse)
458 if (image->previous == (Image *) NULL)
460 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
462 if (status == MagickFalse)
472 if (number_colormaps == 0)
474 if (AcquireImageColormap(image,map_length,exception) == MagickFalse)
475 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
477 if (number_colormaps == 1)
478 for (i=0; i < (ssize_t) image->colors; i++)
483 image->colormap[i].red=ScaleCharToQuantum((unsigned char) i);
484 image->colormap[i].green=ScaleCharToQuantum((unsigned char) i);
485 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) i);
488 if (number_colormaps > 1)
489 for (i=0; i < (ssize_t) image->colors; i++)
491 image->colormap[i].red=ScaleCharToQuantum(*p);
492 image->colormap[i].green=ScaleCharToQuantum(*(p+map_length));
493 image->colormap[i].blue=ScaleCharToQuantum(*(p+map_length*2));
497 if (image->alpha_trait != BlendPixelTrait)
500 Convert raster image to PseudoClass pixel packets.
502 for (y=0; y < (ssize_t) image->rows; y++)
504 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
505 if (q == (Quantum *) NULL)
507 for (x=0; x < (ssize_t) image->columns; x++)
509 SetPixelIndex(image,*p++,q);
510 q+=GetPixelChannels(image);
512 if (SyncAuthenticPixels(image,exception) == MagickFalse)
514 if (image->previous == (Image *) NULL)
516 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
518 if (status == MagickFalse)
522 (void) SyncImage(image,exception);
527 Image has a matte channel-- promote to DirectClass.
529 for (y=0; y < (ssize_t) image->rows; y++)
531 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
532 if (q == (Quantum *) NULL)
534 for (x=0; x < (ssize_t) image->columns; x++)
536 SetPixelRed(image,image->colormap[*p++].red,q);
537 SetPixelGreen(image,image->colormap[*p++].green,q);
538 SetPixelBlue(image,image->colormap[*p++].blue,q);
539 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
540 q+=GetPixelChannels(image);
542 if (SyncAuthenticPixels(image,exception) == MagickFalse)
544 if (image->previous == (Image *) NULL)
546 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
548 if (status == MagickFalse)
552 image->colormap=(PixelInfo *) RelinquishMagickMemory(
554 image->storage_class=DirectClass;
558 if (number_colormaps != 0)
559 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
560 pixel_info=RelinquishVirtualMemory(pixel_info);
561 if (EOFBlob(image) != MagickFalse)
563 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
568 Proceed to next image.
570 if (image_info->number_scenes != 0)
571 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
573 (void) ReadBlobByte(image);
574 count=ReadBlob(image,2,(unsigned char *) magick);
575 if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
578 Allocate next image structure.
580 AcquireNextImage(image_info,image,exception);
581 if (GetNextImageInList(image) == (Image *) NULL)
583 image=DestroyImageList(image);
584 return((Image *) NULL);
586 image=SyncNextImageInList(image);
587 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
589 if (status == MagickFalse)
592 } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
593 (void) CloseBlob(image);
594 return(GetFirstImageInList(image));
598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602 % R e g i s t e r R L E I m a g e %
606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
608 % RegisterRLEImage() adds attributes for the RLE image format to
609 % the list of supported formats. The attributes include the image format
610 % tag, a method to read and/or write the format, whether the format
611 % supports the saving of more than one frame to the same file or blob,
612 % whether the format supports native in-memory I/O, and a brief
613 % description of the format.
615 % The format of the RegisterRLEImage method is:
617 % size_t RegisterRLEImage(void)
620 ModuleExport size_t RegisterRLEImage(void)
625 entry=SetMagickInfo("RLE");
626 entry->decoder=(DecodeImageHandler *) ReadRLEImage;
627 entry->magick=(IsImageFormatHandler *) IsRLE;
628 entry->adjoin=MagickFalse;
629 entry->description=ConstantString("Utah Run length encoded image");
630 entry->module=ConstantString("RLE");
631 (void) RegisterMagickInfo(entry);
632 return(MagickImageCoderSignature);
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % U n r e g i s t e r R L E I m a g e %
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % UnregisterRLEImage() removes format registrations made by the
647 % RLE module from the list of supported formats.
649 % The format of the UnregisterRLEImage method is:
651 % UnregisterRLEImage(void)
654 ModuleExport void UnregisterRLEImage(void)
656 (void) UnregisterMagickInfo("RLE");