root / ImageMagick / trunk / coders / pix.c

Revision 12035, 11.3 kB (checked in by cristy, 5 days ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%                            PPPP   IIIII  X   X                              %
7%                            P   P    I     X X                               %
8%                            PPPP     I      X                                %
9%                            P        I     X X                               %
10%                            P      IIIII  X   X                              %
11%                                                                             %
12%                                                                             %
13%                    Read Alias/Wavefront 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/blob.h"
44#include "magick/blob-private.h"
45#include "magick/exception.h"
46#include "magick/exception-private.h"
47#include "magick/image.h"
48#include "magick/image-private.h"
49#include "magick/list.h"
50#include "magick/magick.h"
51#include "magick/memory_.h"
52#include "magick/monitor.h"
53#include "magick/monitor-private.h"
54#include "magick/quantum-private.h"
55#include "magick/static.h"
56#include "magick/string_.h"
57#include "magick/module.h"
58
59/*
60%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61%                                                                             %
62%                                                                             %
63%                                                                             %
64%   R e a d P I X I m a g e                                                   %
65%                                                                             %
66%                                                                             %
67%                                                                             %
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69%
70%  ReadPIXImage() reads a Alias/Wavefront RLE image file and returns it.
71%  It allocates the memory necessary for the new Image structure and returns a
72%  pointer to the new image.
73%
74%  The format of the ReadPIXImage method is:
75%
76%      Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
77%
78%  A description of each parameter follows:
79%
80%    o image_info: the image info.
81%
82%    o exception: return any errors or warnings in this structure.
83%
84%
85*/
86static Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
87{
88  Image
89    *image;
90
91  IndexPacket
92    index;
93
94  long
95    y;
96
97  MagickBooleanType
98    status;
99
100  Quantum
101    blue,
102    green,
103    red;
104
105  register IndexPacket
106    *indexes;
107
108  register long
109    x;
110
111  register PixelPacket
112    *q;
113
114  size_t
115    length;
116
117  unsigned long
118    bits_per_pixel,
119    height,
120    width;
121
122  /*
123    Open image file.
124  */
125  assert(image_info != (const ImageInfo *) NULL);
126  assert(image_info->signature == MagickSignature);
127  if (image_info->debug != MagickFalse)
128    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
129      image_info->filename);
130  assert(exception != (ExceptionInfo *) NULL);
131  assert(exception->signature == MagickSignature);
132  image=AcquireImage(image_info);
133  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
134  if (status == MagickFalse)
135    {
136      image=DestroyImageList(image);
137      return((Image *) NULL);
138    }
139  /*
140    Read PIX image.
141  */
142  width=ReadBlobMSBShort(image);
143  height=ReadBlobMSBShort(image);
144  (void) ReadBlobMSBShort(image);  /* x-offset */
145  (void) ReadBlobMSBShort(image);  /* y-offset */
146  bits_per_pixel=ReadBlobMSBShort(image);
147  if ((width == 0UL) || (height == 0UL) || ((bits_per_pixel != 8) &&
148      (bits_per_pixel != 24)))
149    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
150  do
151  {
152    /*
153      Initialize image structure.
154    */
155    image->columns=width;
156    image->rows=height;
157    if (bits_per_pixel == 8)
158      if (AcquireImageColormap(image,256) == MagickFalse)
159        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
160    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
161      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
162        break;
163    /*
164      Convert PIX raster image to pixel packets.
165    */
166    if (SetImageExtent(image,0,0) == MagickFalse)
167      {
168        InheritException(exception,&image->exception);
169        return(DestroyImageList(image));
170      }
171    red=(Quantum) 0;
172    green=(Quantum) 0;
173    blue=(Quantum) 0;
174    index=(IndexPacket) 0;
175    length=0;
176    for (y=0; y < (long) image->rows; y++)
177    {
178      q=SetImagePixels(image,0,y,image->columns,1);
179      if (q == (PixelPacket *) NULL)
180        break;
181      indexes=GetIndexes(image);
182      for (x=0; x < (long) image->columns; x++)
183      {
184        if (length == 0)
185          {
186            length=(size_t) ReadBlobByte(image);
187            if (bits_per_pixel == 8)
188              index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
189            else
190              {
191                blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
192                green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
193                red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
194              }
195          }
196        if (image->storage_class == PseudoClass)
197          indexes[x]=index;
198        q->blue=blue;
199        q->green=green;
200        q->red=red;
201        length--;
202        q++;
203      }
204      if (SyncImagePixels(image) == MagickFalse)
205        break;
206      if (image->previous == (Image *) NULL)
207        {
208          status=SetImageProgress(image,LoadImageTag,y,image->rows);
209          if (status == MagickFalse)
210            break;
211        }
212    }
213    if (image->storage_class == PseudoClass)
214      (void) SyncImage(image);
215    if (EOFBlob(image) != MagickFalse)
216      {
217        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
218          image->filename);
219        break;
220      }
221    /*
222      Proceed to next image.
223    */
224    if (image_info->number_scenes != 0)
225      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
226        break;
227    width=ReadBlobMSBLong(image);
228    height=ReadBlobMSBLong(image);
229    (void) ReadBlobMSBShort(image);
230    (void) ReadBlobMSBShort(image);
231    bits_per_pixel=ReadBlobMSBShort(image);
232    status=(width != 0UL) && (height == 0UL) && ((bits_per_pixel == 8) ||
233      (bits_per_pixel == 24)) ? MagickTrue : MagickFalse;
234    if (status == MagickTrue)
235      {
236        /*
237          Allocate next image structure.
238        */
239        AcquireNextImage(image_info,image);
240        if (GetNextImageInList(image) == (Image *) NULL)
241          {
242            image=DestroyImageList(image);
243            return((Image *) NULL);
244          }
245        image=SyncNextImageInList(image);
246        status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
247          GetBlobSize(image));
248        if (status == MagickFalse)
249          break;
250      }
251  } while (status == MagickTrue);
252  (void) CloseBlob(image);
253  return(GetFirstImageInList(image));
254}
255
256/*
257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258%                                                                             %
259%                                                                             %
260%                                                                             %
261%   R e g i s t e r P I X I m a g e                                           %
262%                                                                             %
263%                                                                             %
264%                                                                             %
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266%
267%  RegisterPIXImage() adds attributes for the PIX image format to
268%  the list of supported formats.  The attributes include the image format
269%  tag, a method to read and/or write the format, whether the format
270%  supports the saving of more than one frame to the same file or blob,
271%  whether the format supports native in-memory I/O, and a brief
272%  description of the format.
273%
274%  The format of the RegisterPIXImage method is:
275%
276%      unsigned long RegisterPIXImage(void)
277%
278*/
279ModuleExport unsigned long RegisterPIXImage(void)
280{
281  MagickInfo
282    *entry;
283
284  entry=SetMagickInfo("PIX");
285  entry->decoder=(DecodeImageHandler *) ReadPIXImage;
286  entry->description=ConstantString("Alias/Wavefront RLE image format");
287  entry->module=ConstantString("PIX");
288  (void) RegisterMagickInfo(entry);
289  return(MagickImageCoderSignature);
290}
291
292/*
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294%                                                                             %
295%                                                                             %
296%                                                                             %
297%   U n r e g i s t e r P I X I m a g e                                       %
298%                                                                             %
299%                                                                             %
300%                                                                             %
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302%
303%  UnregisterPIXImage() removes format registrations made by the
304%  PIX module from the list of supported formats.
305%
306%  The format of the UnregisterPIXImage method is:
307%
308%      UnregisterPIXImage(void)
309%
310*/
311ModuleExport void UnregisterPIXImage(void)
312{
313  (void) UnregisterMagickInfo("PIX");
314}
Note: See TracBrowser for help on using the browser.