Powered by Pair ImageMagick logo
Image Magick
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

ImageMagick-6.1.1/coders/gray.c

Go to the documentation of this file.
00001 /* 00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00003 % % 00004 % % 00005 % % 00006 % GGGG RRRR AAA Y Y % 00007 % G R R A A Y Y % 00008 % G GG RRRR AAAAA Y % 00009 % G G R R A A Y % 00010 % GGG R R A A Y % 00011 % % 00012 % % 00013 % Read/Write RAW Gray Image Format. % 00014 % % 00015 % Software Design % 00016 % John Cristy % 00017 % July 1992 % 00018 % % 00019 % % 00020 % Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization % 00021 % dedicated to making software imaging solutions freely available. % 00022 % % 00023 % You may not use this file except in compliance with the License. You may % 00024 % obtain a copy of the License at % 00025 % % 00026 % http://www.imagemagick.org/www/Copyright.html % 00027 % % 00028 % Unless required by applicable law or agreed to in writing, software % 00029 % distributed under the License is distributed on an "AS IS" BASIS, % 00030 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % 00031 % See the License for the specific language governing permissions and % 00032 % limitations under the License. % 00033 % % 00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00035 % 00036 % 00037 */ 00038 00039 /* 00040 Include declarations. 00041 */ 00042 #include "magick/studio.h" 00043 #include "magick/blob.h" 00044 #include "magick/blob_private.h" 00045 #include "magick/colorspace.h" 00046 #include "magick/constitute.h" 00047 #include "magick/error.h" 00048 #include "magick/error_private.h" 00049 #include "magick/image.h" 00050 #include "magick/image_private.h" 00051 #include "magick/list.h" 00052 #include "magick/magick.h" 00053 #include "magick/memory_.h" 00054 #include "magick/monitor.h" 00055 #include "magick/static.h" 00056 #include "magick/string_.h" 00057 00058 /* 00059 Forward declarations. 00060 */ 00061 static MagickBooleanType 00062 WriteGRAYImage(const ImageInfo *,Image *); 00063 00064 /* 00065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00066 % % 00067 % % 00068 % % 00069 % R e a d G R A Y I m a g e % 00070 % % 00071 % % 00072 % % 00073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00074 % 00075 % ReadGRAYImage() reads an image of raw grayscale samples and returns 00076 % it. It allocates the memory necessary for the new Image structure and 00077 % returns a pointer to the new image. 00078 % 00079 % The format of the ReadGRAYImage method is: 00080 % 00081 % Image *ReadGRAYImage(const ImageInfo *image_info, 00082 % ExceptionInfo *exception) 00083 % 00084 % A description of each parameter follows: 00085 % 00086 % o image_info: The image info. 00087 % 00088 % o exception: return any errors or warnings in this structure. 00089 % 00090 % 00091 */ 00092 static Image *ReadGRAYImage(const ImageInfo *image_info, 00093 ExceptionInfo *exception) 00094 { 00095 Image 00096 *image; 00097 00098 long 00099 j, 00100 y; 00101 00102 MagickBooleanType 00103 status; 00104 00105 MagickOffsetType 00106 offset; 00107 00108 register long 00109 i, 00110 x; 00111 00112 register PixelPacket 00113 *q; 00114 00115 ssize_t 00116 count; 00117 00118 size_t 00119 packet_size; 00120 00121 unsigned char 00122 *scanline; 00123 00124 unsigned long 00125 depth, 00126 width; 00127 00128 /* 00129 Open image file. 00130 */ 00131 assert(image_info != (const ImageInfo *) NULL); 00132 assert(image_info->signature == MagickSignature); 00133 if (image_info->debug != MagickFalse) 00134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),image_info->filename); 00135 assert(exception != (ExceptionInfo *) NULL); 00136 assert(exception->signature == MagickSignature); 00137 image=AllocateImage(image_info); 00138 if ((image->columns == 0) || (image->rows == 0)) 00139 ThrowReaderException(OptionError,"MustSpecifyImageSize"); 00140 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); 00141 if (status == MagickFalse) 00142 { 00143 DestroyImageList(image); 00144 return((Image *) NULL); 00145 } 00146 for (i=0; i < image->offset; i++) 00147 (void) ReadBlobByte(image); 00148 /* 00149 Allocate memory for a scanline. 00150 */ 00151 depth=GetImageQuantumDepth(image,MagickTrue); 00152 packet_size=(size_t) (depth/8); 00153 scanline=(unsigned char *) 00154 AcquireMagickMemory(packet_size*image->extract_info.width); 00155 if (scanline == (unsigned char *) NULL) 00156 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); 00157 if (image_info->number_scenes != 0) 00158 while (image->scene < image_info->scene) 00159 { 00160 /* 00161 Skip to next image. 00162 */ 00163 image->scene++; 00164 for (y=0; y < (long) image->rows; y++) 00165 (void) ReadBlob(image,packet_size*image->extract_info.width,scanline); 00166 } 00167 offset=(MagickOffsetType) (packet_size*image->extract_info.x); 00168 do 00169 { 00170 /* 00171 Convert raster image to pixel packets. 00172 */ 00173 if ((image->depth <= QuantumDepth) && (image->depth <= 16)) 00174 if (AllocateImageColormap(image,1UL << image->depth) == MagickFalse) 00175 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); 00176 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) 00177 if (image->scene >= (image_info->scene+image_info->number_scenes-1)) 00178 break; 00179 for (y=0; y < image->extract_info.y; y++) 00180 (void) ReadBlob(image,packet_size*image->extract_info.width,scanline); 00181 for (y=0; y < (long) image->rows; y++) 00182 { 00183 if ((y > 0) || (image->previous == (Image *) NULL)) 00184 (void) ReadBlob(image,packet_size*image->extract_info.width,scanline); 00185 q=SetImagePixels(image,0,y,image->columns,1); 00186 if (q == (PixelPacket *) NULL) 00187 break; 00188 if ((image->depth <= QuantumDepth) && (image->depth <= 16)) 00189 (void) PushImagePixels(image,GrayQuantum,scanline+offset); 00190 else 00191 { 00192 (void) PushImagePixels(image,RedQuantum,scanline+offset); 00193 for (x=0; x < (long) image->columns; x++) 00194 { 00195 q->green=q->red; 00196 q->blue=q->red; 00197 q++; 00198 } 00199 } 00200 if (SyncImagePixels(image) == MagickFalse) 00201 break; 00202 if (image->previous == (Image *) NULL) 00203 if ((image->progress_monitor != (MagickProgressMonitor) NULL) && 00204 (QuantumTick(y,image->rows) != MagickFalse)) 00205 { 00206 status=image->progress_monitor(LoadImageTag,y,image->rows, 00207 image->client_data); 00208 if (status == MagickFalse) 00209 break; 00210 } 00211 } 00212 width=image->extract_info.height-image->rows-image->extract_info.y; 00213 for (j=0; j < (long) width; j++) 00214 (void) ReadBlob(image,packet_size*image->extract_info.width,scanline); 00215 if (EOFBlob(image) != MagickFalse) 00216 { 00217 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", 00218 image->filename); 00219 break; 00220 } 00221 /* 00222 Proceed to next image. 00223 */ 00224 if (image_info->number_scenes != 0) 00225 if (image->scene >= (image_info->scene+image_info->number_scenes-1)) 00226 break; 00227 count=ReadBlob(image,packet_size*image->extract_info.width,scanline); 00228 if (count != 0) 00229 { 00230 /* 00231 Allocate next image structure. 00232 */ 00233 AllocateNextImage(image_info,image); 00234 if (image->next == (Image *) NULL) 00235 { 00236 DestroyImageList(image); 00237 return((Image *) NULL); 00238 } 00239 image=SyncNextImageInList(image); 00240 if (image->progress_monitor != (MagickProgressMonitor) NULL) 00241 { 00242 status=image->progress_monitor(LoadImagesTag,TellBlob(image), 00243 GetBlobSize(image),image->client_data); 00244 if (status == MagickFalse) 00245 break; 00246 } 00247 } 00248 } while (count != 0); 00249 scanline=(unsigned char *) RelinquishMagickMemory(scanline); 00250 CloseBlob(image); 00251 return(GetFirstImageInList(image)); 00252 } 00253 00254 /* 00255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00256 % % 00257 % % 00258 % % 00259 % R e g i s t e r G R A Y I m a g e % 00260 % % 00261 % % 00262 % % 00263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00264 % 00265 % RegisterGRAYImage() adds attributes for the GRAY image format to 00266 % the list of supported formats. The attributes include the image format 00267 % tag, a method to read and/or write the format, whether the format 00268 % supports the saving of more than one frame to the same file or blob, 00269 % whether the format supports native in-memory I/O, and a brief 00270 % description of the format. 00271 % 00272 % The format of the RegisterGRAYImage method is: 00273 % 00274 % RegisterGRAYImage(void) 00275 % 00276 */ 00277 ModuleExport void RegisterGRAYImage(void) 00278 { 00279 MagickInfo 00280 *entry; 00281 00282 entry=SetMagickInfo("GRAY"); 00283 entry->decoder=(DecoderHandler *) ReadGRAYImage; 00284 entry->encoder=(EncoderHandler *) WriteGRAYImage; 00285 entry->raw=MagickTrue; 00286 entry->endian_support=MagickTrue; 00287 entry->description=AcquireString("Raw gray samples"); 00288 entry->module=AcquireString("GRAY"); 00289 (void) RegisterMagickInfo(entry); 00290 } 00291 00292 /* 00293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00294 % % 00295 % % 00296 % % 00297 % U n r e g i s t e r G R A Y I m a g e % 00298 % % 00299 % % 00300 % % 00301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00302 % 00303 % UnregisterGRAYImage() removes format registrations made by the 00304 % GRAY module from the list of supported formats. 00305 % 00306 % The format of the UnregisterGRAYImage method is: 00307 % 00308 % UnregisterGRAYImage(void) 00309 % 00310 */ 00311 ModuleExport void UnregisterGRAYImage(void) 00312 { 00313 (void) UnregisterMagickInfo("GRAY"); 00314 } 00315 00316 /* 00317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00318 % % 00319 % % 00320 % % 00321 % W r i t e G R A Y I m a g e % 00322 % % 00323 % % 00324 % % 00325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00326 % 00327 % WriteGRAYImage() writes an image to a file as gray scale intensity 00328 % values. 00329 % 00330 % The format of the WriteGRAYImage method is: 00331 % 00332 % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,Image *image) 00333 % 00334 % A description of each parameter follows. 00335 % 00336 % o image_info: The image info. 00337 % 00338 % o image: The image. 00339 % 00340 % 00341 */ 00342 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, 00343 Image *image) 00344 { 00345 long 00346 y; 00347 00348 MagickBooleanType 00349 status; 00350 00351 MagickOffsetType 00352 scene; 00353 00354 register const PixelPacket 00355 *p; 00356 00357 size_t 00358 packet_size; 00359 00360 unsigned char 00361 *scanline; 00362 00363 unsigned long 00364 depth; 00365 00366 /* 00367 Open output image file. 00368 */ 00369 assert(image_info != (const ImageInfo *) NULL); 00370 assert(image_info->signature == MagickSignature); 00371 assert(image != (Image *) NULL); 00372 assert(image->signature == MagickSignature); 00373 if (image->debug != MagickFalse) 00374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),image->filename); 00375 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); 00376 if (status == MagickFalse) 00377 return(status); 00378 /* 00379 Convert image to gray scale PseudoColor class. 00380 */ 00381 scene=0; 00382 do 00383 { 00384 /* 00385 Allocate memory for scanline. 00386 */ 00387 (void) SetImageColorspace(image,RGBColorspace); 00388 depth=GetImageQuantumDepth(image,MagickTrue); 00389 packet_size=(size_t) (depth/8); 00390 scanline=(unsigned char *) 00391 AcquireMagickMemory(packet_size*image->columns); 00392 if (scanline == (unsigned char *) NULL) 00393 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); 00394 /* 00395 Convert MIFF to GRAY raster scanline. 00396 */ 00397 for (y=0; y < (long) image->rows; y++) 00398 { 00399 p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); 00400 if (p == (const PixelPacket *) NULL) 00401 break; 00402 (void) PopImagePixels(image,GrayQuantum,scanline); 00403 (void) WriteBlob(image,packet_size*image->columns,scanline); 00404 if (image->previous == (Image *) NULL) 00405 if ((image->progress_monitor != (MagickProgressMonitor) NULL) && 00406 (QuantumTick(y,image->rows) != MagickFalse)) 00407 { 00408 status=image->progress_monitor(SaveImageTag,y,image->rows, 00409 image->client_data); 00410 if (status == MagickFalse) 00411 break; 00412 } 00413 } 00414 scanline=(unsigned char *) RelinquishMagickMemory(scanline); 00415 if (image->next == (Image *) NULL) 00416 break; 00417 image=SyncNextImageInList(image); 00418 if (image->progress_monitor != (MagickProgressMonitor) NULL) 00419 { 00420 status=image->progress_monitor(SaveImagesTag,scene, 00421 GetImageListLength(image),image->client_data); 00422 if (status == MagickFalse) 00423 break; 00424 } 00425 scene++; 00426 } while (image_info->adjoin != MagickFalse); 00427 CloseBlob(image); 00428 return(MagickTrue); 00429 }

Generated on Mon Oct 25 13:40:37 2004 for ImageMagick by doxygen 1.3.7
ImageMagick Copyright © 2004, ImageMagick Studio LLC