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