]> granicus.if.org Git - imagemagick/blob - coders/gray.c
(no commit message)
[imagemagick] / coders / gray.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                         GGGG  RRRR    AAA   Y   Y                           %
7 %                        G      R   R  A   A   Y Y                            %
8 %                        G  GG  RRRR   AAAAA    Y                             %
9 %                        G   G  R R    A   A    Y                             %
10 %                         GGG   R  R   A   A    Y                             %
11 %                                                                             %
12 %                                                                             %
13 %                    Read/Write RAW Gray 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/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colorspace.h"
47 #include "magick/constitute.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/pixel.h"
58 #include "magick/pixel-private.h"
59 #include "magick/quantum-private.h"
60 #include "magick/static.h"
61 #include "magick/statistic.h"
62 #include "magick/string_.h"
63 #include "magick/module.h"
64 \f
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteGRAYImage(const ImageInfo *,Image *);
70 \f
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   R e a d G R A Y I m a g e                                                 %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 %  ReadGRAYImage() reads an image of raw grayscale samples and returns
83 %  it.  It allocates the memory necessary for the new Image structure and
84 %   returns a pointer to the new image.
85 %
86 %  The format of the ReadGRAYImage method is:
87 %
88 %      Image *ReadGRAYImage(const ImageInfo *image_info,
89 %        ExceptionInfo *exception)
90 %
91 %  A description of each parameter follows:
92 %
93 %    o image_info: the image info.
94 %
95 %    o exception: return any errors or warnings in this structure.
96 %
97 */
98 static Image *ReadGRAYImage(const ImageInfo *image_info,
99   ExceptionInfo *exception)
100 {
101   Image
102     *canvas_image,
103     *image;
104
105   long
106     y;
107
108   MagickBooleanType
109     status;
110
111   MagickOffsetType
112     scene;
113
114   QuantumInfo
115     *quantum_info;
116
117   QuantumType
118     quantum_type;
119
120   register long
121     i;
122
123   ssize_t
124     count;
125
126   size_t
127     length;
128
129   unsigned char
130     *pixels;
131
132   /*
133     Open image file.
134   */
135   assert(image_info != (const ImageInfo *) NULL);
136   assert(image_info->signature == MagickSignature);
137   if (image_info->debug != MagickFalse)
138     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
139       image_info->filename);
140   assert(exception != (ExceptionInfo *) NULL);
141   assert(exception->signature == MagickSignature);
142   image=AcquireImage(image_info);
143   if ((image->columns == 0) || (image->rows == 0))
144     ThrowReaderException(OptionError,"MustSpecifyImageSize");
145   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
146   if (status == MagickFalse)
147     {
148       image=DestroyImageList(image);
149       return((Image *) NULL);
150     }
151   for (i=0; i < image->offset; i++)
152     if (ReadBlobByte(image) == EOF)
153       {
154         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
155           image->filename);
156         break;
157       }
158   /*
159     Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
160   */
161   canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
162     exception);
163   (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
164   quantum_type=GrayQuantum;
165   quantum_info=AcquireQuantumInfo(image_info,canvas_image);
166   if (quantum_info == (QuantumInfo *) NULL)
167     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
168   pixels=GetQuantumPixels(quantum_info);
169   if (image_info->number_scenes != 0)
170     while (image->scene < image_info->scene)
171     {
172       /*
173         Skip to next image.
174       */
175       image->scene++;
176       length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
177       for (y=0; y < (long) image->rows; y++)
178       {
179         count=ReadBlob(image,length,pixels);
180         if (count != (ssize_t) length)
181           break;
182       }
183     }
184   scene=0;
185   count=0;
186   length=0;
187   do
188   {
189     /*
190       Read pixels to virtual canvas image then push to image.
191     */
192     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
193       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
194         break;
195     if (scene == 0)
196       {
197         length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
198         count=ReadBlob(image,length,pixels);
199       }
200     for (y=0; y < (long) image->extract_info.height; y++)
201     {
202       register const PixelPacket
203         *restrict p;
204
205       register long
206         x;
207
208       register PixelPacket
209         *restrict q;
210
211       if (count != (ssize_t) length)
212         {
213           ThrowFileException(exception,CorruptImageError,
214             "UnexpectedEndOfFile",image->filename);
215           break;
216         }
217       q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
218       if (q == (PixelPacket *) NULL)
219         break;
220       length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
221         quantum_type,pixels,exception);
222       if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
223         break;
224       if (((y-image->extract_info.y) >= 0) && 
225           ((y-image->extract_info.y) < (long) image->rows))
226         {
227           p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
228             image->columns,1,exception);
229           q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
230             1,exception);
231           if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
232             break;
233           for (x=0; x < (long) image->columns; x++)
234           {
235             SetRedPixelComponent(q,GetRedPixelComponent(p));
236             SetGreenPixelComponent(q,GetGreenPixelComponent(p));
237             SetBluePixelComponent(q,GetBluePixelComponent(p));
238             p++;
239             q++;
240           }
241           if (SyncAuthenticPixels(image,exception) == MagickFalse)
242             break;
243         }
244       if (image->previous == (Image *) NULL)
245         {
246           status=SetImageProgress(image,LoadImageTag,y,image->rows);
247           if (status == MagickFalse)
248             break;
249         }
250       count=ReadBlob(image,length,pixels);
251     }
252     SetQuantumImageType(image,quantum_type);
253     /*
254       Proceed to next image.
255     */
256     if (image_info->number_scenes != 0)
257       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
258         break;
259     if (count == (ssize_t) length)
260       {
261         /*
262           Allocate next image structure.
263         */
264         AcquireNextImage(image_info,image);
265         if (GetNextImageInList(image) == (Image *) NULL)
266           {
267             image=DestroyImageList(image);
268             return((Image *) NULL);
269           }
270         image=SyncNextImageInList(image);
271         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
272           GetBlobSize(image));
273         if (status == MagickFalse)
274           break;
275       }
276     scene++;
277   } while (count == (ssize_t) length);
278   quantum_info=DestroyQuantumInfo(quantum_info);
279   InheritException(&image->exception,&canvas_image->exception);
280   canvas_image=DestroyImage(canvas_image);
281   (void) CloseBlob(image);
282   return(GetFirstImageInList(image));
283 }
284 \f
285 /*
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 %                                                                             %
288 %                                                                             %
289 %                                                                             %
290 %   R e g i s t e r G R A Y I m a g e                                         %
291 %                                                                             %
292 %                                                                             %
293 %                                                                             %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 %
296 %  RegisterGRAYImage() adds attributes for the GRAY image format to
297 %  the list of supported formats.  The attributes include the image format
298 %  tag, a method to read and/or write the format, whether the format
299 %  supports the saving of more than one frame to the same file or blob,
300 %  whether the format supports native in-memory I/O, and a brief
301 %  description of the format.
302 %
303 %  The format of the RegisterGRAYImage method is:
304 %
305 %      unsigned long RegisterGRAYImage(void)
306 %
307 */
308 ModuleExport unsigned long RegisterGRAYImage(void)
309 {
310   MagickInfo
311     *entry;
312
313   entry=SetMagickInfo("GRAY");
314   entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
315   entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
316   entry->raw=MagickTrue;
317   entry->endian_support=MagickTrue;
318   entry->format_type=ExplicitFormatType;
319   entry->description=ConstantString("Raw gray samples");
320   entry->module=ConstantString("GRAY");
321   (void) RegisterMagickInfo(entry);
322   return(MagickImageCoderSignature);
323 }
324 \f
325 /*
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 %                                                                             %
328 %                                                                             %
329 %                                                                             %
330 %   U n r e g i s t e r G R A Y I m a g e                                     %
331 %                                                                             %
332 %                                                                             %
333 %                                                                             %
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 %
336 %  UnregisterGRAYImage() removes format registrations made by the
337 %  GRAY module from the list of supported formats.
338 %
339 %  The format of the UnregisterGRAYImage method is:
340 %
341 %      UnregisterGRAYImage(void)
342 %
343 */
344 ModuleExport void UnregisterGRAYImage(void)
345 {
346   (void) UnregisterMagickInfo("GRAY");
347 }
348 \f
349 /*
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 %                                                                             %
352 %                                                                             %
353 %                                                                             %
354 %   W r i t e G R A Y I m a g e                                               %
355 %                                                                             %
356 %                                                                             %
357 %                                                                             %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 %
360 %  WriteGRAYImage() writes an image to a file as gray scale intensity
361 %  values.
362 %
363 %  The format of the WriteGRAYImage method is:
364 %
365 %      MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
366 %        Image *image)
367 %
368 %  A description of each parameter follows.
369 %
370 %    o image_info: the image info.
371 %
372 %    o image:  The image.
373 %
374 */
375 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
376   Image *image)
377 {
378   long
379     y;
380
381   MagickBooleanType
382     status;
383
384   MagickOffsetType
385     scene;
386
387   QuantumInfo
388     *quantum_info;
389
390   QuantumType
391     quantum_type;
392
393   ssize_t
394     count;
395
396   size_t
397     length;
398
399   unsigned char
400     *pixels;
401
402   /*
403     Open output image file.
404   */
405   assert(image_info != (const ImageInfo *) NULL);
406   assert(image_info->signature == MagickSignature);
407   assert(image != (Image *) NULL);
408   assert(image->signature == MagickSignature);
409   if (image->debug != MagickFalse)
410     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
411   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
412   if (status == MagickFalse)
413     return(status);
414   scene=0;
415   do
416   {
417     /*
418       Write grayscale pixels.
419     */
420     if (image->colorspace != RGBColorspace)
421       (void) TransformImageColorspace(image,RGBColorspace);
422     quantum_type=GrayQuantum;
423     quantum_info=AcquireQuantumInfo(image_info,image);
424     if (quantum_info == (QuantumInfo *) NULL)
425       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
426     pixels=GetQuantumPixels(quantum_info);
427     for (y=0; y < (long) image->rows; y++)
428     {
429       register const PixelPacket
430         *restrict p;
431
432       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
433       if (p == (const PixelPacket *) NULL)
434         break;
435       length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
436         quantum_type,pixels,&image->exception);
437       count=WriteBlob(image,length,pixels);
438       if (count != (ssize_t) length)
439         break;
440       if (image->previous == (Image *) NULL)
441         {
442           status=SetImageProgress(image,SaveImageTag,y,image->rows);
443           if (status == MagickFalse)
444             break;
445         }
446     }
447     quantum_info=DestroyQuantumInfo(quantum_info);
448     if (GetNextImageInList(image) == (Image *) NULL)
449       break;
450     image=SyncNextImageInList(image);
451     status=SetImageProgress(image,SaveImagesTag,scene++,
452       GetImageListLength(image));
453     if (status == MagickFalse)
454       break;
455   } while (image_info->adjoin != MagickFalse);
456   (void) CloseBlob(image);
457   return(MagickTrue);
458 }