]> 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   ssize_t
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 ssize_t
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 < (ssize_t) 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 < (ssize_t) image->extract_info.height; y++)
201     {
202       register const PixelPacket
203         *restrict p;
204
205       register ssize_t
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) < (ssize_t) 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 < (ssize_t) 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,(MagickOffsetType) y,
247                 image->rows);
248           if (status == MagickFalse)
249             break;
250         }
251       count=ReadBlob(image,length,pixels);
252     }
253     SetQuantumImageType(image,quantum_type);
254     /*
255       Proceed to next image.
256     */
257     if (image_info->number_scenes != 0)
258       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
259         break;
260     if (count == (ssize_t) length)
261       {
262         /*
263           Allocate next image structure.
264         */
265         AcquireNextImage(image_info,image);
266         if (GetNextImageInList(image) == (Image *) NULL)
267           {
268             image=DestroyImageList(image);
269             return((Image *) NULL);
270           }
271         image=SyncNextImageInList(image);
272         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
273           GetBlobSize(image));
274         if (status == MagickFalse)
275           break;
276       }
277     scene++;
278   } while (count == (ssize_t) length);
279   quantum_info=DestroyQuantumInfo(quantum_info);
280   InheritException(&image->exception,&canvas_image->exception);
281   canvas_image=DestroyImage(canvas_image);
282   (void) CloseBlob(image);
283   return(GetFirstImageInList(image));
284 }
285 \f
286 /*
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 %                                                                             %
289 %                                                                             %
290 %                                                                             %
291 %   R e g i s t e r G R A Y I m a g e                                         %
292 %                                                                             %
293 %                                                                             %
294 %                                                                             %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 %
297 %  RegisterGRAYImage() adds attributes for the GRAY image format to
298 %  the list of supported formats.  The attributes include the image format
299 %  tag, a method to read and/or write the format, whether the format
300 %  supports the saving of more than one frame to the same file or blob,
301 %  whether the format supports native in-memory I/O, and a brief
302 %  description of the format.
303 %
304 %  The format of the RegisterGRAYImage method is:
305 %
306 %      size_t RegisterGRAYImage(void)
307 %
308 */
309 ModuleExport size_t RegisterGRAYImage(void)
310 {
311   MagickInfo
312     *entry;
313
314   entry=SetMagickInfo("GRAY");
315   entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
316   entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
317   entry->raw=MagickTrue;
318   entry->endian_support=MagickTrue;
319   entry->format_type=ExplicitFormatType;
320   entry->description=ConstantString("Raw gray samples");
321   entry->module=ConstantString("GRAY");
322   (void) RegisterMagickInfo(entry);
323   return(MagickImageCoderSignature);
324 }
325 \f
326 /*
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 %                                                                             %
329 %                                                                             %
330 %                                                                             %
331 %   U n r e g i s t e r G R A Y I m a g e                                     %
332 %                                                                             %
333 %                                                                             %
334 %                                                                             %
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 %
337 %  UnregisterGRAYImage() removes format registrations made by the
338 %  GRAY module from the list of supported formats.
339 %
340 %  The format of the UnregisterGRAYImage method is:
341 %
342 %      UnregisterGRAYImage(void)
343 %
344 */
345 ModuleExport void UnregisterGRAYImage(void)
346 {
347   (void) UnregisterMagickInfo("GRAY");
348 }
349 \f
350 /*
351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 %                                                                             %
353 %                                                                             %
354 %                                                                             %
355 %   W r i t e G R A Y I m a g e                                               %
356 %                                                                             %
357 %                                                                             %
358 %                                                                             %
359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 %
361 %  WriteGRAYImage() writes an image to a file as gray scale intensity
362 %  values.
363 %
364 %  The format of the WriteGRAYImage method is:
365 %
366 %      MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
367 %        Image *image)
368 %
369 %  A description of each parameter follows.
370 %
371 %    o image_info: the image info.
372 %
373 %    o image:  The image.
374 %
375 */
376 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
377   Image *image)
378 {
379   ssize_t
380     y;
381
382   MagickBooleanType
383     status;
384
385   MagickOffsetType
386     scene;
387
388   QuantumInfo
389     *quantum_info;
390
391   QuantumType
392     quantum_type;
393
394   ssize_t
395     count;
396
397   size_t
398     length;
399
400   unsigned char
401     *pixels;
402
403   /*
404     Open output image file.
405   */
406   assert(image_info != (const ImageInfo *) NULL);
407   assert(image_info->signature == MagickSignature);
408   assert(image != (Image *) NULL);
409   assert(image->signature == MagickSignature);
410   if (image->debug != MagickFalse)
411     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
412   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
413   if (status == MagickFalse)
414     return(status);
415   scene=0;
416   do
417   {
418     /*
419       Write grayscale pixels.
420     */
421     if (image->colorspace != RGBColorspace)
422       (void) TransformImageColorspace(image,RGBColorspace);
423     quantum_type=GrayQuantum;
424     quantum_info=AcquireQuantumInfo(image_info,image);
425     if (quantum_info == (QuantumInfo *) NULL)
426       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
427     pixels=GetQuantumPixels(quantum_info);
428     for (y=0; y < (ssize_t) image->rows; y++)
429     {
430       register const PixelPacket
431         *restrict p;
432
433       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
434       if (p == (const PixelPacket *) NULL)
435         break;
436       length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
437         quantum_type,pixels,&image->exception);
438       count=WriteBlob(image,length,pixels);
439       if (count != (ssize_t) length)
440         break;
441       if (image->previous == (Image *) NULL)
442         {
443           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
444                 image->rows);
445           if (status == MagickFalse)
446             break;
447         }
448     }
449     quantum_info=DestroyQuantumInfo(quantum_info);
450     if (GetNextImageInList(image) == (Image *) NULL)
451       break;
452     image=SyncNextImageInList(image);
453     status=SetImageProgress(image,SaveImagesTag,scene++,
454       GetImageListLength(image));
455     if (status == MagickFalse)
456       break;
457   } while (image_info->adjoin != MagickFalse);
458   (void) CloseBlob(image);
459   return(MagickTrue);
460 }