1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% CCCC IIIII N N %
% C I NN N %
% C I N N N %
% C I N NN %
% CCCC IIIII N N %
% %
% %
% Read/Write Kodak Cineon Image Format. %
% Cineon Image Format is a subset of SMTPE DPX %
% %
% %
% Software Design %
% John Cristy %
% Kelly Bergougnoux %
% October 2003 %
% %
% %
% Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% http://www.imagemagick.org/www/Copyright.html %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Cineon image file format draft is available at
% http://www.cineon.com/ff_draft.php.
%
%
*/
/*
Include declarations.
*/
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/colorspace.h"
#include "magick/error.h"
#include "magick/image.h"
#include "magick/list.h"
#include "magick/magick.h"
#include "magick/memory_.h"
#include "magick/monitor.h"
#include "magick/static.h"
#include "magick/string_.h"
/*
Forward declaractions.
*/
static MagickBooleanType
WriteCINImage(const ImageInfo *,Image *);
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% I s C I N E O N %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% IsCIN() returns MagickTrue if the image format type, identified by the magick
% string, is CIN.
%
% The format of the IsCIN method is:
%
% MagickBooleanType IsCIN(const unsigned char *magick,const size_t length)
%
% A description of each parameter follows:
%
% o magick: This string is generally the first few bytes of an image file
% or blob.
%
% o length: Specifies the length of the magick string.
%
%
*/
static MagickBooleanType IsCIN(const unsigned char *magick,const size_t length)
{
if (length < 4)
return(MagickFalse);
if (memcmp(magick,"\200\052\137\327",4) == 0)
return(MagickTrue);
return(MagickFalse);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e a d C I N E O N I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ReadCINImage() reads an CIN X image file and returns it. It allocates
% the memory necessary for the new Image structure and returns a point to the
% new image.
%
% The format of the ReadCINImage method is:
%
% Image *ReadCINImage(const ImageInfo *image_info,
% ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image_info: The image info.
%
% o exception: return any errors or warnings in this structure.
%
%
*/
static Image *ReadCINImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
#define MonoColorType 1
#define RGBColorType 3
char
magick[4];
Image
*image;
long
y;
MagickBooleanType
status;
register long
x;
register PixelPacket
*q;
register long
i;
ssize_t
count;
unsigned char
colortype;
unsigned long
headersize,
pixel;
/*
Open image file.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
if (image_info->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
image=AllocateImage(image_info);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
DestroyImageList(image);
return((Image *) NULL);
}
/*
Read CIN image.
*/
count=ReadBlob(image,4,(unsigned char *) magick);
if ((count == 0) ||
((LocaleNCompare((char *) magick,"\200\052\137\327",4) != 0)))
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
headersize=ReadBlobMSBLong(image);
for (i=0; i < 185; i++)
(void) ReadBlobByte(image);
colortype=(unsigned char) ReadBlobByte(image);
for (i=0; i < 4; i++)
(void) ReadBlobByte(image);
image->depth=ReadBlobByte(image) > 8 ? 16UL : 8UL;
(void) ReadBlobByte(image);
image->columns= ReadBlobMSBLong(image);
image->rows= ReadBlobMSBLong(image);
(void) SeekBlob(image,(MagickOffsetType) headersize,SEEK_SET);
if (image_info->ping)
{
CloseBlob(image);
return(image);
}
/*
Convert CIN raster image to pixel packets.
*/
switch ((int) colortype)
{
case (MonoColorType):
{
q=SetImagePixels(image,0,0,image->columns,image->rows);
for (x=0; x < (long) ((image->columns*image->rows)/3); x++)
{
pixel=ReadBlobMSBLong(image);
q->red=ScaleXToQuantum((pixel >> 0) & 0x3ff,1023);
q->green=q->red;
q->blue=q->red;
q++;
q->red=ScaleXToQuantum((pixel >> 10) & 0x3ff,1023);
q->green=q->red;
q->blue=q->red;
q++;
q->red=ScaleXToQuantum((pixel >> 20) & 0x3ff,1023);
q->green=q->red;
q->blue=q->red;
q++;
}
break;
}
case (RGBColorType):
{
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
for (x=0; x < (long) image->columns; x++)
{
pixel=ReadBlobMSBLong(image);
q->red=ScaleXToQuantum((pixel >> 22) & 0x3ff,1023);
q->green=ScaleXToQuantum((pixel >> 12) & 0x3ff,1023);
q->blue=ScaleXToQuantum((pixel >> 2) & 0x3ff,1023);
q++;
}
if (!SyncImagePixels(image))
break;
if (image->previous == (Image *) NULL)
if (QuantumTick(y,image->rows) != MagickFalse)
if (!MagickMonitor(LoadImageTag,y,image->rows,exception))
break;
}
break;
}
default:
ThrowReaderException(CorruptImageError,"ColorTypeNotSupported");
}
if (EOFBlob(image))
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
image->filename);
CloseBlob(image);
return(GetFirstImageInList(image));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e g i s t e r C I N E O N I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% RegisterCINImage() adds attributes for the CIN image format to the list of
% of supported formats. The attributes include the image format tag, a method
% to read and/or write the format, whether the format supports the saving of
% more than one frame to the same file or blob, whether the format supports
% native in-memory I/O, and a brief description of the format.
%
% The format of the RegisterCINImage method is:
%
% RegisterCINImage(void)
%
*/
ModuleExport void RegisterCINImage(void)
{
MagickInfo
*entry;
entry=SetMagickInfo("CIN");
entry->decoder=(DecoderHandler *) ReadCINImage;
entry->encoder=(EncoderHandler *) WriteCINImage;
entry->magick=(MagickHandler *) IsCIN;
entry->description=AcquireString("Cineon Image File");
entry->module=AcquireString("CIN");
(void) RegisterMagickInfo(entry);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% U n r e g i s t e r C I N E O N I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% UnregisterCINImage() removes format registrations made by the CIN module
% from the list of supported formats.
%
% The format of the UnregisterCINImage method is:
%
% UnregisterCINImage(void)
%
*/
ModuleExport void UnregisterCINImage(void)
{
(void) UnregisterMagickInfo("CINEON");
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% W r i t e C I N E O N I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% WriteCINImage() writes an image in CIN encoded image format.
%
% The format of the WriteCINImage method is:
%
% MagickBooleanType WriteCINImage(const ImageInfo *image_info,Image *image)
%
% A description of each parameter follows.
%
% o image_info: The image info.
%
% o image: The image.
%
%
*/
static MagickBooleanType WriteCINImage(const ImageInfo *image_info,Image *image)
{
long
y;
MagickBooleanType
status;
register const PixelPacket
*p;
register long
i,
x;
unsigned long
pixel;
unsigned long
maxb,
maxg,
maxr,
minb,
ming,
minr;
/*
Get the max values for each component... I guess it "needs" it...
*/
maxr=maxg=maxb=0;
minr=ming=minb=MaxRGB;
for (y=0; y < (long) image->rows; y++)
{
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
if (p == (const PixelPacket *) NULL)
break;
for (x=0; x < (long) image->columns; x++)
{
if ( (unsigned long) p->red > maxr ) maxr = p->red;
if ( (unsigned long) p->green > maxg ) maxg = p->green;
if ( (unsigned long) p->blue > maxb ) maxb = p->blue;
if ( (unsigned long) p->red < minr ) minr = p->red;
if ( (unsigned long) p->green < ming ) ming = p->green;
if ( (unsigned long) p->blue < minb ) minb = p->blue;
p++;
}
}
/*
Div by 64 to get 10bit values since that's all I'll do right now.
*/
maxr/=64;
maxg/=64;
maxb/=64;
minr/=64;
ming/=64;
minb/=64;
/*
Open output image file.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
assert(image != (Image *) NULL);
assert(image->signature == MagickSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),image->filename);
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
if (status == MagickFalse)
return(status);
/* Magick Number... */
(void) SetImageColorspace(image,RGBColorspace);
(void) WriteBlobMSBLong(image,0x802A5FD7UL);
/* Offset to image */
(void) WriteBlobMSBLong(image,0x0800);
/* Generic section header length */
(void) WriteBlobMSBLong(image,0x400);
/* Industry Specific lenght */
(void) WriteBlobMSBLong(image,0x400);
/* variable length section */
(void) WriteBlobMSBLong(image,0x0);
/* Total image file size */
(void) WriteBlobMSBLong(image,4*image->columns*image->rows+0x2000);
/* Version number of header format */
(void) WriteBlobString(image,"V4.5");
(void) WriteBlobMSBLong(image,0x0);
/* Filename */
(void) WriteBlobString(image,image->filename);
for ( i =0 ; i < (long) (100-strlen(image->filename)); i++ )
(void) WriteBlobByte(image,0);
/* Creation Date. */
(void) WriteBlobString(image,"yyyy:mm:dd ");
/* Creation Time. */
(void) WriteBlobString(image,"hh:mm:ssxxx ");
for ( i =0 ; i < 36 ; i++)
(void) WriteBlobByte(image,0);
/* 0 left to right top to bottom */
(void) WriteBlobByte(image,0);
/* 3 channels */
(void) WriteBlobByte(image,3);
/* alignment */
(void) WriteBlobByte(image,0x0);
(void) WriteBlobByte(image,0x0);
/* Do R */
/* Channel 1 designator byte 0 */
(void) WriteBlobByte(image,0);
/* Channel 1 designator byte 1 */
(void) WriteBlobByte(image,1);
/* Bits per pixel... */
(void) WriteBlobByte(image,10);
/* alignment */
(void) WriteBlobByte(image,0);
/* pixels per line */
(void) WriteBlobMSBLong(image,image->columns);
/* lines per image */
(void) WriteBlobMSBLong(image,image->rows);
/* Minimum data value */
(void) WriteBlobMSBLong(image,minr);
/* Minimum quantity represented */
(void) WriteBlobMSBLong(image,0x0);
/* Maximum data value */
(void) WriteBlobMSBLong(image,maxr);
/* Maximum quantity represented */
(void) WriteBlobMSBLong(image,0x40000000);
/* Do G */
/* see above. */
(void) WriteBlobByte(image,0);
(void) WriteBlobByte(image,2);
(void) WriteBlobByte(image,10);
(void) WriteBlobByte(image,0);
(void) WriteBlobMSBLong(image,image->columns);
(void) WriteBlobMSBLong(image,image->rows);
(void) WriteBlobMSBLong(image,ming);
(void) WriteBlobMSBLong(image,0x0);
(void) WriteBlobMSBLong(image,maxg);
(void) WriteBlobMSBLong(image,0x40000000);
/* Go B */
/* see above. */
(void) WriteBlobByte(image,0);
(void) WriteBlobByte(image,3);
(void) WriteBlobByte(image,10);
(void) WriteBlobByte(image,0);
(void) WriteBlobMSBLong(image,image->columns);
(void) WriteBlobMSBLong(image,image->rows);
(void) WriteBlobMSBLong(image,minb);
(void) WriteBlobMSBLong(image,0x0);
(void) WriteBlobMSBLong(image,maxb);
(void) WriteBlobMSBLong(image,0x40000000);
/* pad channel 4-8 */
for (i=0; i < 5*28; i++)
(void) WriteBlobByte(image,0x00);
/* k here's where it gets ugly and I should be shot */
/* White point(colour temp.) x */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* White point(colour temp.) y */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Red primary chromaticity x */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Red primary chromaticity y */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Green primary chromaticity x */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Green primary chromaticity y */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Blue primary chromaticity x */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Blue primary chromaticity y */
(void) WriteBlobMSBLong(image,0x4EFF0000);
/* Label Text... */
for (i=0; i < 200+28; i++)
(void) WriteBlobByte(image,0x00);
/* pixel interleave (rgbrgbr...) */
(void) WriteBlobByte(image,0);
/* Packing longword (32bit) boundaries */
(void) WriteBlobByte(image,5);
/* Data unsigned */
(void) WriteBlobByte(image,0);
/* Image sense: positive image */
(void) WriteBlobByte(image,0);
/* End of line padding */
(void) WriteBlobMSBLong(image,0x0);
/* End of channel padding */
(void) WriteBlobMSBLong(image,0x0);
/* Reseved for future Use.. */
for (i=0; i < 20; i++)
(void) WriteBlobByte(image,0x00);
/* X offset */
(void) WriteBlobMSBLong(image,0x0);
/* Y offset */
(void) WriteBlobMSBLong(image,0x0);
/* Filename */
(void) WriteBlobString(image,image->filename);
for ( i =0 ; i < (long) (100-strlen(image->filename)); i++ )
(void) WriteBlobByte(image,0);
/* date. who cares ? */
for ( i =0 ; i < 12 ; i++ )
(void) WriteBlobByte(image,0);
/* time who cares ? */
for ( i =0 ; i < 12 ; i++ )
(void) WriteBlobByte(image,0);
(void) WriteBlobString(image,"ImageMagick");
for (i = 0; i < 64-11 ; i++)
(void) WriteBlobByte(image,0);
for (i = 0; i < 32 ; i++)
(void) WriteBlobByte(image,0);
for (i = 0; i < 32 ; i++)
(void) WriteBlobByte(image,0);
/* X input device pitch */
(void) WriteBlobMSBLong(image,0x4326AB85);
/* Y input device pitch */
(void) WriteBlobMSBLong(image,0x4326AB85);
/* Image gamma */
(void) WriteBlobMSBLong(image,0x3F800000);
/* Reserved for future use */
for (i = 0; i < 40 ; i++)
(void) WriteBlobByte(image,0);
for ( i = 0 ; i < 4 ; i++)
(void) WriteBlobByte(image,0);
(void) WriteBlobMSBLong(image,0x0);
(void) WriteBlobMSBLong(image,0x0);
for ( i = 0 ; i < 32 ; i++)
(void) WriteBlobByte(image,0);
(void) WriteBlobMSBLong(image,0x0);
(void) WriteBlobMSBLong(image,0x0);
for ( i = 0 ; i < 32 ; i++)
(void) WriteBlobByte(image,0);
for ( i = 0 ; i < 200 ; i++)
(void) WriteBlobByte(image,0);
for ( i = 0 ; i < 740 ; i++)
(void) WriteBlobByte(image,0);
/*
Convert pixel packets to CIN raster image.
*/
for (y=0; y < (long) image->rows; y++)
{
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
if (p == (const PixelPacket *) NULL)
break;
for (x=0; x < (long) image->columns; x++)
{
pixel=(unsigned long)
(((long) ((1023L*p->red+MaxRGB/2)/MaxRGB) & 0x3ff) << 22) |
(((long) ((1023L*p->green+MaxRGB/2)/MaxRGB) &0x3ff) << 12) |
(((long) ((1023L*p->blue+MaxRGB/2)/MaxRGB) &0x3ff) << 2);
(void) WriteBlobMSBLong(image,pixel);
p++;
}
}
CloseBlob(image);
return(status);
}
|