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