]> granicus.if.org Git - imagemagick/blob - coders/rla.c
(no commit message)
[imagemagick] / coders / rla.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            RRRR   L       AAA                               %
7 %                            R   R  L      A   A                              %
8 %                            RRRR   L      AAAAA                              %
9 %                            R R    L      A   A                              %
10 %                            R  R   LLLLL  A   A                              %
11 %                                                                             %
12 %                                                                             %
13 %                      Read Alias/Wavefront 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/property.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.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 R L A I m a g e                                                   %
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %
72 %  ReadRLAImage() reads a run-length encoded Wavefront RLA image file
73 %  and returns it.  It allocates the memory necessary for the new Image
74 %  structure and returns a pointer to the new image.
75 %
76 %  Note:  This module was contributed by Lester Vecsey (master@internexus.net).
77 %
78 %  The format of the ReadRLAImage method is:
79 %
80 %      Image *ReadRLAImage(const ImageInfo *image_info,ExceptionInfo *exception)
81 %
82 %  A description of each parameter follows:
83 %
84 %    o image_info: the image info.
85 %
86 %    o exception: return any errors or warnings in this structure.
87 %
88 */
89 static Image *ReadRLAImage(const ImageInfo *image_info,ExceptionInfo *exception)
90 {
91   typedef struct _WindowFrame
92   {
93     short
94       left,
95       right,
96       bottom,
97       top;
98   } WindowFrame;
99
100   typedef struct _RLAInfo
101   {
102     WindowFrame
103       window,
104       active_window;
105
106     short
107       frame,
108       storage_type,
109       number_channels,
110       number_matte_channels,
111       number_auxiliary_channels,
112       revision;
113
114     char
115       gamma[16],
116       red_primary[24],
117       green_primary[24],
118       blue_primary[24],
119       white_point[24];
120
121     ssize_t
122       job_number;
123
124     char
125       name[128],
126       description[128],
127       program[64],
128       machine[32],
129       user[32],
130       date[20],
131       aspect[24],
132       aspect_ratio[8],
133       chan[32];
134
135     short
136       field;
137
138     char
139       time[12],
140       filter[32];
141
142     short
143       bits_per_channel,
144       matte_type,
145       matte_bits,
146       auxiliary_type,
147       auxiliary_bits;
148
149     char
150       auxiliary[32],
151       space[36];
152
153     ssize_t
154       next;
155   } RLAInfo;
156
157   Image
158     *image;
159
160   int
161     channel,
162     length,
163     runlength;
164
165   ssize_t
166     y;
167
168   ssize_t
169     *scanlines;
170
171   MagickBooleanType
172     status;
173
174   MagickOffsetType
175     offset;
176
177   register ssize_t
178     i,
179     x;
180
181   register PixelPacket
182     *q;
183
184   ssize_t
185     count;
186
187   RLAInfo
188     rla_info;
189
190   unsigned char
191     byte;
192
193   /*
194     Open image file.
195   */
196   assert(image_info != (const ImageInfo *) NULL);
197   assert(image_info->signature == MagickSignature);
198   if (image_info->debug != MagickFalse)
199     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
200       image_info->filename);
201   assert(exception != (ExceptionInfo *) NULL);
202   assert(exception->signature == MagickSignature);
203   image=AcquireImage(image_info);
204   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
205   if (status == MagickFalse)
206     {
207       image=DestroyImageList(image);
208       return((Image *) NULL);
209     }
210   rla_info.window.left=(short) ReadBlobMSBShort(image);
211   rla_info.window.right=(short) ReadBlobMSBShort(image);
212   rla_info.window.bottom=(short) ReadBlobMSBShort(image);
213   rla_info.window.top=(short) ReadBlobMSBShort(image);
214   rla_info.active_window.left=(short) ReadBlobMSBShort(image);
215   rla_info.active_window.right=(short) ReadBlobMSBShort(image);
216   rla_info.active_window.bottom=(short) ReadBlobMSBShort(image);
217   rla_info.active_window.top=(short) ReadBlobMSBShort(image);
218   rla_info.frame=(short) ReadBlobMSBShort(image);
219   rla_info.storage_type=(short) ReadBlobMSBShort(image);
220   rla_info.number_channels=(short) ReadBlobMSBShort(image);
221   rla_info.number_matte_channels=(short) ReadBlobMSBShort(image);
222   if (rla_info.number_channels == 0)
223     rla_info.number_channels=3;
224   rla_info.number_channels+=rla_info.number_matte_channels;
225   rla_info.number_auxiliary_channels=(short) ReadBlobMSBShort(image);
226   rla_info.revision=(short) ReadBlobMSBShort(image);
227   count=ReadBlob(image,16,(unsigned char *) rla_info.gamma);
228   count=ReadBlob(image,24,(unsigned char *) rla_info.red_primary);
229   count=ReadBlob(image,24,(unsigned char *) rla_info.green_primary);
230   count=ReadBlob(image,24,(unsigned char *) rla_info.blue_primary);
231   count=ReadBlob(image,24,(unsigned char *) rla_info.white_point);
232   rla_info.job_number=(ssize_t) ReadBlobMSBLong(image);
233   count=ReadBlob(image,128,(unsigned char *) rla_info.name);
234   count=ReadBlob(image,128,(unsigned char *) rla_info.description);
235   count=ReadBlob(image,64,(unsigned char *) rla_info.program);
236   count=ReadBlob(image,32,(unsigned char *) rla_info.machine);
237   count=ReadBlob(image,32,(unsigned char *) rla_info.user);
238   count=ReadBlob(image,20,(unsigned char *) rla_info.date);
239   count=ReadBlob(image,24,(unsigned char *) rla_info.aspect);
240   count=ReadBlob(image,8,(unsigned char *) rla_info.aspect_ratio);
241   count=ReadBlob(image,32,(unsigned char *) rla_info.chan);
242   rla_info.field=(short) ReadBlobMSBShort(image);
243   count=ReadBlob(image,12,(unsigned char *) rla_info.time);
244   count=ReadBlob(image,32,(unsigned char *) rla_info.filter);
245   rla_info.bits_per_channel=(short) ReadBlobMSBShort(image);
246   rla_info.matte_type=(short) ReadBlobMSBShort(image);
247   rla_info.matte_bits=(short) ReadBlobMSBShort(image);
248   rla_info.auxiliary_type=(short) ReadBlobMSBShort(image);
249   rla_info.auxiliary_bits=(short) ReadBlobMSBShort(image);
250   count=ReadBlob(image,32,(unsigned char *) rla_info.auxiliary);
251   count=ReadBlob(image,36,(unsigned char *) rla_info.space);
252   if ((size_t) count != 36)
253     ThrowReaderException(CorruptImageError,"UnableToReadImageData");
254   rla_info.next=(ssize_t) ReadBlobMSBLong(image);
255   /*
256     Initialize image structure.
257   */
258   image->matte=rla_info.number_matte_channels != 0 ? MagickTrue : MagickFalse;
259   image->columns=1UL*rla_info.active_window.right-rla_info.active_window.left+1;
260   image->rows=1UL*rla_info.active_window.top-rla_info.active_window.bottom+1;
261   if (image_info->ping != MagickFalse)
262     {
263       (void) CloseBlob(image);
264       return(GetFirstImageInList(image));
265     }
266   scanlines=(ssize_t *) AcquireQuantumMemory(image->rows,sizeof(*scanlines));
267   if (scanlines == (ssize_t *) NULL)
268     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
269   if (*rla_info.description != '\0')
270     (void) SetImageProperty(image,"comment",rla_info.description);
271   /*
272     Read offsets to each scanline data.
273   */
274   for (i=0; i < (ssize_t) image->rows; i++)
275     scanlines[i]=(ssize_t) ReadBlobMSBLong(image);
276   /*
277     Read image data.
278   */
279   x=0;
280   for (y=0; y < (ssize_t) image->rows; y++)
281   {
282     offset=SeekBlob(image,scanlines[image->rows-y-1],SEEK_SET);
283     if (offset < 0)
284       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
285     for (channel=0; channel < (int) rla_info.number_channels; channel++)
286     {
287       length=(int) ReadBlobMSBShort(image);
288       while (length > 0)
289       {
290         byte=(unsigned char) ReadBlobByte(image);
291         runlength=byte;
292         if (byte > 127)
293           runlength=byte-256;
294         length--;
295         if (length == 0)
296           break;
297         if (runlength < 0)
298           {
299             while (runlength < 0)
300             {
301               q=GetAuthenticPixels(image,(ssize_t) (x % image->columns),
302                 (ssize_t) (y % image->rows),1,1,exception);
303               if (q == (PixelPacket *) NULL)
304                 break;
305               byte=(unsigned char) ReadBlobByte(image);
306               length--;
307               switch (channel)
308               {
309                 case 0:
310                 {
311                   q->red=ScaleCharToQuantum(byte);
312                   break;
313                 }
314                 case 1:
315                 {
316                   q->green=ScaleCharToQuantum(byte);
317                   break;
318                 }
319                 case 2:
320                 {
321                   q->blue=ScaleCharToQuantum(byte);
322                   break;
323                 }
324                 case 3:
325                 default:
326                 {
327                   q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(byte));
328                   break;
329                 }
330               }
331               if (SyncAuthenticPixels(image,exception) == MagickFalse)
332                 break;
333               x++;
334               runlength++;
335             }
336             continue;
337           }
338         byte=(unsigned char) ReadBlobByte(image);
339         length--;
340         runlength++;
341         do
342         {
343           q=GetAuthenticPixels(image,(ssize_t) (x % image->columns),
344             (ssize_t) (y % image->rows),1,1,exception);
345           if (q == (PixelPacket *) NULL)
346             break;
347           switch (channel)
348           {
349             case 0:
350             {
351               q->red=ScaleCharToQuantum(byte);
352               break;
353             }
354             case 1:
355             {
356               q->green=ScaleCharToQuantum(byte);
357               break;
358             }
359             case 2:
360             {
361               q->blue=ScaleCharToQuantum(byte);
362               break;
363             }
364             case 3:
365             default:
366             {
367               q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(byte));
368               break;
369             }
370           }
371           if (SyncAuthenticPixels(image,exception) == MagickFalse)
372             break;
373           x++;
374           runlength--;
375         }
376         while (runlength > 0);
377       }
378     }
379     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
380                 image->rows);
381     if (status == MagickFalse)
382       break;
383   }
384   if (EOFBlob(image) != MagickFalse)
385     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
386       image->filename);
387   (void) CloseBlob(image);
388   return(GetFirstImageInList(image));
389 }
390 \f
391 /*
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 %                                                                             %
394 %                                                                             %
395 %                                                                             %
396 %   R e g i s t e r R L A I m a g e                                           %
397 %                                                                             %
398 %                                                                             %
399 %                                                                             %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 %
402 %  RegisterRLAImage() adds attributes for the RLA image format to
403 %  the list of supported formats.  The attributes include the image format
404 %  tag, a method to read and/or write the format, whether the format
405 %  supports the saving of more than one frame to the same file or blob,
406 %  whether the format supports native in-memory I/O, and a brief
407 %  description of the format.
408 %
409 %  The format of the RegisterRLAImage method is:
410 %
411 %      size_t RegisterRLAImage(void)
412 %
413 */
414 ModuleExport size_t RegisterRLAImage(void)
415 {
416   MagickInfo
417     *entry;
418
419   entry=SetMagickInfo("RLA");
420   entry->decoder=(DecodeImageHandler *) ReadRLAImage;
421   entry->adjoin=MagickFalse;
422   entry->description=ConstantString("Alias/Wavefront image");
423   entry->module=ConstantString("RLA");
424   (void) RegisterMagickInfo(entry);
425   return(MagickImageCoderSignature);
426 }
427 \f
428 /*
429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 %                                                                             %
431 %                                                                             %
432 %                                                                             %
433 %   U n r e g i s t e r R L A I m a g e                                       %
434 %                                                                             %
435 %                                                                             %
436 %                                                                             %
437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 %
439 %  UnregisterRLAImage() removes format registrations made by the
440 %  RLA module from the list of supported formats.
441 %
442 %  The format of the UnregisterRLAImage method is:
443 %
444 %      UnregisterRLAImage(void)
445 %
446 */
447 ModuleExport void UnregisterRLAImage(void)
448 {
449   (void) UnregisterMagickInfo("RLA");
450 }