]> 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-2012 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,exception);
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   image->colorspace=GRAYColorspace;
154   canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
155     exception);
156   (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod,
157     exception);
158   quantum_type=GrayQuantum;
159   quantum_info=AcquireQuantumInfo(image_info,canvas_image);
160   if (quantum_info == (QuantumInfo *) NULL)
161     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
162   pixels=GetQuantumPixels(quantum_info);
163   if (image_info->number_scenes != 0)
164     while (image->scene < image_info->scene)
165     {
166       /*
167         Skip to next image.
168       */
169       image->scene++;
170       length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
171       for (y=0; y < (ssize_t) image->rows; y++)
172       {
173         count=ReadBlob(image,length,pixels);
174         if (count != (ssize_t) length)
175           break;
176       }
177     }
178   scene=0;
179   count=0;
180   length=0;
181   do
182   {
183     /*
184       Read pixels to virtual canvas image then push to image.
185     */
186     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
187       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
188         break;
189     if (scene == 0)
190       {
191         length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
192         count=ReadBlob(image,length,pixels);
193       }
194     for (y=0; y < (ssize_t) image->extract_info.height; y++)
195     {
196       register const Quantum
197         *restrict p;
198
199       register ssize_t
200         x;
201
202       register Quantum
203         *restrict q;
204
205       if (count != (ssize_t) length)
206         {
207           ThrowFileException(exception,CorruptImageError,
208             "UnexpectedEndOfFile",image->filename);
209           break;
210         }
211       q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
212       if (q == (Quantum *) NULL)
213         break;
214       length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
215         quantum_type,pixels,exception);
216       if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
217         break;
218       if (((y-image->extract_info.y) >= 0) && 
219           ((y-image->extract_info.y) < (ssize_t) image->rows))
220         {
221           p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
222             image->columns,1,exception);
223           q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
224             1,exception);
225           if ((p == (const Quantum *) NULL) ||
226               (q == (Quantum *) NULL))
227             break;
228           for (x=0; x < (ssize_t) image->columns; x++)
229           {
230             SetPixelGray(image,GetPixelGray(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,exception);
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   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,ExceptionInfo *exception)
359 %
360 %  A description of each parameter follows.
361 %
362 %    o image_info: the image info.
363 %
364 %    o image:  The image.
365 %
366 %    o exception: return any errors or warnings in this structure.
367 %
368 */
369 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
370   Image *image,ExceptionInfo *exception)
371 {
372   MagickBooleanType
373     status;
374
375   MagickOffsetType
376     scene;
377
378   QuantumInfo
379     *quantum_info;
380
381   QuantumType
382     quantum_type;
383
384   size_t
385     length;
386
387   ssize_t
388     count,
389     y;
390
391   unsigned char
392     *pixels;
393
394   /*
395     Open output image file.
396   */
397   assert(image_info != (const ImageInfo *) NULL);
398   assert(image_info->signature == MagickSignature);
399   assert(image != (Image *) NULL);
400   assert(image->signature == MagickSignature);
401   if (image->debug != MagickFalse)
402     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
403   assert(exception != (ExceptionInfo *) NULL);
404   assert(exception->signature == MagickSignature);
405   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
406   if (status == MagickFalse)
407     return(status);
408   scene=0;
409   do
410   {
411     /*
412       Write grayscale pixels.
413     */
414     if (IsRGBColorspace(image->colorspace) == MagickFalse)
415       (void) TransformImageColorspace(image,sRGBColorspace,exception);
416     quantum_type=GrayQuantum;
417     quantum_info=AcquireQuantumInfo(image_info,image);
418     if (quantum_info == (QuantumInfo *) NULL)
419       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
420     pixels=GetQuantumPixels(quantum_info);
421     for (y=0; y < (ssize_t) image->rows; y++)
422     {
423       register const Quantum
424         *restrict p;
425
426       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
427       if (p == (const Quantum *) NULL)
428         break;
429       length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
430         quantum_type,pixels,exception);
431       count=WriteBlob(image,length,pixels);
432       if (count != (ssize_t) length)
433         break;
434       if (image->previous == (Image *) NULL)
435         {
436           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
437             image->rows);
438           if (status == MagickFalse)
439             break;
440         }
441     }
442     quantum_info=DestroyQuantumInfo(quantum_info);
443     if (GetNextImageInList(image) == (Image *) NULL)
444       break;
445     image=SyncNextImageInList(image);
446     status=SetImageProgress(image,SaveImagesTag,scene++,
447       GetImageListLength(image));
448     if (status == MagickFalse)
449       break;
450   } while (image_info->adjoin != MagickFalse);
451   (void) CloseBlob(image);
452   return(MagickTrue);
453 }