root / ImageMagick / trunk / coders / rle.c

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