]> granicus.if.org Git - imagemagick/blob - coders/pix.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[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-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/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   MagickBooleanType
97     status;
98
99   Quantum
100     blue,
101     green,
102     red;
103
104   register IndexPacket
105     *indexes;
106
107   register ssize_t
108     x;
109
110   register PixelPacket
111     *q;
112
113   size_t
114     bits_per_pixel,
115     height,
116     length,
117     width;
118
119   ssize_t
120     y;
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     red=(Quantum) 0;
167     green=(Quantum) 0;
168     blue=(Quantum) 0;
169     index=(IndexPacket) 0;
170     length=0;
171     for (y=0; y < (ssize_t) image->rows; y++)
172     {
173       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
174       if (q == (PixelPacket *) NULL)
175         break;
176       indexes=GetAuthenticIndexQueue(image);
177       for (x=0; x < (ssize_t) image->columns; x++)
178       {
179         if (length == 0)
180           {
181             length=(size_t) ReadBlobByte(image);
182             if (bits_per_pixel == 8)
183               index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
184             else
185               {
186                 blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
187                 green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
188                 red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
189               }
190           }
191         if (image->storage_class == PseudoClass)
192           SetIndexPixelComponent(indexes+x,index);
193         SetBluePixelComponent(q,blue);
194         SetGreenPixelComponent(q,green);
195         SetRedPixelComponent(q,red);
196         length--;
197         q++;
198       }
199       if (SyncAuthenticPixels(image,exception) == MagickFalse)
200         break;
201       if (image->previous == (Image *) NULL)
202         {
203           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
204             image->rows);
205           if (status == MagickFalse)
206             break;
207         }
208     }
209     if (image->storage_class == PseudoClass)
210       (void) SyncImage(image);
211     if (EOFBlob(image) != MagickFalse)
212       {
213         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
214           image->filename);
215         break;
216       }
217     /*
218       Proceed to next image.
219     */
220     if (image_info->number_scenes != 0)
221       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
222         break;
223     width=ReadBlobMSBLong(image);
224     height=ReadBlobMSBLong(image);
225     (void) ReadBlobMSBShort(image);
226     (void) ReadBlobMSBShort(image);
227     bits_per_pixel=ReadBlobMSBShort(image);
228     status=(width != 0UL) && (height == 0UL) && ((bits_per_pixel == 8) ||
229       (bits_per_pixel == 24)) ? MagickTrue : MagickFalse;
230     if (status == MagickTrue)
231       {
232         /*
233           Allocate next image structure.
234         */
235         AcquireNextImage(image_info,image);
236         if (GetNextImageInList(image) == (Image *) NULL)
237           {
238             image=DestroyImageList(image);
239             return((Image *) NULL);
240           }
241         image=SyncNextImageInList(image);
242         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
243           GetBlobSize(image));
244         if (status == MagickFalse)
245           break;
246       }
247   } while (status == MagickTrue);
248   (void) CloseBlob(image);
249   return(GetFirstImageInList(image));
250 }
251 \f
252 /*
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 %                                                                             %
255 %                                                                             %
256 %                                                                             %
257 %   R e g i s t e r P I X I m a g e                                           %
258 %                                                                             %
259 %                                                                             %
260 %                                                                             %
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 %
263 %  RegisterPIXImage() adds attributes for the PIX image format to
264 %  the list of supported formats.  The attributes include the image format
265 %  tag, a method to read and/or write the format, whether the format
266 %  supports the saving of more than one frame to the same file or blob,
267 %  whether the format supports native in-memory I/O, and a brief
268 %  description of the format.
269 %
270 %  The format of the RegisterPIXImage method is:
271 %
272 %      size_t RegisterPIXImage(void)
273 %
274 */
275 ModuleExport size_t RegisterPIXImage(void)
276 {
277   MagickInfo
278     *entry;
279
280   entry=SetMagickInfo("PIX");
281   entry->decoder=(DecodeImageHandler *) ReadPIXImage;
282   entry->description=ConstantString("Alias/Wavefront RLE image format");
283   entry->module=ConstantString("PIX");
284   (void) RegisterMagickInfo(entry);
285   return(MagickImageCoderSignature);
286 }
287 \f
288 /*
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 %                                                                             %
291 %                                                                             %
292 %                                                                             %
293 %   U n r e g i s t e r P I X I m a g e                                       %
294 %                                                                             %
295 %                                                                             %
296 %                                                                             %
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 %
299 %  UnregisterPIXImage() removes format registrations made by the
300 %  PIX module from the list of supported formats.
301 %
302 %  The format of the UnregisterPIXImage method is:
303 %
304 %      UnregisterPIXImage(void)
305 %
306 */
307 ModuleExport void UnregisterPIXImage(void)
308 {
309   (void) UnregisterMagickInfo("PIX");
310 }