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