cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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-2009 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 | |
| 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 | |
| 65 | /* |
| 66 | Forward declarations. |
| 67 | */ |
| 68 | static MagickBooleanType |
| 69 | WriteGRAYImage(const ImageInfo *,Image *); |
| 70 | |
| 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 | long |
| 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 long |
| 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 < (long) 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 | if (count != (ssize_t) length) |
| 200 | break; |
| 201 | } |
| 202 | for (y=0; y < (long) image->extract_info.height; y++) |
| 203 | { |
| 204 | register const PixelPacket |
| 205 | *__restrict p; |
| 206 | |
| 207 | register long |
| 208 | x; |
| 209 | |
| 210 | register PixelPacket |
| 211 | *__restrict q; |
| 212 | |
| 213 | q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception); |
| 214 | if (q == (PixelPacket *) NULL) |
| 215 | break; |
| 216 | length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info, |
| 217 | quantum_type,pixels,exception); |
| 218 | if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse) |
| 219 | break; |
| 220 | count=ReadBlob(image,length,pixels); |
| 221 | if (((y-image->extract_info.y) >= 0) && |
| 222 | ((y-image->extract_info.y) < (long) image->rows)) |
| 223 | { |
| 224 | p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0, |
| 225 | image->columns,1,exception); |
| 226 | q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns, |
| 227 | 1,exception); |
| 228 | if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) |
| 229 | break; |
| 230 | for (x=0; x < (long) image->columns; x++) |
| 231 | { |
| 232 | q->red=p->red; |
| 233 | q->green=p->green; |
| 234 | q->blue=p->blue; |
| 235 | p++; |
| 236 | q++; |
| 237 | } |
| 238 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 239 | break; |
| 240 | } |
| 241 | if (image->previous == (Image *) NULL) |
| 242 | { |
| 243 | status=SetImageProgress(image,LoadImageTag,y,image->rows); |
| 244 | if (status == MagickFalse) |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | SetQuantumImageType(image,quantum_type); |
cristy | 94f2007 | 2009-09-12 02:12:36 +0000 | [diff] [blame^] | 249 | if (EOFBlob(image) != MagickFalse) |
| 250 | { |
| 251 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 252 | image->filename); |
| 253 | break; |
| 254 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 255 | /* |
| 256 | Proceed to next image. |
| 257 | */ |
| 258 | if (image_info->number_scenes != 0) |
| 259 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 260 | break; |
| 261 | if (count == (ssize_t) length) |
| 262 | { |
| 263 | /* |
| 264 | Allocate next image structure. |
| 265 | */ |
| 266 | AcquireNextImage(image_info,image); |
| 267 | if (GetNextImageInList(image) == (Image *) NULL) |
| 268 | { |
| 269 | image=DestroyImageList(image); |
| 270 | return((Image *) NULL); |
| 271 | } |
| 272 | image=SyncNextImageInList(image); |
| 273 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 274 | GetBlobSize(image)); |
| 275 | if (status == MagickFalse) |
| 276 | break; |
| 277 | } |
| 278 | scene++; |
| 279 | } while (count == (ssize_t) length); |
| 280 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 281 | InheritException(exception,&canvas_image->exception); |
| 282 | canvas_image=DestroyImage(canvas_image); |
| 283 | (void) CloseBlob(image); |
| 284 | return(GetFirstImageInList(image)); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 289 | % % |
| 290 | % % |
| 291 | % % |
| 292 | % R e g i s t e r G R A Y I m a g e % |
| 293 | % % |
| 294 | % % |
| 295 | % % |
| 296 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 297 | % |
| 298 | % RegisterGRAYImage() adds attributes for the GRAY image format to |
| 299 | % the list of supported formats. The attributes include the image format |
| 300 | % tag, a method to read and/or write the format, whether the format |
| 301 | % supports the saving of more than one frame to the same file or blob, |
| 302 | % whether the format supports native in-memory I/O, and a brief |
| 303 | % description of the format. |
| 304 | % |
| 305 | % The format of the RegisterGRAYImage method is: |
| 306 | % |
| 307 | % unsigned long RegisterGRAYImage(void) |
| 308 | % |
| 309 | */ |
| 310 | ModuleExport unsigned long RegisterGRAYImage(void) |
| 311 | { |
| 312 | MagickInfo |
| 313 | *entry; |
| 314 | |
| 315 | entry=SetMagickInfo("GRAY"); |
| 316 | entry->decoder=(DecodeImageHandler *) ReadGRAYImage; |
| 317 | entry->encoder=(EncodeImageHandler *) WriteGRAYImage; |
| 318 | entry->raw=MagickTrue; |
| 319 | entry->endian_support=MagickTrue; |
| 320 | entry->format_type=ExplicitFormatType; |
| 321 | entry->description=ConstantString("Raw gray samples"); |
| 322 | entry->module=ConstantString("GRAY"); |
| 323 | (void) RegisterMagickInfo(entry); |
| 324 | return(MagickImageCoderSignature); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 329 | % % |
| 330 | % % |
| 331 | % % |
| 332 | % U n r e g i s t e r G R A Y I m a g e % |
| 333 | % % |
| 334 | % % |
| 335 | % % |
| 336 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 337 | % |
| 338 | % UnregisterGRAYImage() removes format registrations made by the |
| 339 | % GRAY module from the list of supported formats. |
| 340 | % |
| 341 | % The format of the UnregisterGRAYImage method is: |
| 342 | % |
| 343 | % UnregisterGRAYImage(void) |
| 344 | % |
| 345 | */ |
| 346 | ModuleExport void UnregisterGRAYImage(void) |
| 347 | { |
| 348 | (void) UnregisterMagickInfo("GRAY"); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 353 | % % |
| 354 | % % |
| 355 | % % |
| 356 | % W r i t e G R A Y I m a g e % |
| 357 | % % |
| 358 | % % |
| 359 | % % |
| 360 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 361 | % |
| 362 | % WriteGRAYImage() writes an image to a file as gray scale intensity |
| 363 | % values. |
| 364 | % |
| 365 | % The format of the WriteGRAYImage method is: |
| 366 | % |
| 367 | % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, |
| 368 | % Image *image) |
| 369 | % |
| 370 | % A description of each parameter follows. |
| 371 | % |
| 372 | % o image_info: the image info. |
| 373 | % |
| 374 | % o image: The image. |
| 375 | % |
| 376 | */ |
| 377 | static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, |
| 378 | Image *image) |
| 379 | { |
| 380 | long |
| 381 | y; |
| 382 | |
| 383 | MagickBooleanType |
| 384 | status; |
| 385 | |
| 386 | MagickOffsetType |
| 387 | scene; |
| 388 | |
| 389 | QuantumInfo |
| 390 | *quantum_info; |
| 391 | |
| 392 | QuantumType |
| 393 | quantum_type; |
| 394 | |
| 395 | ssize_t |
| 396 | count; |
| 397 | |
| 398 | size_t |
| 399 | length; |
| 400 | |
| 401 | unsigned char |
| 402 | *pixels; |
| 403 | |
| 404 | /* |
| 405 | Open output image file. |
| 406 | */ |
| 407 | assert(image_info != (const ImageInfo *) NULL); |
| 408 | assert(image_info->signature == MagickSignature); |
| 409 | assert(image != (Image *) NULL); |
| 410 | assert(image->signature == MagickSignature); |
| 411 | if (image->debug != MagickFalse) |
| 412 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 413 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
| 414 | if (status == MagickFalse) |
| 415 | return(status); |
| 416 | scene=0; |
| 417 | do |
| 418 | { |
| 419 | /* |
| 420 | Write grayscale pixels. |
| 421 | */ |
| 422 | if (image->colorspace != RGBColorspace) |
| 423 | (void) TransformImageColorspace(image,RGBColorspace); |
| 424 | quantum_type=GrayQuantum; |
| 425 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 426 | if (quantum_info == (QuantumInfo *) NULL) |
| 427 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 428 | pixels=GetQuantumPixels(quantum_info); |
| 429 | for (y=0; y < (long) image->rows; y++) |
| 430 | { |
| 431 | register const PixelPacket |
| 432 | *__restrict p; |
| 433 | |
| 434 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
| 435 | if (p == (const PixelPacket *) NULL) |
| 436 | break; |
| 437 | length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info, |
| 438 | quantum_type,pixels,&image->exception); |
| 439 | count=WriteBlob(image,length,pixels); |
| 440 | if (count != (ssize_t) length) |
| 441 | break; |
| 442 | if (image->previous == (Image *) NULL) |
| 443 | { |
| 444 | status=SetImageProgress(image,SaveImageTag,y,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 | } |