root / ImageMagick / trunk / coders / avs.c

Revision 11742, 14.9 kB (checked in by cristy, 3 weeks ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%                             AAA   V   V  SSSSS                              %
7%                            A   A  V   V  SS                                 %
8%                            AAAAA  V   V   SSS                               %
9%                            A   A   V V      SS                              %
10%                            A   A    V    SSSSS                              %
11%                                                                             %
12%                                                                             %
13%                        Read/Write AVS X Image Format.                       %
14%                                                                             %
15%                              Software Design                                %
16%                                John Cristy                                  %
17%                                 July 1992                                   %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2008 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
39/*
40  Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/colorspace.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  Forward declarations.
62*/
63static MagickBooleanType
64  WriteAVSImage(const ImageInfo *,Image *);
65
66/*
67%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68%                                                                             %
69%                                                                             %
70%                                                                             %
71%   R e a d A V S I m a g e                                                   %
72%                                                                             %
73%                                                                             %
74%                                                                             %
75%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76%
77%  ReadAVSImage() reads an AVS X image file and returns it.  It
78%  allocates the memory necessary for the new Image structure and returns a
79%  pointer to the new image.
80%
81%  The format of the ReadAVSImage method is:
82%
83%      Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
84%
85%  A description of each parameter follows:
86%
87%    o image_info: the image info.
88%
89%    o exception: return any errors or warnings in this structure.
90%
91*/
92static Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
93{
94  Image
95    *image;
96
97  long
98    y;
99
100  MagickBooleanType
101    status;
102
103  register long
104    x;
105
106  register PixelPacket
107    *q;
108
109  register unsigned char
110    *p;
111
112  ssize_t
113    count;
114
115  size_t
116    length;
117
118  unsigned char
119    *pixels;
120
121  unsigned long
122    height,
123    width;
124
125  /*
126    Open image file.
127  */
128  assert(image_info != (const ImageInfo *) NULL);
129  assert(image_info->signature == MagickSignature);
130  if (image_info->debug != MagickFalse)
131    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132      image_info->filename);
133  assert(exception != (ExceptionInfo *) NULL);
134  assert(exception->signature == MagickSignature);
135  image=AcquireImage(image_info);
136  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
137  if (status == MagickFalse)
138    {
139      image=DestroyImageList(image);
140      return((Image *) NULL);
141    }
142  /*
143    Read AVS X image.
144  */
145  width=ReadBlobMSBLong(image);
146  height=ReadBlobMSBLong(image);
147  if (EOFBlob(image) != MagickFalse)
148    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
149  if ((width == 0UL) || (height == 0UL))
150    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151  do
152  {
153    /*
154      Convert AVS raster image to pixel packets.
155    */
156    image->columns=width;
157    image->rows=height;
158    image->depth=8;
159    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
160      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
161        break;
162    if (SetImageExtent(image,0,0) == MagickFalse)
163      {
164        InheritException(exception,&image->exception);
165        return(DestroyImageList(image));
166      }
167    length=(size_t) image->columns;
168    pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
169    if (pixels == (unsigned char *) NULL)
170      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
171    length*=4*sizeof(*pixels);
172    for (y=0; y < (long) image->rows; y++)
173    {
174      count=ReadBlob(image,length,pixels);
175      if ((size_t) count != length)
176        ThrowReaderException(CorruptImageError,"UnableToReadImageData");
177      p=pixels;
178      q=SetImagePixels(image,0,y,image->columns,1);
179      if (q == (PixelPacket *) NULL)
180        break;
181      for (x=0; x < (long) image->columns; x++)
182      {
183        q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
184        q->red=ScaleCharToQuantum(*p++);
185        q->green=ScaleCharToQuantum(*p++);
186        q->blue=ScaleCharToQuantum(*p++);
187        if (q->opacity != OpaqueOpacity)
188          image->matte=MagickTrue;
189        q++;
190      }
191      if (SyncImagePixels(image) == MagickFalse)
192        break;
193      if (image->previous == (Image *) NULL)
194        if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
195            (QuantumTick(y,image->rows) != MagickFalse))
196          {
197            status=image->progress_monitor(LoadImageTag,y,image->rows,
198              image->client_data);
199            if (status == MagickFalse)
200              break;
201          }
202    }
203    pixels=(unsigned char *) RelinquishMagickMemory(pixels);
204    if (EOFBlob(image) != MagickFalse)
205      {
206        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
207          image->filename);
208        break;
209      }
210    /*
211      Proceed to next image.
212    */
213    if (image_info->number_scenes != 0)
214      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
215        break;
216    width=ReadBlobMSBLong(image);
217    height=ReadBlobMSBLong(image);
218    if ((width != 0UL) && (height != 0UL))
219      {
220        /*
221          Allocate next image structure.
222        */
223        AcquireNextImage(image_info,image);
224        if (GetNextImageInList(image) == (Image *) NULL)
225          {
226            image=DestroyImageList(image);
227            return((Image *) NULL);
228          }
229        image=SyncNextImageInList(image);
230        if (image->progress_monitor != (MagickProgressMonitor) NULL)
231          {
232            status=image->progress_monitor(LoadImagesTag,TellBlob(image),
233              GetBlobSize(image),image->client_data);
234            if (status == MagickFalse)
235              break;
236          }
237      }
238  } while ((width != 0UL) && (height != 0UL));
239  (void) CloseBlob(image);
240  return(GetFirstImageInList(image));
241}
242
243/*
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245%                                                                             %
246%                                                                             %
247%                                                                             %
248%   R e g i s t e r A V S I m a g e                                           %
249%                                                                             %
250%                                                                             %
251%                                                                             %
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253%
254%  RegisterAVSImage() adds attributes for the AVS X image format to the list
255%  of supported formats.  The attributes include the image format tag, a
256%  method to read and/or write the format, whether the format supports the
257%  saving of more than one frame to the same file or blob, whether the format
258%  supports native in-memory I/O, and a brief description of the format.
259%
260%  The format of the RegisterAVSImage method is:
261%
262%      unsigned long RegisterAVSImage(void)
263%
264*/
265ModuleExport unsigned long RegisterAVSImage(void)
266{
267  MagickInfo
268    *entry;
269
270  entry=SetMagickInfo("AVS");
271  entry->decoder=(DecodeImageHandler *) ReadAVSImage;
272  entry->encoder=(EncodeImageHandler *) WriteAVSImage;
273  entry->description=ConstantString("AVS X image");
274  entry->module=ConstantString("AVS");
275  (void) RegisterMagickInfo(entry);
276  return(MagickImageCoderSignature);
277}
278
279/*
280%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281%                                                                             %
282%                                                                             %
283%                                                                             %
284%   U n r e g i s t e r A V S I m a g e                                       %
285%                                                                             %
286%                                                                             %
287%                                                                             %
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289%
290%  UnregisterAVSImage() removes format registrations made by the
291%  AVS module from the list of supported formats.
292%
293%  The format of the UnregisterAVSImage method is:
294%
295%      UnregisterAVSImage(void)
296%
297*/
298ModuleExport void UnregisterAVSImage(void)
299{
300  (void) UnregisterMagickInfo("AVS");
301}
302
303/*
304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305%                                                                             %
306%                                                                             %
307%                                                                             %
308%   W r i t e A V S I m a g e                                                 %
309%                                                                             %
310%                                                                             %
311%                                                                             %
312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313%
314%  WriteAVSImage() writes an image to a file in AVS X image format.
315%
316%  The format of the WriteAVSImage method is:
317%
318%      MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
319%
320%  A description of each parameter follows.
321%
322%    o image_info: the image info.
323%
324%    o image:  The image.
325%
326*/
327static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
328{
329  MagickBooleanType
330    status;
331
332  MagickOffsetType
333    scene;
334
335  register const PixelPacket
336    *p;
337
338  register long
339    x,
340    y;
341
342  register unsigned char
343    *q;
344
345  unsigned char
346    *pixels;
347
348  /*
349    Open output image file.
350  */
351  assert(image_info != (const ImageInfo *) NULL);
352  assert(image_info->signature == MagickSignature);
353  assert(image != (Image *) NULL);
354  assert(image->signature == MagickSignature);
355  if (image->debug != MagickFalse)
356    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
357  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
358  if (status == MagickFalse)
359    return(status);
360  scene=0;
361  do
362  {
363    /*
364      Write AVS header.
365    */
366    if (image->colorspace != RGBColorspace)
367      (void) SetImageColorspace(image,RGBColorspace);
368    (void) WriteBlobMSBLong(image,image->columns);
369    (void) WriteBlobMSBLong(image,image->rows);
370    /*
371      Allocate memory for pixels.
372    */
373    pixels=(unsigned char *) AcquireMagickMemory((size_t) image->columns*
374      4*sizeof(*pixels));
375    if (pixels == (unsigned char *) NULL)
376      ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
377    /*
378      Convert MIFF to AVS raster pixels.
379    */
380    for (y=0; y < (long) image->rows; y++)
381    {
382      p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
383      if (p == (PixelPacket *) NULL)
384        break;
385      q=pixels;
386      for (x=0; x < (long) image->columns; x++)
387      {
388        *q++=ScaleQuantumToChar((Quantum) (QuantumRange-
389          (image->matte != MagickFalse ? p->opacity : OpaqueOpacity)));
390        *q++=ScaleQuantumToChar(p->red);
391        *q++=ScaleQuantumToChar(p->green);
392        *q++=ScaleQuantumToChar(p->blue);
393        p++;
394      }
395      (void) WriteBlob(image,(size_t) (q-pixels),pixels);
396      if (image->previous == (Image *) NULL)
397        if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
398            (QuantumTick(y,image->rows) != MagickFalse))
399          {
400            status=image->progress_monitor(SaveImageTag,y,image->rows,
401              image->client_data);
402            if (status == MagickFalse)
403              break;
404          }
405    }
406    pixels=(unsigned char *) RelinquishMagickMemory(pixels);
407    if (GetNextImageInList(image) == (Image *) NULL)
408      break;
409    image=SyncNextImageInList(image);
410    if (image->progress_monitor != (MagickProgressMonitor) NULL)
411      {
412        status=image->progress_monitor(SaveImagesTag,scene,
413          GetImageListLength(image),image->client_data);
414        if (status == MagickFalse)
415          break;
416      }
417    scene++;
418  } while (image_info->adjoin != MagickFalse);
419  (void) CloseBlob(image);
420  return(MagickTrue);
421}
Note: See TracBrowser for help on using the browser.