2
% Copyright (C) 2003 GraphicsMagick Group
3
% Copyright (C) 2002 ImageMagick Studio
4
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
6
% This program is covered by multiple licenses, which are described in
7
% Copyright.txt. You should have received a copy of Copyright.txt with this
8
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
% Read/Write RAW CMYK Image Format. %
30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
#include "magick/studio.h"
39
#include "magick/cache.h"
40
#include "magick/constitute.h"
41
#include "magick/blob.h"
42
#include "magick/magick.h"
43
#include "magick/monitor.h"
44
#include "magick/utility.h"
50
WriteCMYKImage(const ImageInfo *,Image *);
53
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
% R e a d C M Y K I m a g e %
61
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
% Method ReadCMYKImage reads an image of raw cyan, magenta, yellow, and black
64
% samples and returns it. It allocates the memory necessary for the new
65
% Image structure and returns a pointer to the new image.
67
% The format of the ReadCMYKImage method is:
69
% Image *ReadCMYKImage(const ImageInfo *image_info,
70
% ExceptionInfo *exception)
72
% A description of each parameter follows:
74
% o image: Method ReadCMYKImage returns a pointer to the image after
75
% reading. A null image is returned if there is a memory shortage or
76
% if the image cannot be read.
78
% o image_info: Specifies a pointer to a ImageInfo structure.
80
% o exception: return any errors or warnings in this structure.
84
static Image *ReadCMYKImage(const ImageInfo *image_info,
85
ExceptionInfo *exception)
112
assert(image_info != (const ImageInfo *) NULL);
113
assert(image_info->signature == MagickSignature);
114
assert(exception != (ExceptionInfo *) NULL);
115
assert(exception->signature == MagickSignature);
116
image=AllocateImage(image_info);
117
if ((image->columns == 0) || (image->rows == 0))
118
ThrowReaderException(OptionError,MustSpecifyImageSize,image);
119
if (image_info->interlace != PartitionInterlace)
124
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
126
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
127
for (i=0; i < image->offset; i++)
128
(void) ReadBlobByte(image);
131
Allocate memory for a scanline.
133
packet_size=image->depth > 8 ? 8 : 4;
134
if (LocaleCompare(image_info->magick,"CMYKA") == 0)
137
packet_size=image->depth > 8 ? 10 : 8;
139
scanline=MagickAllocateMemory(unsigned char *,
140
packet_size*image->tile_info.width);
141
if (scanline == (unsigned char *) NULL)
142
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
143
if (image_info->subrange != 0)
144
while (image->scene < image_info->subimage)
150
for (y=0; y < (long) image->rows; y++)
151
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
153
x=(long) (packet_size*image->tile_info.x);
157
Convert raster image to pixel packets.
159
image->colorspace=CMYKColorspace;
160
if (image_info->ping && (image_info->subrange != 0))
161
if (image->scene >= (image_info->subimage+image_info->subrange-1))
163
switch (image_info->interlace)
169
No interlacing: CMYKCMYKCMYKCMYKCMYKCMYK...
171
for (y=0; y < image->tile_info.y; y++)
172
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
173
for (y=0; y < (long) image->rows; y++)
175
if ((y > 0) || (image->previous == (Image *) NULL))
176
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
177
q=SetImagePixels(image,0,y,image->columns,1);
178
if (q == (PixelPacket *) NULL)
181
(void) PushImagePixels(image,CMYKQuantum,scanline+x);
183
(void) PushImagePixels(image,CMYKAQuantum,scanline+x);
184
if (!SyncImagePixels(image))
186
if (image->previous == (Image *) NULL)
187
if (QuantumTick(y,image->rows))
188
if (!MagickMonitor(LoadImageText,y,image->rows,exception))
191
count=image->tile_info.height-image->rows-image->tile_info.y;
192
for (i=0; i < (long) count; i++)
193
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
199
Line interlacing: CCC...MMM...YYY...KKK...CCC...MMM...YYY...KKK...
201
packet_size=image->depth > 8 ? 2 : 1;
202
for (y=0; y < image->tile_info.y; y++)
203
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
204
for (y=0; y < (long) image->rows; y++)
206
if ((y > 0) || (image->previous == (Image *) NULL))
207
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
208
q=SetImagePixels(image,0,y,image->columns,1);
209
if (q == (PixelPacket *) NULL)
211
(void) PushImagePixels(image,CyanQuantum,scanline+x);
212
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
213
(void) PushImagePixels(image,MagentaQuantum,scanline+x);
214
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
215
(void) PushImagePixels(image,YellowQuantum,scanline+x);
216
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
217
(void) PushImagePixels(image,BlackQuantum,scanline+x);
220
(void) ReadBlob(image,packet_size*image->tile_info.width,
222
(void) PushImagePixels(image,AlphaQuantum,scanline+x);
224
if (!SyncImagePixels(image))
226
if (image->previous == (Image *) NULL)
227
if (QuantumTick(y,image->rows))
228
if (!MagickMonitor(LoadImageText,y,image->rows,exception))
231
count=image->tile_info.height-image->rows-image->tile_info.y;
232
for (i=0; i < (long) count; i++)
233
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
237
case PartitionInterlace:
243
Plane interlacing: CCCCCC...MMMMMM...YYYYYY...KKKKKK...
245
if (image_info->interlace == PartitionInterlace)
247
AppendImageFormat("C",image->filename);
248
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
250
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
252
packet_size=image->depth > 8 ? 2 : 1;
253
for (y=0; y < image->tile_info.y; y++)
254
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
256
span=image->rows*(image->matte ? 5 : 4);
257
for (y=0; y < (long) image->rows; y++)
259
if ((y > 0) || (image->previous == (Image *) NULL))
260
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
261
q=SetImagePixels(image,0,y,image->columns,1);
262
if (q == (PixelPacket *) NULL)
264
(void) PushImagePixels(image,CyanQuantum,scanline+x);
265
if (!SyncImagePixels(image))
267
if (image->previous == (Image *) NULL)
268
if (QuantumTick(i,span))
269
if (!MagickMonitor(LoadImageText,i,span,&image->exception))
273
count=image->tile_info.height-image->rows-image->tile_info.y;
274
for (i=0; i < (long) count; i++)
275
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
276
if (image_info->interlace == PartitionInterlace)
279
AppendImageFormat("M",image->filename);
280
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
282
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
284
for (y=0; y < image->tile_info.y; y++)
285
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
286
for (y=0; y < (long) image->rows; y++)
288
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
289
q=GetImagePixels(image,0,y,image->columns,1);
290
if (q == (PixelPacket *) NULL)
292
(void) PushImagePixels(image,MagentaQuantum,scanline+x);
293
if (!SyncImagePixels(image))
295
if (image->previous == (Image *) NULL)
296
if (QuantumTick(i,span))
297
if (!MagickMonitor(LoadImageText,i,span,&image->exception))
301
count=image->tile_info.height-image->rows-image->tile_info.y;
302
for (i=0; i < (long) count; i++)
303
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
304
if (image_info->interlace == PartitionInterlace)
307
AppendImageFormat("Y",image->filename);
308
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
310
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
312
for (y=0; y < image->tile_info.y; y++)
313
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
314
for (y=0; y < (long) image->rows; y++)
316
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
317
q=GetImagePixels(image,0,y,image->columns,1);
318
if (q == (PixelPacket *) NULL)
320
(void) PushImagePixels(image,YellowQuantum,scanline+x);
321
if (!SyncImagePixels(image))
323
if (image->previous == (Image *) NULL)
324
if (QuantumTick(i,span))
325
if (!MagickMonitor(LoadImageText,i,span,&image->exception))
329
count=image->tile_info.height-image->rows-image->tile_info.y;
330
for (i=0; i < (long) count; i++)
331
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
332
if (image_info->interlace == PartitionInterlace)
335
AppendImageFormat("K",image->filename);
336
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
338
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
340
for (y=0; y < image->tile_info.y; y++)
341
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
342
for (y=0; y < (long) image->rows; y++)
344
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
345
q=GetImagePixels(image,0,y,image->columns,1);
346
if (q == (PixelPacket *) NULL)
348
(void) PushImagePixels(image,BlackQuantum,scanline+x);
349
if (!SyncImagePixels(image))
351
if (image->previous == (Image *) NULL)
352
if (QuantumTick(i,span))
353
if (!MagickMonitor(LoadImageText,i,span,&image->exception))
357
count=image->tile_info.height-image->rows-image->tile_info.y;
358
for (i=0; i < (long) count; i++)
359
(void) ReadBlob(image,packet_size*image->tile_info.width,scanline);
365
if (image_info->interlace == PartitionInterlace)
368
AppendImageFormat("A",image->filename);
369
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
371
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
373
for (y=0; y < image->tile_info.y; y++)
374
(void) ReadBlob(image,packet_size*image->tile_info.width,
376
for (y=0; y < (long) image->rows; y++)
378
(void) ReadBlob(image,packet_size*image->tile_info.width,
380
q=GetImagePixels(image,0,y,image->columns,1);
381
if (q == (PixelPacket *) NULL)
383
(void) PushImagePixels(image,AlphaQuantum,scanline+x);
384
if (!SyncImagePixels(image))
386
if (image->previous == (Image *) NULL)
387
if (QuantumTick(i,span))
388
if (!MagickMonitor(LoadImageText,i,span,&image->exception))
392
count=image->tile_info.height-image->rows-image->tile_info.y;
393
for (i=0; i < (long) count; i++)
394
(void) ReadBlob(image,packet_size*image->tile_info.width,
397
if (image_info->interlace == PartitionInterlace)
398
(void) strncpy(image->filename,image_info->filename,MaxTextExtent-1);
404
ThrowException(exception,CorruptImageError,UnexpectedEndOfFile,
409
Proceed to next image.
411
if (image_info->subrange != 0)
412
if (image->scene >= (image_info->subimage+image_info->subrange-1))
414
if (image_info->interlace == PartitionInterlace)
416
count=ReadBlob(image,packet_size*image->tile_info.width,scanline);
420
Allocate next image structure.
422
AllocateNextImage(image_info,image);
423
if (image->next == (Image *) NULL)
425
DestroyImageList(image);
426
return((Image *) NULL);
428
image=SyncNextImageInList(image);
429
status=MagickMonitor(LoadImagesText,TellBlob(image),GetBlobSize(image),
434
} while (count != 0);
435
MagickFreeMemory(scanline);
436
while (image->previous != (Image *) NULL)
437
image=image->previous;
443
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447
% R e g i s t e r C M Y K I m a g e %
451
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453
% Method RegisterCMYKImage adds attributes for the CMYK image format to
454
% the list of supported formats. The attributes include the image format
455
% tag, a method to read and/or write the format, whether the format
456
% supports the saving of more than one frame to the same file or blob,
457
% whether the format supports native in-memory I/O, and a brief
458
% description of the format.
460
% The format of the RegisterCMYKImage method is:
462
% RegisterCMYKImage(void)
465
ModuleExport void RegisterCMYKImage(void)
470
entry=SetMagickInfo("CMYK");
471
entry->decoder=(DecoderHandler) ReadCMYKImage;
472
entry->encoder=(EncoderHandler) WriteCMYKImage;
475
AcquireString("Raw cyan, magenta, yellow, and black samples");
476
entry->module=AcquireString("CMYK");
477
(void) RegisterMagickInfo(entry);
478
entry=SetMagickInfo("CMYKA");
479
entry->decoder=(DecoderHandler) ReadCMYKImage;
480
entry->encoder=(EncoderHandler) WriteCMYKImage;
483
AcquireString("Raw cyan, magenta, yellow, black, and opacity samples");
484
entry->module=AcquireString("CMYK");
485
(void) RegisterMagickInfo(entry);
489
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493
% U n r e g i s t e r C M Y K I m a g e %
497
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499
% Method UnregisterCMYKImage removes format registrations made by the
500
% CMYK module from the list of supported formats.
502
% The format of the UnregisterCMYKImage method is:
504
% UnregisterCMYKImage(void)
507
ModuleExport void UnregisterCMYKImage(void)
509
(void) UnregisterMagickInfo("CMYK");
510
(void) UnregisterMagickInfo("CMYKA");
514
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518
% W r i t e C M Y K I m a g e %
522
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524
% Method WriteCMYKImage writes an image to a file in red, green, and blue
527
% The format of the WriteCMYKImage method is:
529
% unsigned int WriteCMYKImage(const ImageInfo *image_info,Image *image)
531
% A description of each parameter follows.
533
% o status: Method WriteCMYKImage return True if the image is written.
534
% False is returned is there is a memory shortage or if the image file
537
% o image_info: Specifies a pointer to a ImageInfo structure.
539
% o image: A pointer to an Image structure.
543
static unsigned int WriteCMYKImage(const ImageInfo *image_info,Image *image)
548
register const PixelPacket
560
Allocate memory for pixels.
562
assert(image_info != (const ImageInfo *) NULL);
563
assert(image_info->signature == MagickSignature);
564
assert(image != (Image *) NULL);
565
assert(image->signature == MagickSignature);
566
packet_size=image->depth > 8 ? 8 : 4;
567
if (LocaleCompare(image_info->magick,"CMYKA") == 0)
568
packet_size=image->depth > 8 ? 10 : 8;
569
pixels=MagickAllocateMemory(unsigned char *,packet_size*image->columns);
570
if (pixels == (unsigned char *) NULL)
571
ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image);
572
if (image_info->interlace != PartitionInterlace)
575
Open output image file.
577
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
579
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
585
Convert MIFF to CMYK raster pixels.
587
TransformColorspace(image,CMYKColorspace);
588
if (LocaleCompare(image_info->magick,"CMYKA") == 0)
590
SetImageOpacity(image,OpaqueOpacity);
591
switch (image_info->interlace)
597
No interlacing: CMYKCMYKCMYKCMYKCMYKCMYK...
599
for (y=0; y < (long) image->rows; y++)
601
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
602
if (p == (const PixelPacket *) NULL)
604
if (LocaleCompare(image_info->magick,"CMYKA") != 0)
606
(void) PopImagePixels(image,CMYKQuantum,pixels);
607
(void) WriteBlob(image,packet_size*image->columns,pixels);
611
(void) PopImagePixels(image,CMYKAQuantum,pixels);
612
(void) WriteBlob(image,packet_size*image->columns,pixels);
614
if (image->previous == (Image *) NULL)
615
if (QuantumTick(y,image->rows))
616
if (!MagickMonitor(SaveImageText,y,image->rows,&image->exception))
624
Line interlacing: CCC...MMM...YYY...KKK...CCC...MMM...YYY...KKK...
626
for (y=0; y < (long) image->rows; y++)
628
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
629
if (p == (const PixelPacket *) NULL)
631
(void) PopImagePixels(image,CyanQuantum,pixels);
632
(void) WriteBlob(image,image->columns,pixels);
633
(void) PopImagePixels(image,MagentaQuantum,pixels);
634
(void) WriteBlob(image,image->columns,pixels);
635
(void) PopImagePixels(image,YellowQuantum,pixels);
636
(void) WriteBlob(image,image->columns,pixels);
637
(void) PopImagePixels(image,BlackQuantum,pixels);
638
(void) WriteBlob(image,image->columns,pixels);
639
if (LocaleCompare(image_info->magick,"CMYKA") == 0)
641
(void) PopImagePixels(image,AlphaQuantum,pixels);
642
(void) WriteBlob(image,image->columns,pixels);
644
if (QuantumTick(y,image->rows))
645
if (!MagickMonitor(SaveImageText,y,image->rows,&image->exception))
651
case PartitionInterlace:
654
Plane interlacing: CCCCCC...MMMMMM...YYYYYY...KKKKKK...
656
if (image_info->interlace == PartitionInterlace)
658
AppendImageFormat("C",image->filename);
660
OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
662
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
664
for (y=0; y < (long) image->rows; y++)
666
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
667
if (p == (const PixelPacket *) NULL)
669
(void) PopImagePixels(image,CyanQuantum,pixels);
670
(void) WriteBlob(image,image->columns,pixels);
672
if (image_info->interlace == PartitionInterlace)
675
AppendImageFormat("M",image->filename);
676
status=OpenBlob(image_info,image,WriteBinaryBlobMode,
679
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
681
if (!MagickMonitor(SaveImageText,100,400,&image->exception))
683
for (y=0; y < (long) image->rows; y++)
685
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
686
if (p == (const PixelPacket *) NULL)
688
(void) PopImagePixels(image,MagentaQuantum,pixels);
689
(void) WriteBlob(image,image->columns,pixels);
691
if (image_info->interlace == PartitionInterlace)
694
AppendImageFormat("Y",image->filename);
695
status=OpenBlob(image_info,image,WriteBinaryBlobMode,
698
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
700
if (!MagickMonitor(SaveImageText,200,400,&image->exception))
702
for (y=0; y < (long) image->rows; y++)
704
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
705
if (p == (const PixelPacket *) NULL)
707
(void) PopImagePixels(image,YellowQuantum,pixels);
708
(void) WriteBlob(image,image->columns,pixels);
710
if (image_info->interlace == PartitionInterlace)
713
AppendImageFormat("K",image->filename);
714
status=OpenBlob(image_info,image,WriteBinaryBlobMode,
717
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
719
if (!MagickMonitor(SaveImageText,200,400,&image->exception))
721
for (y=0; y < (long) image->rows; y++)
723
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
724
if (p == (const PixelPacket *) NULL)
726
(void) PopImagePixels(image,BlackQuantum,pixels);
727
(void) WriteBlob(image,image->columns,pixels);
729
if (LocaleCompare(image_info->magick,"CMYKA") == 0)
731
if (!MagickMonitor(SaveImageText,300,400,&image->exception))
733
if (image_info->interlace == PartitionInterlace)
736
AppendImageFormat("A",image->filename);
737
status=OpenBlob(image_info,image,WriteBinaryBlobMode,
740
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
742
for (y=0; y < (long) image->rows; y++)
744
p=AcquireImagePixels(image,0,y,image->columns,1,
746
if (p == (const PixelPacket *) NULL)
748
(void) PopImagePixels(image,AlphaQuantum,pixels);
749
(void) WriteBlob(image,image->columns,pixels);
752
if (image_info->interlace == PartitionInterlace)
753
(void) strncpy(image->filename,image_info->filename,MaxTextExtent-1);
754
if (!MagickMonitor(SaveImageText,400,400,&image->exception))
759
if (image->next == (Image *) NULL)
761
image=SyncNextImageInList(image);
762
status=MagickMonitor(SaveImagesText,scene++,GetImageListLength(image),
766
} while (image_info->adjoin);
767
MagickFreeMemory(pixels);
768
if (image_info->adjoin)
769
while (image->previous != (Image *) NULL)
770
image=image->previous;