blob: 41e4e8feb43028d09b18edb3a2a39a21521c3626 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/colormap.h"
47#include "MagickCore/exception.h"
48#include "MagickCore/exception-private.h"
49#include "MagickCore/image.h"
50#include "MagickCore/image-private.h"
51#include "MagickCore/list.h"
52#include "MagickCore/magick.h"
53#include "MagickCore/memory_.h"
54#include "MagickCore/monitor.h"
55#include "MagickCore/monitor-private.h"
56#include "MagickCore/pixel-accessor.h"
57#include "MagickCore/quantum-private.h"
58#include "MagickCore/static.h"
59#include "MagickCore/string_.h"
60#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000061
62/*
63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64% %
65% %
66% %
67% R e a d P I X I m a g e %
68% %
69% %
70% %
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72%
73% ReadPIXImage() reads a Alias/Wavefront RLE image file and returns it.
74% It allocates the memory necessary for the new Image structure and returns a
75% pointer to the new image.
76%
77% The format of the ReadPIXImage method is:
78%
79% Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
80%
81% A description of each parameter follows:
82%
83% o image_info: the image info.
84%
85% o exception: return any errors or warnings in this structure.
86%
87%
88*/
89static Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
90{
91 Image
92 *image;
93
cristy3ed852e2009-09-05 21:47:34 +000094 MagickBooleanType
95 status;
96
97 Quantum
98 blue,
99 green,
cristy4c08aed2011-07-01 19:47:50 +0000100 index,
cristy3ed852e2009-09-05 21:47:34 +0000101 red;
102
cristybb503372010-05-27 20:51:26 +0000103 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000104 x;
105
cristy4c08aed2011-07-01 19:47:50 +0000106 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000107 *q;
108
109 size_t
cristy3ed852e2009-09-05 21:47:34 +0000110 bits_per_pixel,
111 height,
cristyaff6d802011-04-26 01:46:31 +0000112 length,
cristy3ed852e2009-09-05 21:47:34 +0000113 width;
114
cristyaff6d802011-04-26 01:46:31 +0000115 ssize_t
116 y;
117
cristy3ed852e2009-09-05 21:47:34 +0000118 /*
119 Open image file.
120 */
121 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000122 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000123 if (image_info->debug != MagickFalse)
124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
125 image_info->filename);
126 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000127 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000128 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000129 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
130 if (status == MagickFalse)
131 {
132 image=DestroyImageList(image);
133 return((Image *) NULL);
134 }
135 /*
136 Read PIX image.
137 */
138 width=ReadBlobMSBShort(image);
139 height=ReadBlobMSBShort(image);
140 (void) ReadBlobMSBShort(image); /* x-offset */
141 (void) ReadBlobMSBShort(image); /* y-offset */
142 bits_per_pixel=ReadBlobMSBShort(image);
143 if ((width == 0UL) || (height == 0UL) || ((bits_per_pixel != 8) &&
144 (bits_per_pixel != 24)))
145 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
146 do
147 {
148 /*
149 Initialize image structure.
150 */
151 image->columns=width;
152 image->rows=height;
153 if (bits_per_pixel == 8)
cristy018f07f2011-09-04 21:15:19 +0000154 if (AcquireImageColormap(image,256,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000155 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
156 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
157 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
158 break;
cristyacabb842014-12-14 23:36:33 +0000159 status=SetImageExtent(image,image->columns,image->rows,exception);
160 if (status == MagickFalse)
161 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000162 /*
163 Convert PIX raster image to pixel packets.
164 */
165 red=(Quantum) 0;
166 green=(Quantum) 0;
167 blue=(Quantum) 0;
cristy4c08aed2011-07-01 19:47:50 +0000168 index=0;
cristy3ed852e2009-09-05 21:47:34 +0000169 length=0;
cristybb503372010-05-27 20:51:26 +0000170 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000171 {
172 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000173 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000174 break;
cristybb503372010-05-27 20:51:26 +0000175 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000176 {
177 if (length == 0)
178 {
179 length=(size_t) ReadBlobByte(image);
180 if (bits_per_pixel == 8)
181 index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
182 else
183 {
184 blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
185 green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
186 red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
187 }
188 }
189 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000190 SetPixelIndex(image,index,q);
191 SetPixelBlue(image,blue,q);
192 SetPixelGreen(image,green,q);
193 SetPixelRed(image,red,q);
cristy3ed852e2009-09-05 21:47:34 +0000194 length--;
cristyed231572011-07-14 02:18:59 +0000195 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000196 }
197 if (SyncAuthenticPixels(image,exception) == MagickFalse)
198 break;
199 if (image->previous == (Image *) NULL)
200 {
cristycee97112010-05-28 00:44:52 +0000201 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000202 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000203 if (status == MagickFalse)
204 break;
205 }
206 }
207 if (image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +0000208 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000209 if (EOFBlob(image) != MagickFalse)
210 {
211 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
212 image->filename);
213 break;
214 }
215 /*
216 Proceed to next image.
217 */
218 if (image_info->number_scenes != 0)
219 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
220 break;
221 width=ReadBlobMSBLong(image);
222 height=ReadBlobMSBLong(image);
223 (void) ReadBlobMSBShort(image);
224 (void) ReadBlobMSBShort(image);
225 bits_per_pixel=ReadBlobMSBShort(image);
226 status=(width != 0UL) && (height == 0UL) && ((bits_per_pixel == 8) ||
227 (bits_per_pixel == 24)) ? MagickTrue : MagickFalse;
cristycd8b3312013-12-22 01:51:11 +0000228 if (status != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000229 {
230 /*
231 Allocate next image structure.
232 */
cristy9950d572011-10-01 18:22:35 +0000233 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000234 if (GetNextImageInList(image) == (Image *) NULL)
235 {
236 image=DestroyImageList(image);
237 return((Image *) NULL);
238 }
239 image=SyncNextImageInList(image);
240 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
241 GetBlobSize(image));
242 if (status == MagickFalse)
243 break;
244 }
cristycd8b3312013-12-22 01:51:11 +0000245 } while (status != MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000246 (void) CloseBlob(image);
247 return(GetFirstImageInList(image));
248}
249
250/*
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252% %
253% %
254% %
255% R e g i s t e r P I X I m a g e %
256% %
257% %
258% %
259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260%
261% RegisterPIXImage() adds attributes for the PIX image format to
262% the list of supported formats. The attributes include the image format
263% tag, a method to read and/or write the format, whether the format
264% supports the saving of more than one frame to the same file or blob,
265% whether the format supports native in-memory I/O, and a brief
266% description of the format.
267%
268% The format of the RegisterPIXImage method is:
269%
cristybb503372010-05-27 20:51:26 +0000270% size_t RegisterPIXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000271%
272*/
cristybb503372010-05-27 20:51:26 +0000273ModuleExport size_t RegisterPIXImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000274{
275 MagickInfo
276 *entry;
277
dirk06b627a2015-04-06 18:59:17 +0000278 entry=AcquireMagickInfo("PIX","PIX","Alias/Wavefront RLE image format");
cristy3ed852e2009-09-05 21:47:34 +0000279 entry->decoder=(DecodeImageHandler *) ReadPIXImage;
cristy3ed852e2009-09-05 21:47:34 +0000280 (void) RegisterMagickInfo(entry);
281 return(MagickImageCoderSignature);
282}
283
284/*
285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286% %
287% %
288% %
289% U n r e g i s t e r P I X I m a g e %
290% %
291% %
292% %
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294%
295% UnregisterPIXImage() removes format registrations made by the
296% PIX module from the list of supported formats.
297%
298% The format of the UnregisterPIXImage method is:
299%
300% UnregisterPIXImage(void)
301%
302*/
303ModuleExport void UnregisterPIXImage(void)
304{
305 (void) UnregisterMagickInfo("PIX");
306}