]> 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-2010 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   long
145     y;
146
147   MagickStatusType
148     flags;
149
150   MagickSizeType
151     number_pixels;
152
153   register IndexPacket
154     *indexes;
155
156   register long
157     x;
158
159   register PixelPacket
160     *q;
161
162   register long
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   unsigned long
179     bits_per_pixel,
180     map_length,
181     number_colormaps,
182     number_planes;
183
184   /*
185     Open image file.
186   */
187   assert(image_info != (const ImageInfo *) NULL);
188   assert(image_info->signature == MagickSignature);
189   if (image_info->debug != MagickFalse)
190     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
191       image_info->filename);
192   assert(exception != (ExceptionInfo *) NULL);
193   assert(exception->signature == MagickSignature);
194   image=AcquireImage(image_info);
195   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
196   if (status == MagickFalse)
197     {
198       image=DestroyImageList(image);
199       return((Image *) NULL);
200     }
201   /*
202     Determine if this a RLE file.
203   */
204   count=ReadBlob(image,2,(unsigned char *) magick);
205   if ((count == 0) || (memcmp(magick,"\122\314",2) != 0))
206     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
207   do
208   {
209     /*
210       Read image header.
211     */
212     (void) ReadBlobLSBShort(image);
213     (void) ReadBlobLSBShort(image);
214     image->columns=ReadBlobLSBShort(image);
215     image->rows=ReadBlobLSBShort(image);
216     flags=(MagickStatusType) ReadBlobByte(image);
217     image->matte=flags & 0x04 ? MagickTrue : MagickFalse;
218     number_planes=1UL*ReadBlobByte(image);
219     bits_per_pixel=1UL*ReadBlobByte(image);
220     number_colormaps=1UL*ReadBlobByte(image);
221     map_length=1UL << ReadBlobByte(image);
222     if ((number_planes == 0) || (number_planes == 2) || (bits_per_pixel != 8) ||
223         (image->columns == 0))
224       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
225     if (flags & 0x02)
226       {
227         /*
228           No background color-- initialize to black.
229         */
230         for (i=0; i < (long) number_planes; i++)
231           background_color[i]=0;
232         (void) ReadBlobByte(image);
233       }
234     else
235       {
236         /*
237           Initialize background color.
238         */
239         p=background_color;
240         for (i=0; i < (long) number_planes; i++)
241           *p++=(unsigned char) ReadBlobByte(image);
242       }
243     if ((number_planes & 0x01) == 0)
244       (void) ReadBlobByte(image);
245     colormap=(unsigned char *) NULL;
246     if (number_colormaps != 0)
247       {
248         /*
249           Read image colormaps.
250         */
251         colormap=(unsigned char *) AcquireQuantumMemory(number_colormaps,
252           map_length*sizeof(*colormap));
253         if (colormap == (unsigned char *) NULL)
254           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
255         p=colormap;
256         for (i=0; i < (long) number_colormaps; i++)
257           for (x=0; x < (long) map_length; x++)
258             *p++=(unsigned char) ScaleShortToQuantum(ReadBlobLSBShort(image));
259       }
260     if ((flags & 0x08) != 0)
261       {
262         char
263           *comment;
264
265         unsigned long
266           length;
267
268         /*
269           Read image comment.
270         */
271         length=ReadBlobLSBShort(image);
272         if (length != 0)
273           {
274             comment=(char *) AcquireQuantumMemory(length,sizeof(*comment));
275             if (comment == (char *) NULL)
276               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
277             count=ReadBlob(image,length-1,(unsigned char *) comment);
278             comment[length-1]='\0';
279             (void) SetImageProperty(image,"comment",comment);
280             comment=DestroyString(comment);
281             if ((length & 0x01) == 0)
282               (void) ReadBlobByte(image);
283           }
284       }
285     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
286       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
287         break;
288     /*
289       Allocate RLE pixels.
290     */
291     if (image->matte != MagickFalse)
292       number_planes++;
293     number_pixels=(MagickSizeType) image->columns*image->rows;
294     if ((number_pixels*number_planes) != (size_t) (number_pixels*number_planes))
295       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
296     rle_pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
297       image->rows*number_planes*sizeof(*rle_pixels));
298     if (rle_pixels == (unsigned char *) NULL)
299       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
300     if ((flags & 0x01) && !(flags & 0x02))
301       {
302         long
303           j;
304
305         /*
306           Set background color.
307         */
308         p=rle_pixels;
309         for (i=0; i < (long) number_pixels; i++)
310         {
311           if (image->matte == MagickFalse)
312             for (j=0; j < (long) number_planes; j++)
313               *p++=background_color[j];
314           else
315             {
316               for (j=0; j < (long) (number_planes-1); j++)
317                 *p++=background_color[j];
318               *p++=0;  /* initialize matte channel */
319             }
320         }
321       }
322     /*
323       Read runlength-encoded image.
324     */
325     plane=0;
326     x=0;
327     y=0;
328     opcode=ReadBlobByte(image);
329     do
330     {
331       switch (opcode & 0x3f)
332       {
333         case SkipLinesOp:
334         {
335           operand=ReadBlobByte(image);
336           if (opcode & 0x40)
337             operand=(int) ReadBlobLSBShort(image);
338           x=0;
339           y+=operand;
340           break;
341         }
342         case SetColorOp:
343         {
344           operand=ReadBlobByte(image);
345           plane=(unsigned char) operand;
346           if (plane == 255)
347             plane=(unsigned char) (number_planes-1);
348           x=0;
349           break;
350         }
351         case SkipPixelsOp:
352         {
353           operand=ReadBlobByte(image);
354           if (opcode & 0x40)
355             operand=(int) ReadBlobLSBShort(image);
356           x+=operand;
357           break;
358         }
359         case ByteDataOp:
360         {
361           operand=ReadBlobByte(image);
362           if (opcode & 0x40)
363             operand=(int) ReadBlobLSBShort(image);
364           p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
365             x*number_planes+plane;
366           operand++;
367           for (i=0; i < (long) operand; i++)
368           {
369             pixel=(unsigned char) ReadBlobByte(image);
370             if ((y < (long) image->rows) && ((x+i) < (long) image->columns))
371               *p=pixel;
372             p+=number_planes;
373           }
374           if (operand & 0x01)
375             (void) ReadBlobByte(image);
376           x+=operand;
377           break;
378         }
379         case RunDataOp:
380         {
381           operand=ReadBlobByte(image);
382           if (opcode & 0x40)
383             operand=(int) ReadBlobLSBShort(image);
384           pixel=(unsigned char) ReadBlobByte(image);
385           (void) ReadBlobByte(image);
386           operand++;
387           p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
388             x*number_planes+plane;
389           for (i=0; i < (long) operand; i++)
390           {
391             if ((y < (long) image->rows) && ((x+i) < (long) image->columns))
392               *p=pixel;
393             p+=number_planes;
394           }
395           x+=operand;
396           break;
397         }
398         default:
399           break;
400       }
401       opcode=ReadBlobByte(image);
402     } while (((opcode & 0x3f) != EOFOp) && (opcode != EOF));
403     if (number_colormaps != 0)
404       {
405         MagickStatusType
406           mask;
407
408         /*
409           Apply colormap affineation to image.
410         */
411         mask=(MagickStatusType) (map_length-1);
412         p=rle_pixels;
413         if (number_colormaps == 1)
414           for (i=0; i < (long) number_pixels; i++)
415           {
416             *p=colormap[*p & mask];
417             p++;
418           }
419         else
420           if ((number_planes >= 3) && (number_colormaps >= 3))
421             for (i=0; i < (long) number_pixels; i++)
422               for (x=0; x < (long) number_planes; x++)
423               {
424                 *p=colormap[x*map_length+(*p & mask)];
425                 p++;
426               }
427       }
428     /*
429       Initialize image structure.
430     */
431     if (number_planes >= 3)
432       {
433         /*
434           Convert raster image to DirectClass pixel packets.
435         */
436         p=rle_pixels;
437         for (y=0; y < (long) image->rows; y++)
438         {
439           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
440           if (q == (PixelPacket *) NULL)
441             break;
442           for (x=0; x < (long) image->columns; x++)
443           {
444             q->red=ScaleCharToQuantum(*p++);
445             q->green=ScaleCharToQuantum(*p++);
446             q->blue=ScaleCharToQuantum(*p++);
447             if (image->matte != MagickFalse)
448               q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
449             q++;
450           }
451           if (SyncAuthenticPixels(image,exception) == MagickFalse)
452             break;
453           if (image->previous == (Image *) NULL)
454             {
455               status=SetImageProgress(image,LoadImageTag,y,image->rows);
456               if (status == MagickFalse)
457                 break;
458             }
459         }
460       }
461     else
462       {
463         /*
464           Create colormap.
465         */
466         if (number_colormaps == 0)
467           map_length=256;
468         if (AcquireImageColormap(image,map_length) == MagickFalse)
469           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
470         p=colormap;
471         if (number_colormaps == 1)
472           for (i=0; i < (long) image->colors; i++)
473           {
474             /*
475               Pseudocolor.
476             */
477             image->colormap[i].red=ScaleCharToQuantum((unsigned char) i);
478             image->colormap[i].green=ScaleCharToQuantum((unsigned char) i);
479             image->colormap[i].blue=ScaleCharToQuantum((unsigned char) i);
480           }
481         else
482           if (number_colormaps > 1)
483             for (i=0; i < (long) image->colors; i++)
484             {
485               image->colormap[i].red=ScaleCharToQuantum(*p);
486               image->colormap[i].green=ScaleCharToQuantum(*(p+map_length));
487               image->colormap[i].blue=ScaleCharToQuantum(*(p+map_length*2));
488               p++;
489             }
490         p=rle_pixels;
491         if (image->matte == MagickFalse)
492           {
493             /*
494               Convert raster image to PseudoClass pixel packets.
495             */
496             for (y=0; y < (long) image->rows; y++)
497             {
498               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
499               if (q == (PixelPacket *) NULL)
500                 break;
501               indexes=GetAuthenticIndexQueue(image);
502               for (x=0; x < (long) image->columns; x++)
503                 indexes[x]=(IndexPacket) (*p++);
504               if (SyncAuthenticPixels(image,exception) == MagickFalse)
505                 break;
506               if (image->previous == (Image *) NULL)
507                 {
508                   status=SetImageProgress(image,LoadImageTag,y,image->rows);
509                   if (status == MagickFalse)
510                     break;
511                 }
512             }
513             (void) SyncImage(image);
514           }
515         else
516           {
517             /*
518               Image has a matte channel-- promote to DirectClass.
519             */
520             for (y=0; y < (long) image->rows; y++)
521             {
522               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
523               if (q == (PixelPacket *) NULL)
524                 break;
525               for (x=0; x < (long) image->columns; x++)
526               {
527                 q->red=image->colormap[*p++].red;
528                 q->green=image->colormap[*p++].green;
529                 q->blue=image->colormap[*p++].blue;
530                 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
531                 q++;
532               }
533               if (SyncAuthenticPixels(image,exception) == MagickFalse)
534                 break;
535               if (image->previous == (Image *) NULL)
536                 {
537                   status=SetImageProgress(image,LoadImageTag,y,image->rows);
538                   if (status == MagickFalse)
539                     break;
540                 }
541             }
542             image->colormap=(PixelPacket *)
543               RelinquishMagickMemory(image->colormap);
544             image->storage_class=DirectClass;
545             image->colors=0;
546           }
547       }
548     if (number_colormaps != 0)
549       colormap=(unsigned char *) RelinquishMagickMemory(colormap);
550     rle_pixels=(unsigned char *) RelinquishMagickMemory(rle_pixels);
551     if (EOFBlob(image) != MagickFalse)
552       {
553         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
554           image->filename);
555         break;
556       }
557     /*
558       Proceed to next image.
559     */
560     if (image_info->number_scenes != 0)
561       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
562         break;
563     (void) ReadBlobByte(image);
564     count=ReadBlob(image,2,(unsigned char *) magick);
565     if ((count != 0) && (memcmp(magick,"\122\314",2) == 0))
566       {
567         /*
568           Allocate next image structure.
569         */
570         AcquireNextImage(image_info,image);
571         if (GetNextImageInList(image) == (Image *) NULL)
572           {
573             image=DestroyImageList(image);
574             return((Image *) NULL);
575           }
576         image=SyncNextImageInList(image);
577         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
578           GetBlobSize(image));
579         if (status == MagickFalse)
580           break;
581       }
582   } while ((count != 0) && (memcmp(magick,"\122\314",2) == 0));
583   (void) CloseBlob(image);
584   return(GetFirstImageInList(image));
585 }
586 \f
587 /*
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
589 %                                                                             %
590 %                                                                             %
591 %                                                                             %
592 %   R e g i s t e r R L E I m a g e                                           %
593 %                                                                             %
594 %                                                                             %
595 %                                                                             %
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597 %
598 %  RegisterRLEImage() adds attributes for the RLE image format to
599 %  the list of supported formats.  The attributes include the image format
600 %  tag, a method to read and/or write the format, whether the format
601 %  supports the saving of more than one frame to the same file or blob,
602 %  whether the format supports native in-memory I/O, and a brief
603 %  description of the format.
604 %
605 %  The format of the RegisterRLEImage method is:
606 %
607 %      unsigned long RegisterRLEImage(void)
608 %
609 */
610 ModuleExport unsigned long RegisterRLEImage(void)
611 {
612   MagickInfo
613     *entry;
614
615   entry=SetMagickInfo("RLE");
616   entry->decoder=(DecodeImageHandler *) ReadRLEImage;
617   entry->magick=(IsImageFormatHandler *) IsRLE;
618   entry->adjoin=MagickFalse;
619   entry->description=ConstantString("Utah Run length encoded image");
620   entry->module=ConstantString("RLE");
621   (void) RegisterMagickInfo(entry);
622   return(MagickImageCoderSignature);
623 }
624 \f
625 /*
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627 %                                                                             %
628 %                                                                             %
629 %                                                                             %
630 %   U n r e g i s t e r R L E I m a g e                                       %
631 %                                                                             %
632 %                                                                             %
633 %                                                                             %
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 %
636 %  UnregisterRLEImage() removes format registrations made by the
637 %  RLE module from the list of supported formats.
638 %
639 %  The format of the UnregisterRLEImage method is:
640 %
641 %      UnregisterRLEImage(void)
642 %
643 */
644 ModuleExport void UnregisterRLEImage(void)
645 {
646   (void) UnregisterMagickInfo("RLE");
647 }