2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read URT RLE Image Format %
20 % Copyright 1999-2012 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
161 register unsigned char
176 background_color[256],
185 assert(image_info != (const ImageInfo *) NULL);
186 assert(image_info->signature == MagickSignature);
187 if (image_info->debug != MagickFalse)
188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
189 image_info->filename);
190 assert(exception != (ExceptionInfo *) NULL);
191 assert(exception->signature == MagickSignature);
192 image=AcquireImage(image_info,exception);
193 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
194 if (status == MagickFalse)
196 image=DestroyImageList(image);
197 return((Image *) NULL);
200 Determine if this a RLE file.
202 count=ReadBlob(image,2,(unsigned char *) magick);
203 if ((count == 0) || (memcmp(magick,"\122\314",2) != 0))
204 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
210 (void) ReadBlobLSBShort(image);
211 (void) ReadBlobLSBShort(image);
212 image->columns=ReadBlobLSBShort(image);
213 image->rows=ReadBlobLSBShort(image);
214 flags=(MagickStatusType) ReadBlobByte(image);
215 image->matte=flags & 0x04 ? MagickTrue : MagickFalse;
216 number_planes=1UL*ReadBlobByte(image);
217 bits_per_pixel=1UL*ReadBlobByte(image);
218 number_colormaps=1UL*ReadBlobByte(image);
220 map_length=one << ReadBlobByte(image);
221 if ((number_planes == 0) || (number_planes == 2) || (bits_per_pixel != 8) ||
222 (image->columns == 0))
223 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
227 No background color-- initialize to black.
229 for (i=0; i < (ssize_t) number_planes; i++)
230 background_color[i]=0;
231 (void) ReadBlobByte(image);
236 Initialize background color.
239 for (i=0; i < (ssize_t) number_planes; i++)
240 *p++=(unsigned char) ReadBlobByte(image);
242 if ((number_planes & 0x01) == 0)
243 (void) ReadBlobByte(image);
244 colormap=(unsigned char *) NULL;
245 if (number_colormaps != 0)
248 Read image colormaps.
250 colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
251 map_length*sizeof(*colormap));
252 if (colormap == (unsigned char *) NULL)
253 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
255 for (i=0; i < (ssize_t) number_colormaps; i++)
256 for (x=0; x < (ssize_t) map_length; x++)
257 *p++=(unsigned char) ScaleShortToQuantum(ReadBlobLSBShort(image));
259 if ((flags & 0x08) != 0)
270 length=ReadBlobLSBShort(image);
273 comment=(char *) AcquireQuantumMemory(length,sizeof(*comment));
274 if (comment == (char *) NULL)
275 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
276 count=ReadBlob(image,length-1,(unsigned char *) comment);
277 comment[length-1]='\0';
278 (void) SetImageProperty(image,"comment",comment,exception);
279 comment=DestroyString(comment);
280 if ((length & 0x01) == 0)
281 (void) ReadBlobByte(image);
284 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
285 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
290 if (image->matte != MagickFalse)
292 number_pixels=(MagickSizeType) image->columns*image->rows;
293 if ((number_pixels*number_planes) != (size_t) (number_pixels*number_planes))
294 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
295 rle_pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
296 image->rows*number_planes*sizeof(*rle_pixels));
297 if (rle_pixels == (unsigned char *) NULL)
298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
299 if ((flags & 0x01) && !(flags & 0x02))
305 Set background color.
308 for (i=0; i < (ssize_t) number_pixels; i++)
310 if (image->matte == MagickFalse)
311 for (j=0; j < (ssize_t) number_planes; j++)
312 *p++=background_color[j];
315 for (j=0; j < (ssize_t) (number_planes-1); j++)
316 *p++=background_color[j];
317 *p++=0; /* initialize matte channel */
322 Read runlength-encoded image.
327 opcode=ReadBlobByte(image);
330 switch (opcode & 0x3f)
334 operand=ReadBlobByte(image);
336 operand=(int) ReadBlobLSBShort(image);
343 operand=ReadBlobByte(image);
344 plane=(unsigned char) operand;
346 plane=(unsigned char) (number_planes-1);
352 operand=ReadBlobByte(image);
354 operand=(int) ReadBlobLSBShort(image);
360 operand=ReadBlobByte(image);
362 operand=(int) ReadBlobLSBShort(image);
363 p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
364 x*number_planes+plane;
366 for (i=0; i < (ssize_t) operand; i++)
368 pixel=(unsigned char) ReadBlobByte(image);
369 if ((y < (ssize_t) image->rows) &&
370 ((x+i) < (ssize_t) image->columns))
375 (void) ReadBlobByte(image);
381 operand=ReadBlobByte(image);
383 operand=(int) ReadBlobLSBShort(image);
384 pixel=(unsigned char) ReadBlobByte(image);
385 (void) ReadBlobByte(image);
387 p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
388 x*number_planes+plane;
389 for (i=0; i < (ssize_t) operand; i++)
391 if ((y < (ssize_t) image->rows) &&
392 ((x+i) < (ssize_t) image->columns))
402 opcode=ReadBlobByte(image);
403 } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
404 if (number_colormaps != 0)
410 Apply colormap affineation to image.
412 mask=(MagickStatusType) (map_length-1);
414 if (number_colormaps == 1)
415 for (i=0; i < (ssize_t) number_pixels; i++)
417 *p=colormap[*p & mask];
421 if ((number_planes >= 3) && (number_colormaps >= 3))
422 for (i=0; i < (ssize_t) number_pixels; i++)
423 for (x=0; x < (ssize_t) number_planes; x++)
425 *p=colormap[x*map_length+(*p & mask)];
430 Initialize image structure.
432 if (number_planes >= 3)
435 Convert raster image to DirectClass pixel packets.
438 for (y=0; y < (ssize_t) image->rows; y++)
440 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
441 if (q == (Quantum *) NULL)
443 for (x=0; x < (ssize_t) image->columns; x++)
445 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
446 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
447 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
448 if (image->matte != MagickFalse)
449 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
450 q+=GetPixelChannels(image);
452 if (SyncAuthenticPixels(image,exception) == MagickFalse)
454 if (image->previous == (Image *) NULL)
456 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
458 if (status == MagickFalse)
468 if (number_colormaps == 0)
470 if (AcquireImageColormap(image,map_length,exception) == MagickFalse)
471 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
473 if (number_colormaps == 1)
474 for (i=0; i < (ssize_t) image->colors; i++)
479 image->colormap[i].red=ScaleCharToQuantum((unsigned char) i);
480 image->colormap[i].green=ScaleCharToQuantum((unsigned char) i);
481 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) i);
484 if (number_colormaps > 1)
485 for (i=0; i < (ssize_t) image->colors; i++)
487 image->colormap[i].red=ScaleCharToQuantum(*p);
488 image->colormap[i].green=ScaleCharToQuantum(*(p+map_length));
489 image->colormap[i].blue=ScaleCharToQuantum(*(p+map_length*2));
493 if (image->matte == MagickFalse)
496 Convert raster image to PseudoClass pixel packets.
498 for (y=0; y < (ssize_t) image->rows; y++)
500 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
501 if (q == (Quantum *) NULL)
503 for (x=0; x < (ssize_t) image->columns; x++)
505 SetPixelIndex(image,*p++,q);
506 q+=GetPixelChannels(image);
508 if (SyncAuthenticPixels(image,exception) == MagickFalse)
510 if (image->previous == (Image *) NULL)
512 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
514 if (status == MagickFalse)
518 (void) SyncImage(image,exception);
523 Image has a matte channel-- promote to DirectClass.
525 for (y=0; y < (ssize_t) image->rows; y++)
527 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
528 if (q == (Quantum *) NULL)
530 for (x=0; x < (ssize_t) image->columns; x++)
532 SetPixelRed(image,image->colormap[*p++].red,q);
533 SetPixelGreen(image,image->colormap[*p++].green,q);
534 SetPixelBlue(image,image->colormap[*p++].blue,q);
535 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
536 q+=GetPixelChannels(image);
538 if (SyncAuthenticPixels(image,exception) == MagickFalse)
540 if (image->previous == (Image *) NULL)
542 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
544 if (status == MagickFalse)
548 image->colormap=(PixelInfo *) RelinquishMagickMemory(
550 image->storage_class=DirectClass;
554 if (number_colormaps != 0)
555 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
556 rle_pixels=(unsigned char *) RelinquishMagickMemory(rle_pixels);
557 if (EOFBlob(image) != MagickFalse)
559 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
564 Proceed to next image.
566 if (image_info->number_scenes != 0)
567 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
569 (void) ReadBlobByte(image);
570 count=ReadBlob(image,2,(unsigned char *) magick);
571 if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
574 Allocate next image structure.
576 AcquireNextImage(image_info,image,exception);
577 if (GetNextImageInList(image) == (Image *) NULL)
579 image=DestroyImageList(image);
580 return((Image *) NULL);
582 image=SyncNextImageInList(image);
583 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
585 if (status == MagickFalse)
588 } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
589 (void) CloseBlob(image);
590 return(GetFirstImageInList(image));
594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 % R e g i s t e r R L E I m a g e %
602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
604 % RegisterRLEImage() adds attributes for the RLE image format to
605 % the list of supported formats. The attributes include the image format
606 % tag, a method to read and/or write the format, whether the format
607 % supports the saving of more than one frame to the same file or blob,
608 % whether the format supports native in-memory I/O, and a brief
609 % description of the format.
611 % The format of the RegisterRLEImage method is:
613 % size_t RegisterRLEImage(void)
616 ModuleExport size_t RegisterRLEImage(void)
621 entry=SetMagickInfo("RLE");
622 entry->decoder=(DecodeImageHandler *) ReadRLEImage;
623 entry->magick=(IsImageFormatHandler *) IsRLE;
624 entry->adjoin=MagickFalse;
625 entry->description=ConstantString("Utah Run length encoded image");
626 entry->module=ConstantString("RLE");
627 (void) RegisterMagickInfo(entry);
628 return(MagickImageCoderSignature);
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 % U n r e g i s t e r R L E I m a g e %
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 % UnregisterRLEImage() removes format registrations made by the
643 % RLE module from the list of supported formats.
645 % The format of the UnregisterRLEImage method is:
647 % UnregisterRLEImage(void)
650 ModuleExport void UnregisterRLEImage(void)
652 (void) UnregisterMagickInfo("RLE");