]> granicus.if.org Git - imagemagick/blob - coders/rle.c
(no commit message)
[imagemagick] / coders / rle.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            RRRR   L      EEEEE                              %
7 %                            R   R  L      E                                  %
8 %                            RRRR   L      EEE                                %
9 %                            R R    L      E                                  %
10 %                            R  R   LLLLL  EEEEE                              %
11 %                                                                             %
12 %                                                                             %
13 %                          Read URT RLE Image Format                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "magick/studio.h"
43 #include "magick/property.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/colormap.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
61 \f
62 /*
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %                                                                             %
65 %                                                                             %
66 %                                                                             %
67 %   I s R L E                                                                 %
68 %                                                                             %
69 %                                                                             %
70 %                                                                             %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %
73 %  IsRLE() returns MagickTrue if the image format type, identified by the
74 %  magick string, is RLE.
75 %
76 %  The format of the ReadRLEImage method is:
77 %
78 %      MagickBooleanType IsRLE(const unsigned char *magick,const size_t length)
79 %
80 %  A description of each parameter follows:
81 %
82 %    o magick: compare image format pattern against these bytes.
83 %
84 %    o length: Specifies the length of the magick string.
85 %
86 %
87 */
88 static MagickBooleanType IsRLE(const unsigned char *magick,const size_t length)
89 {
90   if (length < 2)
91     return(MagickFalse);
92   if (memcmp(magick,"\122\314",2) == 0)
93     return(MagickTrue);
94   return(MagickFalse);
95 }
96 \f
97 /*
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 %                                                                             %
100 %                                                                             %
101 %                                                                             %
102 %   R e a d R L E I m a g e                                                   %
103 %                                                                             %
104 %                                                                             %
105 %                                                                             %
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %
108 %  ReadRLEImage() reads a run-length encoded Utah Raster Toolkit
109 %  image file and returns it.  It allocates the memory necessary for the new
110 %  Image structure and returns a pointer to the new image.
111 %
112 %  The format of the ReadRLEImage method is:
113 %
114 %      Image *ReadRLEImage(const ImageInfo *image_info,ExceptionInfo *exception)
115 %
116 %  A description of each parameter follows:
117 %
118 %    o image_info: the image info.
119 %
120 %    o exception: return any errors or warnings in this structure.
121 %
122 %
123 */
124 static Image *ReadRLEImage(const ImageInfo *image_info,ExceptionInfo *exception)
125 {
126 #define SkipLinesOp  0x01
127 #define SetColorOp  0x02
128 #define SkipPixelsOp  0x03
129 #define ByteDataOp  0x05
130 #define RunDataOp  0x06
131 #define EOFOp  0x07
132
133   char
134     magick[12];
135
136   Image
137     *image;
138
139   int
140     opcode,
141     operand,
142     status;
143
144   ssize_t
145     y;
146
147   MagickStatusType
148     flags;
149
150   MagickSizeType
151     number_pixels;
152
153   register IndexPacket
154     *indexes;
155
156   register ssize_t
157     x;
158
159   register PixelPacket
160     *q;
161
162   register ssize_t
163     i;
164
165   register unsigned char
166     *p;
167
168   ssize_t
169     count;
170
171   unsigned char
172     background_color[256],
173     *colormap,
174     pixel,
175     plane,
176     *rle_pixels;
177
178   size_t
179     bits_per_pixel,
180     map_length,
181     number_colormaps,
182     number_planes,
183     one;
184
185   /*
186     Open image file.
187   */
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);
196   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
197   if (status == MagickFalse)
198     {
199       image=DestroyImageList(image);
200       return((Image *) NULL);
201     }
202   /*
203     Determine if this a RLE file.
204   */
205   count=ReadBlob(image,2,(unsigned char *) magick);
206   if ((count == 0) || (memcmp(magick,"\122\314",2) != 0))
207     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
208   do
209   {
210     /*
211       Read image header.
212     */
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->matte=flags & 0x04 ? MagickTrue : MagickFalse;
219     number_planes=1UL*ReadBlobByte(image);
220     bits_per_pixel=1UL*ReadBlobByte(image);
221     number_colormaps=1UL*ReadBlobByte(image);
222     one=1;
223     map_length=one << ReadBlobByte(image);
224     if ((number_planes == 0) || (number_planes == 2) || (bits_per_pixel != 8) ||
225         (image->columns == 0))
226       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
227     if (flags & 0x02)
228       {
229         /*
230           No background color-- initialize to black.
231         */
232         for (i=0; i < (ssize_t) number_planes; i++)
233           background_color[i]=0;
234         (void) ReadBlobByte(image);
235       }
236     else
237       {
238         /*
239           Initialize background color.
240         */
241         p=background_color;
242         for (i=0; i < (ssize_t) number_planes; i++)
243           *p++=(unsigned char) ReadBlobByte(image);
244       }
245     if ((number_planes & 0x01) == 0)
246       (void) ReadBlobByte(image);
247     colormap=(unsigned char *) NULL;
248     if (number_colormaps != 0)
249       {
250         /*
251           Read image colormaps.
252         */
253         colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
254           map_length*sizeof(*colormap));
255         if (colormap == (unsigned char *) NULL)
256           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
257         p=colormap;
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));
261       }
262     if ((flags & 0x08) != 0)
263       {
264         char
265           *comment;
266
267         size_t
268           length;
269
270         /*
271           Read image comment.
272         */
273         length=ReadBlobLSBShort(image);
274         if (length != 0)
275           {
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);
282             comment=DestroyString(comment);
283             if ((length & 0x01) == 0)
284               (void) ReadBlobByte(image);
285           }
286       }
287     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
288       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
289         break;
290     /*
291       Allocate RLE pixels.
292     */
293     if (image->matte != MagickFalse)
294       number_planes++;
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     rle_pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
299       image->rows*number_planes*sizeof(*rle_pixels));
300     if (rle_pixels == (unsigned char *) NULL)
301       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
302     if ((flags & 0x01) && !(flags & 0x02))
303       {
304         ssize_t
305           j;
306
307         /*
308           Set background color.
309         */
310         p=rle_pixels;
311         for (i=0; i < (ssize_t) number_pixels; i++)
312         {
313           if (image->matte == MagickFalse)
314             for (j=0; j < (ssize_t) number_planes; j++)
315               *p++=background_color[j];
316           else
317             {
318               for (j=0; j < (ssize_t) (number_planes-1); j++)
319                 *p++=background_color[j];
320               *p++=0;  /* initialize matte channel */
321             }
322         }
323       }
324     /*
325       Read runlength-encoded image.
326     */
327     plane=0;
328     x=0;
329     y=0;
330     opcode=ReadBlobByte(image);
331     do
332     {
333       switch (opcode & 0x3f)
334       {
335         case SkipLinesOp:
336         {
337           operand=ReadBlobByte(image);
338           if (opcode & 0x40)
339             operand=(int) ReadBlobLSBShort(image);
340           x=0;
341           y+=operand;
342           break;
343         }
344         case SetColorOp:
345         {
346           operand=ReadBlobByte(image);
347           plane=(unsigned char) operand;
348           if (plane == 255)
349             plane=(unsigned char) (number_planes-1);
350           x=0;
351           break;
352         }
353         case SkipPixelsOp:
354         {
355           operand=ReadBlobByte(image);
356           if (opcode & 0x40)
357             operand=(int) ReadBlobLSBShort(image);
358           x+=operand;
359           break;
360         }
361         case ByteDataOp:
362         {
363           operand=ReadBlobByte(image);
364           if (opcode & 0x40)
365             operand=(int) ReadBlobLSBShort(image);
366           p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
367             x*number_planes+plane;
368           operand++;
369           for (i=0; i < (ssize_t) operand; i++)
370           {
371             pixel=(unsigned char) ReadBlobByte(image);
372             if ((y < (ssize_t) image->rows) && ((x+i) < (ssize_t) image->columns))
373               *p=pixel;
374             p+=number_planes;
375           }
376           if (operand & 0x01)
377             (void) ReadBlobByte(image);
378           x+=operand;
379           break;
380         }
381         case RunDataOp:
382         {
383           operand=ReadBlobByte(image);
384           if (opcode & 0x40)
385             operand=(int) ReadBlobLSBShort(image);
386           pixel=(unsigned char) ReadBlobByte(image);
387           (void) ReadBlobByte(image);
388           operand++;
389           p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
390             x*number_planes+plane;
391           for (i=0; i < (ssize_t) operand; i++)
392           {
393             if ((y < (ssize_t) image->rows) && ((x+i) < (ssize_t) image->columns))
394               *p=pixel;
395             p+=number_planes;
396           }
397           x+=operand;
398           break;
399         }
400         default:
401           break;
402       }
403       opcode=ReadBlobByte(image);
404     } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
405     if (number_colormaps != 0)
406       {
407         MagickStatusType
408           mask;
409
410         /*
411           Apply colormap affineation to image.
412         */
413         mask=(MagickStatusType) (map_length-1);
414         p=rle_pixels;
415         if (number_colormaps == 1)
416           for (i=0; i < (ssize_t) number_pixels; i++)
417           {
418             *p=colormap[*p & mask];
419             p++;
420           }
421         else
422           if ((number_planes >= 3) && (number_colormaps >= 3))
423             for (i=0; i < (ssize_t) number_pixels; i++)
424               for (x=0; x < (ssize_t) number_planes; x++)
425               {
426                 *p=colormap[x*map_length+(*p & mask)];
427                 p++;
428               }
429       }
430     /*
431       Initialize image structure.
432     */
433     if (number_planes >= 3)
434       {
435         /*
436           Convert raster image to DirectClass pixel packets.
437         */
438         p=rle_pixels;
439         for (y=0; y < (ssize_t) image->rows; y++)
440         {
441           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
442           if (q == (PixelPacket *) NULL)
443             break;
444           for (x=0; x < (ssize_t) image->columns; x++)
445           {
446             q->red=ScaleCharToQuantum(*p++);
447             q->green=ScaleCharToQuantum(*p++);
448             q->blue=ScaleCharToQuantum(*p++);
449             if (image->matte != MagickFalse)
450               q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
451             q++;
452           }
453           if (SyncAuthenticPixels(image,exception) == MagickFalse)
454             break;
455           if (image->previous == (Image *) NULL)
456             {
457               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
458                 image->rows);
459               if (status == MagickFalse)
460                 break;
461             }
462         }
463       }
464     else
465       {
466         /*
467           Create colormap.
468         */
469         if (number_colormaps == 0)
470           map_length=256;
471         if (AcquireImageColormap(image,map_length) == MagickFalse)
472           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
473         p=colormap;
474         if (number_colormaps == 1)
475           for (i=0; i < (ssize_t) image->colors; i++)
476           {
477             /*
478               Pseudocolor.
479             */
480             image->colormap[i].red=ScaleCharToQuantum((unsigned char) i);
481             image->colormap[i].green=ScaleCharToQuantum((unsigned char) i);
482             image->colormap[i].blue=ScaleCharToQuantum((unsigned char) i);
483           }
484         else
485           if (number_colormaps > 1)
486             for (i=0; i < (ssize_t) image->colors; i++)
487             {
488               image->colormap[i].red=ScaleCharToQuantum(*p);
489               image->colormap[i].green=ScaleCharToQuantum(*(p+map_length));
490               image->colormap[i].blue=ScaleCharToQuantum(*(p+map_length*2));
491               p++;
492             }
493         p=rle_pixels;
494         if (image->matte == MagickFalse)
495           {
496             /*
497               Convert raster image to PseudoClass pixel packets.
498             */
499             for (y=0; y < (ssize_t) image->rows; y++)
500             {
501               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
502               if (q == (PixelPacket *) NULL)
503                 break;
504               indexes=GetAuthenticIndexQueue(image);
505               for (x=0; x < (ssize_t) image->columns; x++)
506                 indexes[x]=(IndexPacket) (*p++);
507               if (SyncAuthenticPixels(image,exception) == MagickFalse)
508                 break;
509               if (image->previous == (Image *) NULL)
510                 {
511                   status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
512                 image->rows);
513                   if (status == MagickFalse)
514                     break;
515                 }
516             }
517             (void) SyncImage(image);
518           }
519         else
520           {
521             /*
522               Image has a matte channel-- promote to DirectClass.
523             */
524             for (y=0; y < (ssize_t) image->rows; y++)
525             {
526               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
527               if (q == (PixelPacket *) NULL)
528                 break;
529               for (x=0; x < (ssize_t) image->columns; x++)
530               {
531                 q->red=image->colormap[*p++].red;
532                 q->green=image->colormap[*p++].green;
533                 q->blue=image->colormap[*p++].blue;
534                 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
535                 q++;
536               }
537               if (SyncAuthenticPixels(image,exception) == MagickFalse)
538                 break;
539               if (image->previous == (Image *) NULL)
540                 {
541                   status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
542                 image->rows);
543                   if (status == MagickFalse)
544                     break;
545                 }
546             }
547             image->colormap=(PixelPacket *)
548               RelinquishMagickMemory(image->colormap);
549             image->storage_class=DirectClass;
550             image->colors=0;
551           }
552       }
553     if (number_colormaps != 0)
554       colormap=(unsigned char *) RelinquishMagickMemory(colormap);
555     rle_pixels=(unsigned char *) RelinquishMagickMemory(rle_pixels);
556     if (EOFBlob(image) != MagickFalse)
557       {
558         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
559           image->filename);
560         break;
561       }
562     /*
563       Proceed to next image.
564     */
565     if (image_info->number_scenes != 0)
566       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
567         break;
568     (void) ReadBlobByte(image);
569     count=ReadBlob(image,2,(unsigned char *) magick);
570     if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
571       {
572         /*
573           Allocate next image structure.
574         */
575         AcquireNextImage(image_info,image);
576         if (GetNextImageInList(image) == (Image *) NULL)
577           {
578             image=DestroyImageList(image);
579             return((Image *) NULL);
580           }
581         image=SyncNextImageInList(image);
582         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
583           GetBlobSize(image));
584         if (status == MagickFalse)
585           break;
586       }
587   } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
588   (void) CloseBlob(image);
589   return(GetFirstImageInList(image));
590 }
591 \f
592 /*
593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594 %                                                                             %
595 %                                                                             %
596 %                                                                             %
597 %   R e g i s t e r R L E I m a g e                                           %
598 %                                                                             %
599 %                                                                             %
600 %                                                                             %
601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602 %
603 %  RegisterRLEImage() adds attributes for the RLE image format to
604 %  the list of supported formats.  The attributes include the image format
605 %  tag, a method to read and/or write the format, whether the format
606 %  supports the saving of more than one frame to the same file or blob,
607 %  whether the format supports native in-memory I/O, and a brief
608 %  description of the format.
609 %
610 %  The format of the RegisterRLEImage method is:
611 %
612 %      size_t RegisterRLEImage(void)
613 %
614 */
615 ModuleExport size_t RegisterRLEImage(void)
616 {
617   MagickInfo
618     *entry;
619
620   entry=SetMagickInfo("RLE");
621   entry->decoder=(DecodeImageHandler *) ReadRLEImage;
622   entry->magick=(IsImageFormatHandler *) IsRLE;
623   entry->adjoin=MagickFalse;
624   entry->description=ConstantString("Utah Run length encoded image");
625   entry->module=ConstantString("RLE");
626   (void) RegisterMagickInfo(entry);
627   return(MagickImageCoderSignature);
628 }
629 \f
630 /*
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 %                                                                             %
633 %                                                                             %
634 %                                                                             %
635 %   U n r e g i s t e r R L E I m a g e                                       %
636 %                                                                             %
637 %                                                                             %
638 %                                                                             %
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 %
641 %  UnregisterRLEImage() removes format registrations made by the
642 %  RLE module from the list of supported formats.
643 %
644 %  The format of the UnregisterRLEImage method is:
645 %
646 %      UnregisterRLEImage(void)
647 %
648 */
649 ModuleExport void UnregisterRLEImage(void)
650 {
651   (void) UnregisterMagickInfo("RLE");
652 }