blob: 9cf1528cc76defb4ff07f696b0413c30c7f46961 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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/cache.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% R e a d P I X I m a g e %
66% %
67% %
68% %
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%
71% ReadPIXImage() reads a Alias/Wavefront RLE image file and returns it.
72% It allocates the memory necessary for the new Image structure and returns a
73% pointer to the new image.
74%
75% The format of the ReadPIXImage method is:
76%
77% Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
78%
79% A description of each parameter follows:
80%
81% o image_info: the image info.
82%
83% o exception: return any errors or warnings in this structure.
84%
85%
86*/
87static Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
88{
89 Image
90 *image;
91
92 IndexPacket
93 index;
94
95 long
96 y;
97
98 MagickBooleanType
99 status;
100
101 Quantum
102 blue,
103 green,
104 red;
105
106 register IndexPacket
107 *indexes;
108
109 register long
110 x;
111
112 register PixelPacket
113 *q;
114
115 size_t
116 length;
117
118 unsigned long
119 bits_per_pixel,
120 height,
121 width;
122
123 /*
124 Open image file.
125 */
126 assert(image_info != (const ImageInfo *) NULL);
127 assert(image_info->signature == MagickSignature);
128 if (image_info->debug != MagickFalse)
129 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
130 image_info->filename);
131 assert(exception != (ExceptionInfo *) NULL);
132 assert(exception->signature == MagickSignature);
133 image=AcquireImage(image_info);
134 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
135 if (status == MagickFalse)
136 {
137 image=DestroyImageList(image);
138 return((Image *) NULL);
139 }
140 /*
141 Read PIX image.
142 */
143 width=ReadBlobMSBShort(image);
144 height=ReadBlobMSBShort(image);
145 (void) ReadBlobMSBShort(image); /* x-offset */
146 (void) ReadBlobMSBShort(image); /* y-offset */
147 bits_per_pixel=ReadBlobMSBShort(image);
148 if ((width == 0UL) || (height == 0UL) || ((bits_per_pixel != 8) &&
149 (bits_per_pixel != 24)))
150 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151 do
152 {
153 /*
154 Initialize image structure.
155 */
156 image->columns=width;
157 image->rows=height;
158 if (bits_per_pixel == 8)
159 if (AcquireImageColormap(image,256) == MagickFalse)
160 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
161 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
162 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
163 break;
164 /*
165 Convert PIX raster image to pixel packets.
166 */
167 red=(Quantum) 0;
168 green=(Quantum) 0;
169 blue=(Quantum) 0;
170 index=(IndexPacket) 0;
171 length=0;
172 for (y=0; y < (long) image->rows; y++)
173 {
174 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
175 if (q == (PixelPacket *) NULL)
176 break;
177 indexes=GetAuthenticIndexQueue(image);
178 for (x=0; x < (long) image->columns; x++)
179 {
180 if (length == 0)
181 {
182 length=(size_t) ReadBlobByte(image);
183 if (bits_per_pixel == 8)
184 index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
185 else
186 {
187 blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
188 green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
189 red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
190 }
191 }
192 if (image->storage_class == PseudoClass)
193 indexes[x]=index;
194 q->blue=blue;
195 q->green=green;
196 q->red=red;
197 length--;
198 q++;
199 }
200 if (SyncAuthenticPixels(image,exception) == MagickFalse)
201 break;
202 if (image->previous == (Image *) NULL)
203 {
204 status=SetImageProgress(image,LoadImageTag,y,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
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% unsigned long RegisterPIXImage(void)
273%
274*/
275ModuleExport unsigned long 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
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*/
307ModuleExport void UnregisterPIXImage(void)
308{
309 (void) UnregisterMagickInfo("PIX");
310}