/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e V I C A R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method WriteVICARImage writes an image in the VICAR rasterfile format. % Vicar files contain a text header, followed by one or more planes of binary % grayscale image data. Vicar files are designed to allow many planes to be % stacked together to form image cubes. This method only writes a single % grayscale plane. % % Method WriteVICARImage was written contributed by % [email protected]. % % The format of the WriteVICARImage method is: % % unsigned int WriteVICARImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o status: Method WriteVICARImage return True if the image is written. % False is returned is there is a memory shortage or if the image file % fails to write. % % o image_info: Specifies a pointer to a ImageInfo structure. % % o image: A pointer to an Image structure. % % */ static unsigned int WriteVICARImage(const ImageInfo *image_info,Image *image) { char header[MaxTextExtent]; int y; unsigned char *scanline; unsigned int status; /* Open output image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,image); (void) TransformColorspace(image,RGBColorspace); /* Write header. */ (void) memset(header,' ',MaxTextExtent); FormatString(header,"LBLSIZE=%u FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 " "DIM=2 EOL=0 RECSIZE=%lu ORG='BSQ' NL=%lu NS=%lu NB=1 N1=0 N2=0 N3=0 N4=0 " "NBB=0 NLB=0 TASK='GraphicsMagick'",MaxTextExtent,image->columns,image->rows, image->columns); (void) WriteBlob(image,MaxTextExtent,header); /* Allocate memory for scanline. */ scanline=MagickAllocateMemory(unsigned char *,image->columns); if (scanline == (unsigned char *) NULL) ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image); /* Write VICAR scanline. */ for (y=0; y < (long) image->rows; y++) { if (!AcquireImagePixels(image,0,y,image->columns,1,&image->exception)) break; (void) ExportImagePixelArea(image,GrayQuantum,8,scanline,0,0); (void) WriteBlob(image,image->columns,scanline); if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; } MagickFreeMemory(scanline); CloseBlob(image); return(True); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e M A T T E I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function WriteMATTEImage writes an image of matte bytes to a file. It % consists of data from the matte component of the image [0..255]. % % The format of the WriteMATTEImage method is: % % unsigned int WriteMATTEImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o status: Function WriteMATTEImage return True if the image is written. % False is returned is there is a memory shortage or if the image file % fails to write. % % o image_info: Specifies a pointer to a ImageInfo structure. % % o image: A pointer to an Image structure. % % */ static unsigned int WriteMATTEImage(const ImageInfo *image_info,Image *image) { Image *matte_image; long y; register const PixelPacket *p; register long x; register PixelPacket *q; unsigned int status; if (!image->matte) ThrowWriterException(CoderError,ImageDoesNotHaveAMatteChannel,image); matte_image= CloneImage(image,image->columns,image->rows,True,&image->exception); if (matte_image == (Image *) NULL) return(False); (void) (void) SetImageType(matte_image,TrueColorType); /* Convert image to matte pixels. */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); q=SetImagePixels(matte_image,0,y,matte_image->columns,1); if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) break; for (x=0; x < (long) image->columns; x++) { q->red=p->opacity; q->green=p->opacity; q->blue=p->opacity; q->opacity=OpaqueOpacity; p++; q++; } if (!SyncImagePixels(matte_image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; } (void) FormatString(matte_image->filename,"MIFF:%.1024s",image->filename); status=WriteImage(image_info,matte_image); DestroyImage(matte_image); return(status); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e C M Y K I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method WriteCMYKImage writes an image to a file in red, green, and blue % rasterfile format. % % The format of the WriteCMYKImage method is: % % unsigned int WriteCMYKImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o status: Method WriteCMYKImage return True if the image is written. % False is returned is there is a memory shortage or if the image file % fails to write. % % o image_info: Specifies a pointer to a ImageInfo structure. % % o image: A pointer to an Image structure. % % */ static unsigned int WriteCMYKImage(const ImageInfo *image_info,Image *image) { int y; register const PixelPacket *p; unsigned char *pixels; unsigned int packet_size, quantum_size, scene, status; ExportPixelAreaOptions export_options; ExportPixelAreaInfo export_info; if (image->depth <= 8) quantum_size=8; else if (image->depth <= 16) quantum_size=16; else quantum_size=32; /* Allocate memory for pixels. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); packet_size=(quantum_size*4)/8; if (LocaleCompare(image_info->magick,"CMYKA") == 0) packet_size=(quantum_size*5)/8; pixels=MagickAllocateArray(unsigned char *,packet_size,image->columns); if (pixels == (unsigned char *) NULL) ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image); if (image_info->interlace != PartitionInterlace) { /* Open output image file. */ status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,image); } scene=0; do { /* Convert MIFF to CMYK raster pixels. */ (void) TransformColorspace(image,CMYKColorspace); if (LocaleCompare(image_info->magick,"CMYKA") == 0) if (!image->matte) SetImageOpacity(image,OpaqueOpacity); /* Initialize export options. */ ExportPixelAreaOptionsInit(&export_options); if (image->endian != UndefinedEndian) export_options.endian=image->endian; else if (image_info->endian != UndefinedEndian) export_options.endian=image_info->endian; if (image->logging) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Image depth %u bits, Endian %s",quantum_size, EndianTypeToString(export_options.endian)); switch (image_info->interlace) { case NoInterlace: default: { /* No interlacing: CMYKCMYKCMYKCMYKCMYKCMYK... */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; if (LocaleCompare(image_info->magick,"CMYKA") != 0) { (void) ExportImagePixelArea(image,CMYKQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } else { (void) ExportImagePixelArea(image,CMYKAQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; } break; } case LineInterlace: { /* Line interlacing: CCC...MMM...YYY...KKK...CCC...MMM...YYY...KKK... */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ExportImagePixelArea(image,CyanQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); (void) ExportImagePixelArea(image,MagentaQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); (void) ExportImagePixelArea(image,YellowQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); (void) ExportImagePixelArea(image,BlackQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); if (LocaleCompare(image_info->magick,"CMYKA") == 0) { (void) ExportImagePixelArea(image,AlphaQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; } break; } case PlaneInterlace: case PartitionInterlace: { /* Plane interlacing: CCCCCC...MMMMMM...YYYYYY...KKKKKK... */ if (image_info->interlace == PartitionInterlace) { AppendImageFormat("C",image->filename); status= OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,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; (void) ExportImagePixelArea(image,CyanQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("M",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,image); } if (!MagickMonitorFormatted(100,400,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ExportImagePixelArea(image,MagentaQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("Y",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,image); } if (!MagickMonitorFormatted(200,400,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ExportImagePixelArea(image,YellowQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("K",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,image); } if (!MagickMonitorFormatted(200,400,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ExportImagePixelArea(image,BlackQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } if (LocaleCompare(image_info->magick,"CMYKA") == 0) { if (!MagickMonitorFormatted(300,400,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("A",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,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; (void) ExportImagePixelArea(image,AlphaQuantum,quantum_size,pixels, &export_options,&export_info); (void) WriteBlob(image,export_info.bytes_exported,pixels); } } if (image_info->interlace == PartitionInterlace) (void) strlcpy(image->filename,image_info->filename,MaxTextExtent); if (!MagickMonitorFormatted(400,400,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; break; } } if (image->next == (Image *) NULL) break; image=SyncNextImageInList(image); status=MagickMonitorFormatted(scene++,GetImageListLength(image), &image->exception,SaveImagesText, image->filename); if (status == False) break; } while (image_info->adjoin); MagickFreeMemory(pixels); if (image_info->adjoin) while (image->previous != (Image *) NULL) image=image->previous; CloseBlob(image); return(True); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e G R A Y I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteGRAYImage() writes an image to a file as gray scale intensity % values. % % The format of the WriteGRAYImage method is: % % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, % Image *image) % % A description of each parameter follows. % % o image_info: The image info. % % o image: The image. % */ static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, Image *image) { long y; MagickBooleanType status; MagickOffsetType scene; QuantumInfo quantum_info; register const PixelPacket *p; size_t packet_size; unsigned char *pixels; /* 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(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); /* Convert image to gray scale PseudoColor class. */ scene=0; do { /* Allocate memory for pixels. */ GetQuantumInfo(image_info,&quantum_info); if (image_info->colorspace == UndefinedColorspace) (void) SetImageColorspace(image,RGBColorspace); packet_size=(size_t) (image->depth+7)/8; pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size* sizeof(*pixels)); if (pixels == (unsigned char *) NULL) ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); /* Convert MIFF to GRAY raster pixels. */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ImportQuantumPixels(image,&quantum_info,GrayQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } pixels=(unsigned char *) RelinquishMagickMemory(pixels); if (GetNextImageInList(image) == (Image *) NULL) break; image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(SaveImagesTag,scene, GetImageListLength(image),image->client_data); if (status == MagickFalse) break; } scene++; } while (image_info->adjoin != MagickFalse); CloseBlob(image); return(MagickTrue); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d J B I G I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadJBIGImage() reads a JBIG image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadJBIGImage method is: % % Image *ReadJBIGImage(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 *ReadJBIGImage(const ImageInfo *image_info, ExceptionInfo *exception) { Image *image; IndexPacket index; long length, y; MagickBooleanType status; register IndexPacket *indexes; register long x; register PixelPacket *q; register unsigned char *p; ssize_t count; struct jbg_dec_state jbig_info; unsigned char bit, *buffer, byte; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", 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) { image=DestroyImageList(image); return((Image *) NULL); } /* Initialize JBIG toolkit. */ jbg_dec_init(&jbig_info); jbg_dec_maxsize(&jbig_info,(unsigned long) image->columns,(unsigned long) image->rows); image->columns=jbg_dec_getwidth(&jbig_info); image->rows=jbg_dec_getheight(&jbig_info); image->depth=8; image->storage_class=PseudoClass; image->colors=2; /* Read JBIG file. */ buffer=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferSize, sizeof(*buffer)); if (buffer == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); status=JBG_EAGAIN; do { length=(long) ReadBlob(image,MagickMaxBufferSize,buffer); if (length == 0) break; p=buffer; count=0; while ((length > 0) && ((status == JBG_EAGAIN) || (status == JBG_EOK))) { size_t count; status=jbg_dec_in(&jbig_info,p,length,&count); p+=count; length-=(long) count; } } while ((status == JBG_EAGAIN) || (status == JBG_EOK)); /* Create colormap. */ image->columns=jbg_dec_getwidth(&jbig_info); image->rows=jbg_dec_getheight(&jbig_info); if (AllocateImageColormap(image,2) == MagickFalse) { buffer=(unsigned char *) RelinquishMagickMemory(buffer); ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } image->colormap[0].red=0; image->colormap[0].green=0; image->colormap[0].blue=0; image->colormap[1].red=QuantumRange; image->colormap[1].green=QuantumRange; image->colormap[1].blue=QuantumRange; image->x_resolution=300; image->y_resolution=300; if (image_info->ping != MagickFalse) { (void) CloseBlob(image); return(GetFirstImageInList(image)); } /* Convert X bitmap image to pixel packets. */ if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } p=jbg_dec_getimage(&jbig_info,0); for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(image); bit=0; byte=0; for (x=0; x < (long) image->columns; x++) { if (bit == 0) byte=(*p++); index=(byte & 0x80) ? 0 : 1; bit++; byte<<=1; if (bit == 8) bit=0; indexes[x]=index; *q++=image->colormap[(long) index]; } if (SyncImagePixels(image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } /* Free scale resource. */ jbg_dec_free(&jbig_info); buffer=(unsigned char *) RelinquishMagickMemory(buffer); (void) CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e R G B I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteRGBImage() writes an image to a file in red, green, and blue % rasterfile format. % % The format of the WriteRGBImage method is: % % MagickBooleanType WriteRGBImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o image_info: The image info. % % o image: The image. % */ static MagickBooleanType WriteRGBImage(const ImageInfo *image_info,Image *image) { long y; MagickBooleanType status; MagickOffsetType scene; QuantumInfo quantum_info; register const PixelPacket *p; size_t packet_size; unsigned char *pixels; /* Allocate memory for pixels. */ 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(),"%s",image->filename); packet_size=(size_t) ((3*image->depth+7)/8); if ((LocaleCompare(image_info->magick,"RGBA") == 0) || (LocaleCompare(image_info->magick,"RGBO") == 0)) packet_size+=(image->depth+7)/8; pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size* sizeof(*pixels)); if (pixels == (unsigned char *) NULL) ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); if (image_info->interlace != PartitionInterlace) { /* Open output image file. */ status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); } scene=0; do { /* Convert MIFF to RGB raster pixels. */ GetQuantumInfo(image_info,&quantum_info); if (image_info->colorspace == UndefinedColorspace) (void) SetImageColorspace(image,RGBColorspace); if (LocaleCompare(image_info->magick,"RGBA") == 0) if (image->matte == MagickFalse) (void) SetImageOpacity(image,OpaqueOpacity); switch (image_info->interlace) { case NoInterlace: default: { /* No interlacing: RGBRGBRGBRGBRGBRGB... */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; if (LocaleCompare(image_info->magick,"RGBA") != 0) { (void) ImportQuantumPixels(image,&quantum_info,RGBQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } else { if (LocaleCompare(image_info->magick,"RGBA") == 0) (void) ImportQuantumPixels(image,&quantum_info,RGBAQuantum, pixels); else (void) ImportQuantumPixels(image,&quantum_info,RGBOQuantum, pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } break; } case LineInterlace: { /* Line interlacing: RRR...GGG...BBB...RRR...GGG...BBB... */ packet_size=(image->depth+7)/8; for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ImportQuantumPixels(image,&quantum_info,RedQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); (void) ImportQuantumPixels(image,&quantum_info,GreenQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); (void) ImportQuantumPixels(image,&quantum_info,BlueQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); if (LocaleCompare(image_info->magick,"RGBA") == 0) { if (LocaleCompare(image_info->magick,"RGBA") == 0) (void) ImportQuantumPixels(image,&quantum_info,AlphaQuantum, pixels); else (void) ImportQuantumPixels(image,&quantum_info,OpacityQuantum, pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } break; } case PlaneInterlace: case PartitionInterlace: { /* Plane interlacing: RRRRRR...GGGGGG...BBBBBB... */ packet_size=(image->depth+7)/8; if (image_info->interlace == PartitionInterlace) { AppendImageFormat("R",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == MagickFalse) return(status); } for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ImportQuantumPixels(image,&quantum_info,RedQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("G",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == MagickFalse) return(status); } if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImageTag,100,400, image->client_data); if (status == MagickFalse) break; } for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ImportQuantumPixels(image,&quantum_info,GreenQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("B",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == MagickFalse) return(status); } if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImageTag,200,400, image->client_data); if (status == MagickFalse) break; } for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ImportQuantumPixels(image,&quantum_info,BlueQuantum,pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } if (LocaleCompare(image_info->magick,"RGBA") == 0) { if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImageTag,300,400, image->client_data); if (status == MagickFalse) break; } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("A",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode, &image->exception); if (status == MagickFalse) return(status); } for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1, &image->exception); if (p == (const PixelPacket *) NULL) break; if (LocaleCompare(image_info->magick,"RGBA") == 0) (void) ImportQuantumPixels(image,&quantum_info,AlphaQuantum, pixels); else (void) ImportQuantumPixels(image,&quantum_info,OpacityQuantum, pixels); (void) WriteBlob(image,packet_size*image->columns,pixels); } } if (image_info->interlace == PartitionInterlace) (void) CopyMagickString(image->filename,image_info->filename, MaxTextExtent); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImageTag,400,400, image->client_data); if (status == MagickFalse) break; } break; } } if (GetNextImageInList(image) == (Image *) NULL) break; image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(SaveImagesTag,scene, GetImageListLength(image),image->client_data); if (status == MagickFalse) break; } scene++; } while (image_info->adjoin != MagickFalse); pixels=(unsigned char *) RelinquishMagickMemory(pixels); CloseBlob(image); return(MagickTrue); }
unsigned long *HalfImage(unsigned long *image,int colums,int rows) { #define Minify(weight) \ total.red+=(weight)*(r->red); \ total.green+=(weight)*(r->green); \ total.blue+=(weight)*(r->blue); \ total.opacity+=(weight)*(r->opacity); \ r++; unsigned long *minify_image; long y; /* Initialize minified image. */ minify_image= image; /* Reduce each row. */ { unsigned long row_count=0,zero=0; float red,green,blue,opacity; zero=0; for (y=0; y < (long) minify_image->rows; y++) { DoublePixelPacket total; register const PixelPacket *p, *r; register long x; register PixelPacket *q; MagickBool thread_status; p=AcquireImagePixels(image,-2,2*(y-1),image->columns+4,4,exception); q=SetImagePixelsEx(minify_image,0,y,minify_image->columns,1,exception); for (x=0; x < columns/2; x++) { /* Compute weighted average of target pixel color components. */ total=zero; r=p; Minify(3.0); Minify(7.0); Minify(7.0); Minify(3.0); r=p+(image->columns+4); Minify(7.0); Minify(15.0); Minify(15.0); Minify(7.0); r=p+2*(image->columns+4); Minify(7.0); Minify(15.0); Minify(15.0); Minify(7.0); r=p+3*(image->columns+4); Minify(3.0); Minify(7.0); Minify(7.0); Minify(3.0); q->red=(Quantum) (total.red/128.0+0.5); q->green=(Quantum) (total.green/128.0+0.5); q->blue=(Quantum) (total.blue/128.0+0.5); q->opacity=(Quantum) (total.opacity/128.0+0.5); p+=2; q++; } if (!SyncImagePixelsEx(minify_image,exception)) thread_status=MagickFail; #if defined(HAVE_OPENMP) # pragma omp critical (GM_MinifyImage) #endif { row_count++; if (QuantumTick(row_count,image->rows)) if (!MagickMonitorFormatted(row_count,image->rows,exception, MinifyImageText,image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } } minify_image->is_grayscale=image->is_grayscale; return(minify_image); }
static Image *ReadJP2Image(const ImageInfo *image_info, ExceptionInfo *exception) { Image *image; long y; jas_image_t *jp2_image; jas_matrix_t *pixels; jas_stream_t *jp2_stream; register long x; register PixelPacket *q; int component, components[4], number_components; Quantum *channel_lut[4]; unsigned int status; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AllocateImage(image_info); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); /* Obtain a JP2 Stream. */ jp2_stream=JP2StreamManager(image); if (jp2_stream == (jas_stream_t *) NULL) ThrowReaderException(DelegateError,UnableToManageJP2Stream,image); jp2_image=jas_image_decode(jp2_stream,-1,0); if (jp2_image == (jas_image_t *) NULL) { (void) jas_stream_close(jp2_stream); ThrowReaderException(DelegateError,UnableToDecodeImageFile,image); } /* Validate that we can handle the image and obtain component indexes. */ switch (jas_clrspc_fam(jas_image_clrspc(jp2_image))) { case JAS_CLRSPC_FAM_RGB: { if (((components[0]= jas_image_getcmptbytype(jp2_image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0) || ((components[1]= jas_image_getcmptbytype(jp2_image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0) || ((components[2]= jas_image_getcmptbytype(jp2_image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0)) { (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); ThrowReaderException(CorruptImageError,MissingImageChannel,image); } number_components=3; (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Image is in RGB colorspace family"); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "RED is in channel %d, GREEN is in channel %d, BLUE is in channel %d", components[0],components[1],components[2]); if((components[3]=jas_image_getcmptbytype(jp2_image, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_OPACITY))) > 0) { image->matte=MagickTrue; (void) LogMagickEvent(CoderEvent,GetMagickModule(), "OPACITY is in channel %d",components[3]); number_components++; } break; } case JAS_CLRSPC_FAM_GRAY: { if ((components[0]= jas_image_getcmptbytype(jp2_image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y))) < 0) { (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); ThrowReaderException(CorruptImageError,MissingImageChannel,image); } (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Image is in GRAY colorspace family"); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "GRAY is in channel %d",components[0]); number_components=1; break; } case JAS_CLRSPC_FAM_YCBCR: { components[0]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_YCBCR_Y); components[1]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_YCBCR_CB); components[2]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_YCBCR_CR); if ((components[0] < 0) || (components[1] < 0) || (components[2] < 0)) { (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); ThrowReaderException(CorruptImageError,MissingImageChannel,image); } number_components=3; components[3]=jas_image_getcmptbytype(jp2_image,JAS_IMAGE_CT_OPACITY); if (components[3] > 0) { image->matte=True; number_components++; } image->colorspace=YCbCrColorspace; (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Image is in YCBCR colorspace family"); break; } default: { (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); ThrowReaderException(CoderError,ColorspaceModelIsNotSupported,image); } } image->columns=jas_image_width(jp2_image); image->rows=jas_image_height(jp2_image); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "columns=%lu rows=%lu components=%d",image->columns,image->rows, number_components); for (component=0; component < number_components; component++) { if(((unsigned long) jas_image_cmptwidth(jp2_image,components[component]) != image->columns) || ((unsigned long) jas_image_cmptheight(jp2_image,components[component]) != image->rows) || (jas_image_cmpttlx(jp2_image, components[component]) != 0) || (jas_image_cmpttly(jp2_image, components[component]) != 0) || (jas_image_cmpthstep(jp2_image, components[component]) != 1) || (jas_image_cmptvstep(jp2_image, components[component]) != 1) || (jas_image_cmptsgnd(jp2_image, components[component]) != false)) { (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); ThrowReaderException(CoderError,IrregularChannelGeometryNotSupported,image); } } image->matte=number_components > 3; for (component=0; component < number_components; component++) { unsigned int component_depth; component_depth=jas_image_cmptprec(jp2_image,components[component]); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Component[%d] depth is %u",component,component_depth); if (0 == component) image->depth=component_depth; else image->depth=Max(image->depth,component_depth); } (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Image depth is %u",image->depth); if (image_info->ping) { (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); return(image); } /* Allocate Jasper pixels. */ pixels=jas_matrix_create(1,(unsigned int) image->columns); if (pixels == (jas_matrix_t *) NULL) { jas_image_destroy(jp2_image); ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); } /* Allocate and populate channel LUTs */ for (component=0; component < (long) number_components; component++) { unsigned long component_depth, i, max_value; double scale_to_quantum; component_depth=jas_image_cmptprec(jp2_image,components[component]); max_value=MaxValueGivenBits(component_depth); scale_to_quantum=MaxRGBDouble/max_value; (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Channel %d scale is %g", component, scale_to_quantum); channel_lut[component]=MagickAllocateArray(Quantum *,max_value+1,sizeof(Quantum)); if (channel_lut[component] == (Quantum *) NULL) { for ( --component; component >= 0; --component) MagickFreeMemory(channel_lut[component]); jas_matrix_destroy(pixels); jas_image_destroy(jp2_image); ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); } for(i=0; i <= max_value; i++) (channel_lut[component])[i]=scale_to_quantum*i+0.5; } /* Convert JPEG 2000 pixels. */ for (y=0; y < (long) image->rows; y++) { q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; if (1 == number_components) { /* Grayscale */ (void) jas_image_readcmpt(jp2_image,(short) components[0],0, (unsigned int) y, (unsigned int) image->columns,1,pixels); for (x=0; x < (long) image->columns; x++) { q->red=q->green=q->blue=(channel_lut[0])[jas_matrix_getv(pixels,x)]; q->opacity=OpaqueOpacity; q++; } } else { /* Red */ (void) jas_image_readcmpt(jp2_image,(short) components[0],0, (unsigned int) y, (unsigned int) image->columns,1,pixels); for (x=0; x < (long) image->columns; x++) q[x].red=(channel_lut[0])[jas_matrix_getv(pixels,x)]; /* Green */ (void) jas_image_readcmpt(jp2_image,(short) components[1],0, (unsigned int) y, (unsigned int) image->columns,1,pixels); for (x=0; x < (long) image->columns; x++) q[x].green=(channel_lut[1])[jas_matrix_getv(pixels,x)]; /* Blue */ (void) jas_image_readcmpt(jp2_image,(short) components[2],0, (unsigned int) y, (unsigned int) image->columns,1,pixels); for (x=0; x < (long) image->columns; x++) q[x].blue=(channel_lut[2])[jas_matrix_getv(pixels,x)]; /* Opacity */ if (number_components > 3) { (void) jas_image_readcmpt(jp2_image,(short) components[3],0, (unsigned int) y, (unsigned int) image->columns,1,pixels); for (x=0; x < (long) image->columns; x++) q[x].opacity=MaxRGB-(channel_lut[3])[jas_matrix_getv(pixels,x)]; } else { for (x=0; x < (long) image->columns; x++) q[x].opacity=OpaqueOpacity; } } if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,exception,LoadImageText, image->filename, image->columns,image->rows)) break; } if (number_components == 1) image->is_grayscale=MagickTrue; { /* Obtain ICC ICM color profile */ jas_cmprof_t *cm_profile; /* Obtain a pointer to the existing jas_cmprof_t profile handle. */ cm_profile=jas_image_cmprof(jp2_image); if (cm_profile != (jas_cmprof_t *) NULL) { jas_iccprof_t *icc_profile; /* Obtain a copy of the jas_iccprof_t ICC profile handle */ icc_profile=jas_iccprof_createfromcmprof(cm_profile); /* or maybe just icc_profile=cm_profile->iccprof */ if (icc_profile != (jas_iccprof_t *) NULL) { jas_stream_t *icc_stream; icc_stream=jas_stream_memopen(NULL,0); if ((icc_stream != (jas_stream_t *) NULL) && (jas_iccprof_save(icc_profile,icc_stream) == 0) && (jas_stream_flush(icc_stream) == 0)) { jas_stream_memobj_t *blob; blob=(jas_stream_memobj_t *) icc_stream->obj_; if (image->logging) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "ICC profile: %lu bytes",(unsigned long) blob->len_); SetImageProfile(image,"ICM",blob->buf_,blob->len_); (void) jas_stream_close(icc_stream); jas_iccprof_destroy(icc_profile); } } } } for (component=0; component < (long) number_components; component++) MagickFreeMemory(channel_lut[component]); jas_matrix_destroy(pixels); (void) jas_stream_close(jp2_stream); jas_image_destroy(jp2_image); return(image); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d V I F F I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadVIFFImage() reads a Khoros Visualization image file and returns % it. It allocates the memory necessary for the new Image structure and % returns a pointer to the new image. % % The format of the ReadVIFFImage method is: % % Image *ReadVIFFImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadVIFFImage returns a pointer to the image after % reading. A null image is returned if there is a memory shortage or if % the image cannot be read. % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % % */ static Image *ReadVIFFImage(const ImageInfo *image_info, ExceptionInfo *exception) { #define VFF_CM_genericRGB 15 #define VFF_CM_ntscRGB 1 #define VFF_CM_NONE 0 #define VFF_DEP_DECORDER 0x4 #define VFF_DEP_NSORDER 0x8 #define VFF_DES_RAW 0 #define VFF_LOC_IMPLICIT 1 #define VFF_MAPTYP_NONE 0 #define VFF_MAPTYP_1_BYTE 1 #define VFF_MAPTYP_2_BYTE 2 #define VFF_MAPTYP_4_BYTE 4 #define VFF_MAPTYP_FLOAT 5 #define VFF_MAPTYP_DOUBLE 7 #define VFF_MS_NONE 0 #define VFF_MS_ONEPERBAND 1 #define VFF_MS_SHARED 3 #define VFF_TYP_BIT 0 #define VFF_TYP_1_BYTE 1 #define VFF_TYP_2_BYTE 2 #define VFF_TYP_4_BYTE 4 #define VFF_TYP_FLOAT 5 #define VFF_TYP_DOUBLE 9 typedef struct _ViffInfo { unsigned char identifier, file_type, release, version, machine_dependency, reserve[3]; char comment[512]; unsigned long rows, columns, subrows; long x_offset, y_offset; float x_bits_per_pixel, y_bits_per_pixel; unsigned long location_type, location_dimension, number_of_images, number_data_bands, data_storage_type, data_encode_scheme, map_scheme, map_storage_type, map_rows, map_columns, map_subrows, map_enable, maps_per_cycle, color_space_model; } ViffInfo; double min_value, scale_factor, value; Image *image; int bit; long y; MagickBooleanType status; MagickSizeType number_pixels; register IndexPacket *indexes; register long x; register PixelPacket *q; register long i; register unsigned char *p; ssize_t count; unsigned char buffer[7], *viff_pixels; unsigned long bytes_per_pixel, lsb_first, max_packets, quantum; ViffInfo viff_info; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", 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) { image=DestroyImageList(image); return((Image *) NULL); } /* Read VIFF header (1024 bytes). */ count=ReadBlob(image,1,&viff_info.identifier); do { /* Verify VIFF identifier. */ if ((count == 0) || ((unsigned char) viff_info.identifier != 0xab)) ThrowReaderException(CorruptImageError,"NotAVIFFImage"); /* Initialize VIFF image. */ count=ReadBlob(image,7,buffer); viff_info.file_type=buffer[0]; viff_info.release=buffer[1]; viff_info.version=buffer[2]; viff_info.machine_dependency=buffer[3]; count=ReadBlob(image,512,(unsigned char *) viff_info.comment); viff_info.comment[511]='\0'; if (strlen(viff_info.comment) > 4) (void) SetImageProperty(image,"comment",viff_info.comment); if ((viff_info.machine_dependency == VFF_DEP_DECORDER) || (viff_info.machine_dependency == VFF_DEP_NSORDER)) { viff_info.rows=ReadBlobLSBLong(image); viff_info.columns=ReadBlobLSBLong(image); viff_info.subrows=ReadBlobLSBLong(image); viff_info.x_offset=(long) ReadBlobLSBLong(image); viff_info.y_offset=(long) ReadBlobLSBLong(image); viff_info.x_bits_per_pixel=(float) ReadBlobLSBLong(image); viff_info.y_bits_per_pixel=(float) ReadBlobLSBLong(image); viff_info.location_type=ReadBlobLSBLong(image); viff_info.location_dimension=ReadBlobLSBLong(image); viff_info.number_of_images=ReadBlobLSBLong(image); viff_info.number_data_bands=ReadBlobLSBLong(image); viff_info.data_storage_type=ReadBlobLSBLong(image); viff_info.data_encode_scheme=ReadBlobLSBLong(image); viff_info.map_scheme=ReadBlobLSBLong(image); viff_info.map_storage_type=ReadBlobLSBLong(image); viff_info.map_rows=ReadBlobLSBLong(image); viff_info.map_columns=ReadBlobLSBLong(image); viff_info.map_subrows=ReadBlobLSBLong(image); viff_info.map_enable=ReadBlobLSBLong(image); viff_info.maps_per_cycle=ReadBlobLSBLong(image); viff_info.color_space_model=ReadBlobLSBLong(image); } else { viff_info.rows=ReadBlobMSBLong(image); viff_info.columns=ReadBlobMSBLong(image); viff_info.subrows=ReadBlobMSBLong(image); viff_info.x_offset=(long) ReadBlobMSBLong(image); viff_info.y_offset=(long) ReadBlobMSBLong(image); viff_info.x_bits_per_pixel=(float) ReadBlobMSBLong(image); viff_info.y_bits_per_pixel=(float) ReadBlobMSBLong(image); viff_info.location_type=ReadBlobMSBLong(image); viff_info.location_dimension=ReadBlobMSBLong(image); viff_info.number_of_images=ReadBlobMSBLong(image); viff_info.number_data_bands=ReadBlobMSBLong(image); viff_info.data_storage_type=ReadBlobMSBLong(image); viff_info.data_encode_scheme=ReadBlobMSBLong(image); viff_info.map_scheme=ReadBlobMSBLong(image); viff_info.map_storage_type=ReadBlobMSBLong(image); viff_info.map_rows=ReadBlobMSBLong(image); viff_info.map_columns=ReadBlobMSBLong(image); viff_info.map_subrows=ReadBlobMSBLong(image); viff_info.map_enable=ReadBlobMSBLong(image); viff_info.maps_per_cycle=ReadBlobMSBLong(image); viff_info.color_space_model=ReadBlobMSBLong(image); } for (i=0; i < 420; i++) (void) ReadBlobByte(image); image->columns=viff_info.rows; image->rows=viff_info.columns; image->depth=viff_info.x_bits_per_pixel <= 8 ? 8UL : MAGICKCORE_QUANTUM_DEPTH; /* Verify that we can read this VIFF image. */ number_pixels=(MagickSizeType) viff_info.columns*viff_info.rows; if (number_pixels != (size_t) number_pixels) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); if (number_pixels == 0) ThrowReaderException(CoderError,"ImageColumnOrRowSizeIsNotSupported"); if ((viff_info.number_data_bands < 1) || (viff_info.number_data_bands > 4)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); if ((viff_info.data_storage_type != VFF_TYP_BIT) && (viff_info.data_storage_type != VFF_TYP_1_BYTE) && (viff_info.data_storage_type != VFF_TYP_2_BYTE) && (viff_info.data_storage_type != VFF_TYP_4_BYTE) && (viff_info.data_storage_type != VFF_TYP_FLOAT) && (viff_info.data_storage_type != VFF_TYP_DOUBLE)) ThrowReaderException(CoderError,"DataStorageTypeIsNotSupported"); if (viff_info.data_encode_scheme != VFF_DES_RAW) ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported"); if ((viff_info.map_storage_type != VFF_MAPTYP_NONE) && (viff_info.map_storage_type != VFF_MAPTYP_1_BYTE) && (viff_info.map_storage_type != VFF_MAPTYP_2_BYTE) && (viff_info.map_storage_type != VFF_MAPTYP_4_BYTE) && (viff_info.map_storage_type != VFF_MAPTYP_FLOAT) && (viff_info.map_storage_type != VFF_MAPTYP_DOUBLE)) ThrowReaderException(CoderError,"MapStorageTypeIsNotSupported"); if ((viff_info.color_space_model != VFF_CM_NONE) && (viff_info.color_space_model != VFF_CM_ntscRGB) && (viff_info.color_space_model != VFF_CM_genericRGB)) ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported"); if (viff_info.location_type != VFF_LOC_IMPLICIT) ThrowReaderException(CoderError,"LocationTypeIsNotSupported"); if (viff_info.number_of_images != 1) ThrowReaderException(CoderError,"NumberOfImagesIsNotSupported"); if (viff_info.map_rows == 0) viff_info.map_scheme=VFF_MS_NONE; switch ((int) viff_info.map_scheme) { case VFF_MS_NONE: { if (viff_info.number_data_bands < 3) { /* Create linear color ramp. */ image->colors=image->depth <= 8 ? 256UL : 65536UL; if (viff_info.data_storage_type == VFF_TYP_BIT) image->colors=2; if (AllocateImageColormap(image,image->colors) == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } break; } case VFF_MS_ONEPERBAND: case VFF_MS_SHARED: { unsigned char *viff_colormap; /* Allocate VIFF colormap. */ switch ((int) viff_info.map_storage_type) { case VFF_MAPTYP_1_BYTE: bytes_per_pixel=1; break; case VFF_MAPTYP_2_BYTE: bytes_per_pixel=2; break; case VFF_MAPTYP_4_BYTE: bytes_per_pixel=4; break; case VFF_MAPTYP_FLOAT: bytes_per_pixel=4; break; case VFF_MAPTYP_DOUBLE: bytes_per_pixel=8; break; default: bytes_per_pixel=1; break; } image->colors=viff_info.map_columns; if (AllocateImageColormap(image,image->colors) == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors, viff_info.map_rows*bytes_per_pixel*sizeof(*viff_colormap)); if (viff_colormap == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); /* Read VIFF raster colormap. */ count=ReadBlob(image,bytes_per_pixel*image->colors*viff_info.map_rows, viff_colormap); lsb_first=1; if (*(char *) &lsb_first && ((viff_info.machine_dependency != VFF_DEP_DECORDER) && (viff_info.machine_dependency != VFF_DEP_NSORDER))) switch ((int) viff_info.map_storage_type) { case VFF_MAPTYP_2_BYTE: { MSBOrderShort(viff_colormap,(bytes_per_pixel*image->colors* viff_info.map_rows)); break; } case VFF_MAPTYP_4_BYTE: case VFF_MAPTYP_FLOAT: { MSBOrderLong(viff_colormap,(bytes_per_pixel*image->colors* viff_info.map_rows)); break; } default: break; } for (i=0; i < (long) (viff_info.map_rows*image->colors); i++) { switch ((int) viff_info.map_storage_type) { case VFF_MAPTYP_2_BYTE: value=1.0*((short *) viff_colormap)[i]; break; case VFF_MAPTYP_4_BYTE: value=1.0*((int *) viff_colormap)[i]; break; case VFF_MAPTYP_FLOAT: value=((float *) viff_colormap)[i]; break; case VFF_MAPTYP_DOUBLE: value=((double *) viff_colormap)[i]; break; default: value=1.0*viff_colormap[i]; break; } if (i < (long) image->colors) { image->colormap[i].red=ScaleCharToQuantum((unsigned char) value); image->colormap[i].green= ScaleCharToQuantum((unsigned char) value); image->colormap[i].blue=ScaleCharToQuantum((unsigned char) value); } else if (i < (long) (2*image->colors)) image->colormap[i % image->colors].green= ScaleCharToQuantum((unsigned char) value); else if (i < (long) (3*image->colors)) image->colormap[i % image->colors].blue= ScaleCharToQuantum((unsigned char) value); } viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap); break; } default: ThrowReaderException(CoderError,"ColormapTypeNotSupported"); } /* Initialize image structure. */ image->matte=viff_info.number_data_bands == 4 ? MagickTrue : MagickFalse; image->storage_class= (viff_info.number_data_bands < 3 ? PseudoClass : DirectClass); image->columns=viff_info.rows; image->rows=viff_info.columns; if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } /* Allocate VIFF pixels. */ switch ((int) viff_info.data_storage_type) { case VFF_TYP_2_BYTE: bytes_per_pixel=2; break; case VFF_TYP_4_BYTE: bytes_per_pixel=4; break; case VFF_TYP_FLOAT: bytes_per_pixel=4; break; case VFF_TYP_DOUBLE: bytes_per_pixel=8; break; default: bytes_per_pixel=1; break; } if (viff_info.data_storage_type == VFF_TYP_BIT) max_packets=((image->columns+7UL) >> 3UL)*image->rows; else max_packets=(unsigned long) (number_pixels*viff_info.number_data_bands); viff_pixels=(unsigned char *) AcquireQuantumMemory(max_packets, bytes_per_pixel*sizeof(*viff_pixels)); if (viff_pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); count=ReadBlob(image,bytes_per_pixel*max_packets,viff_pixels); lsb_first=1; if (*(char *) &lsb_first && ((viff_info.machine_dependency != VFF_DEP_DECORDER) && (viff_info.machine_dependency != VFF_DEP_NSORDER))) switch ((int) viff_info.data_storage_type) { case VFF_TYP_2_BYTE: { MSBOrderShort(viff_pixels,bytes_per_pixel*max_packets); break; } case VFF_TYP_4_BYTE: case VFF_TYP_FLOAT: { MSBOrderLong(viff_pixels,bytes_per_pixel*max_packets); break; } default: break; } min_value=0.0; scale_factor=1.0; if ((viff_info.data_storage_type != VFF_TYP_1_BYTE) && (viff_info.map_scheme == VFF_MS_NONE)) { double max_value; /* Determine scale factor. */ switch ((int) viff_info.data_storage_type) { case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[0]; break; case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[0]; break; case VFF_TYP_FLOAT: value=((float *) viff_pixels)[0]; break; case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[0]; break; default: value=1.0*viff_pixels[0]; break; } max_value=value; min_value=value; for (i=0; i < (long) max_packets; i++) { switch ((int) viff_info.data_storage_type) { case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break; case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break; case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break; case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break; default: value=1.0*viff_pixels[i]; break; } if (value > max_value) max_value=value; else if (value < min_value) min_value=value; } if ((min_value == 0) && (max_value == 0)) scale_factor=0; else if (min_value == max_value) { scale_factor=(MagickRealType) QuantumRange/min_value; min_value=0; } else scale_factor=(MagickRealType) QuantumRange/(max_value-min_value); } /* Convert pixels to Quantum size. */ p=(unsigned char *) viff_pixels; for (i=0; i < (long) max_packets; i++) { switch ((int) viff_info.data_storage_type) { case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break; case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break; case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break; case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break; default: value=1.0*viff_pixels[i]; break; } if (viff_info.map_scheme == VFF_MS_NONE) { value=(value-min_value)*scale_factor; if (value > QuantumRange) value=QuantumRange; else if (value < 0) value=0; } *p=(unsigned char) value; p++; } /* Convert VIFF raster image to pixel packets. */ p=(unsigned char *) viff_pixels; if (viff_info.data_storage_type == VFF_TYP_BIT) { /* Convert bitmap scanline. */ (void) SetImageType(image,BilevelType); (void) SetImageType(image,PaletteType); for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(image); for (x=0; x < (long) (image->columns-7); x+=8) { for (bit=0; bit < 8; bit++) if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0)) { quantum=(unsigned long) indexes[x+bit]; quantum|=0x01; indexes[x+bit]=(IndexPacket) quantum; } p++; } if ((image->columns % 8) != 0) { for (bit=0; bit < (long) (image->columns % 8); bit++) if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0)) { quantum=(unsigned long) indexes[x+bit]; quantum|=0x01; indexes[x+bit]=(IndexPacket) quantum; } p++; } if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } } else if (image->storage_class == PseudoClass) for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(image); for (x=0; x < (long) image->columns; x++) indexes[x]=(IndexPacket) (*p++); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } else { /* Convert DirectColor scanline. */ number_pixels=(MagickSizeType) image->columns*image->rows; 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++) { q->red=ScaleCharToQuantum(*p); q->green=ScaleCharToQuantum(*(p+number_pixels)); q->blue=ScaleCharToQuantum(*(p+2*number_pixels)); if (image->colors != 0) { q->red=image->colormap[(long) q->red].red; q->green=image->colormap[(long) q->green].green; q->blue=image->colormap[(long) q->blue].blue; } q->opacity=(Quantum) (image->matte ? QuantumRange- ScaleCharToQuantum(*(p+number_pixels*3)) : OpaqueOpacity); p++; q++; } if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } } viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels); if (image->storage_class == PseudoClass) (void) SyncImage(image); if (EOFBlob(image) != MagickFalse) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); break; } /* Proceed to next image. */ if (image_info->number_scenes != 0) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; count=ReadBlob(image,1,&viff_info.identifier); if ((count != 0) && (viff_info.identifier == 0xab)) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (GetNextImageInList(image) == (Image *) NULL) { image=DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImagesTag,TellBlob(image), GetBlobSize(image),image->client_data); if (status == MagickFalse) break; } } } while ((count != 0) && (viff_info.identifier == 0xab));
static Image *ReadPlasmaImage(const ImageInfo *image_info, ExceptionInfo *exception) { #define PlasmaImageTag "Plasma/Image" Image *image; ImageInfo *read_info; long y; MagickBooleanType status; register long x; register PixelPacket *q; register unsigned long i; SegmentInfo segment_info; unsigned long depth, max_depth; /* Recursively apply plasma to the image. */ read_info=CloneImageInfo(image_info); SetImageInfoBlob(read_info,(void *) NULL,0); (void) FormatMagickString(read_info->filename,MaxTextExtent, "gradient:%s",image_info->filename); image=ReadImage(read_info,exception); read_info=DestroyImageInfo(read_info); if (image == (Image *) NULL) return((Image *) NULL); image->storage_class=DirectClass; for (y=0; y < (long) image->rows; y++) { q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; for (x=0; x < (long) image->columns; x++) { q->opacity=(Quantum) (QuantumRange/2); q++; } if (SyncImagePixels(image) == MagickFalse) break; } segment_info.x1=0; segment_info.y1=0; segment_info.x2=(double) image->columns-1; segment_info.y2=(double) image->rows-1; if (LocaleCompare(image_info->filename,"fractal") == 0) { /* Seed pixels before recursion. */ PlasmaPixel(image,segment_info.x1,segment_info.y1); PlasmaPixel(image,segment_info.x1,(segment_info.y1+segment_info.y2)/2); PlasmaPixel(image,segment_info.x1,segment_info.y2); PlasmaPixel(image,(segment_info.x1+segment_info.x2)/2,segment_info.y1); PlasmaPixel(image,(segment_info.x1+segment_info.x2)/2, (segment_info.y1+segment_info.y2)/2); PlasmaPixel(image,(segment_info.x1+segment_info.x2)/2,segment_info.y2); PlasmaPixel(image,segment_info.x2,segment_info.y1); PlasmaPixel(image,segment_info.x2,(segment_info.y1+segment_info.y2)/2); PlasmaPixel(image,segment_info.x2,segment_info.y2); } i=(unsigned long) MagickMax(image->columns,image->rows)/2; for (max_depth=0; i != 0; max_depth++) i>>=1; for (depth=1; ; depth++) { if (PlasmaImage(image,&segment_info,0,depth) != MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick((MagickOffsetType) depth,max_depth) != MagickFalse)) { status=image->progress_monitor(PlasmaImageTag,(MagickOffsetType) depth, max_depth,image->client_data); if (status == MagickFalse) break; } } (void) SetImageOpacity(image,OpaqueOpacity); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d O T B I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadOTBImage() reads a on-the-air (level 0) bitmap and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadOTBImage method is: % % Image *ReadOTBImage(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 *ReadOTBImage(const ImageInfo *image_info,ExceptionInfo *exception) { #define GetBit(a,i) (((a) >> (i)) & 1L) Image *image; int byte; long y; MagickBooleanType status; register IndexPacket *indexes; register long x; register PixelPacket *q; unsigned char bit, info, depth; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", 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) { image=DestroyImageList(image); return((Image *) NULL); } /* Initialize image structure. */ info=(unsigned char) ReadBlobByte(image); if (GetBit(info,4) == 0) { image->columns=(unsigned long) ReadBlobByte(image); image->rows=(unsigned long) ReadBlobByte(image); } else { image->columns=(unsigned long) ReadBlobMSBShort(image); image->rows=(unsigned long) ReadBlobMSBShort(image); } if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); depth=(unsigned char) ReadBlobByte(image); if (depth != 1) ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported"); if (AllocateImageColormap(image,2) == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); if (image_info->ping != MagickFalse) { CloseBlob(image); return(GetFirstImageInList(image)); } if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } /* Convert bi-level image to pixel packets. */ for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(image); bit=0; byte=0; for (x=0; x < (long) image->columns; x++) { if (bit == 0) { byte=ReadBlobByte(image); if (byte == EOF) ThrowReaderException(CorruptImageError,"CorruptImage"); } indexes[x]=(IndexPacket) ((byte & (0x01 << (7-bit))) ? 0x00 : 0x01); bit++; if (bit == 8) bit=0; } if (SyncImagePixels(image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } (void) SyncImage(image); if (EOFBlob(image) != MagickFalse) ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e O T B I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteOTBImage() writes an image to a file in the On-the-air Bitmap % (level 0) image format. % % The format of the WriteOTBImage method is: % % MagickBooleanType WriteOTBImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o image_info: The image info. % % o image: The image. % % */ static MagickBooleanType WriteOTBImage(const ImageInfo *image_info,Image *image) { #define SetBit(a,i,set) \ a=(unsigned char) ((set) ? (a) | (1L << (i)) : (a) & ~(1L << (i))) long y; MagickBooleanType status; register const PixelPacket *p; register IndexPacket *indexes; register long x; unsigned char bit, byte, info; /* 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(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); if (image_info->colorspace == UndefinedColorspace) (void) SetImageColorspace(image,RGBColorspace); /* Convert image to a bi-level image. */ (void) SetImageType(image,BilevelType); info=0; if ((image->columns >= 256) || (image->rows >= 256)) SetBit(info,4,1); (void) WriteBlobByte(image,info); if ((image->columns >= 256) || (image->rows >= 256)) { (void) WriteBlobMSBShort(image,(unsigned short) image->columns); (void) WriteBlobMSBShort(image,(unsigned short) image->rows); } else { (void) WriteBlobByte(image,(unsigned char) image->columns); (void) WriteBlobByte(image,(unsigned char) image->rows); } (void) WriteBlobByte(image,1); /* depth */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; indexes=GetIndexes(image); bit=0; byte=0; for (x=0; x < (long) image->columns; x++) { if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0)) byte|=0x1 << (7-bit); bit++; if (bit == 8) { (void) WriteBlobByte(image,byte); bit=0; byte=0; } } if (bit != 0) (void) WriteBlobByte(image,byte); if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } CloseBlob(image); return(MagickTrue); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e P I C O N I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Procedure WritePICONImage() writes an image to a file in the Personal Icon % format. % % The format of the WritePICONImage method is: % % MagickBooleanType WritePICONImage(const ImageInfo *image_info, % Image *image) % % A description of each parameter follows. % % o image_info: the image info. % % o image: The image. % */ static MagickBooleanType WritePICONImage(const ImageInfo *image_info, Image *image) { #define ColormapExtent 155 #define GraymapExtent 95 #define PiconGeometry "48x48>" static unsigned char Colormap[]= { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x06, 0x00, 0x05, 0x00, 0xf4, 0x05, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x4f, 0x4f, 0x70, 0x80, 0x90, 0x7e, 0x7e, 0x7e, 0xdc, 0xdc, 0xdc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x80, 0x00, 0x00, 0xff, 0x1e, 0x90, 0xff, 0x87, 0xce, 0xeb, 0xe6, 0xe6, 0xfa, 0x00, 0xff, 0xff, 0x80, 0x00, 0x80, 0xb2, 0x22, 0x22, 0x2e, 0x8b, 0x57, 0x32, 0xcd, 0x32, 0x00, 0xff, 0x00, 0x98, 0xfb, 0x98, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x63, 0x47, 0xff, 0xa5, 0x00, 0xff, 0xd7, 0x00, 0xff, 0xff, 0x00, 0xee, 0x82, 0xee, 0xa0, 0x52, 0x2d, 0xcd, 0x85, 0x3f, 0xd2, 0xb4, 0x8c, 0xf5, 0xde, 0xb3, 0xff, 0xfa, 0xcd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x05, 0x18, 0x20, 0x10, 0x08, 0x03, 0x51, 0x18, 0x07, 0x92, 0x28, 0x0b, 0xd3, 0x38, 0x0f, 0x14, 0x49, 0x13, 0x55, 0x59, 0x17, 0x96, 0x69, 0x1b, 0xd7, 0x85, 0x00, 0x3b, }, Graymap[]= { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x04, 0x00, 0x04, 0x00, 0xf3, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x12, 0x21, 0x21, 0x21, 0x33, 0x33, 0x33, 0x45, 0x45, 0x45, 0x54, 0x54, 0x54, 0x66, 0x66, 0x66, 0x78, 0x78, 0x78, 0x87, 0x87, 0x87, 0x99, 0x99, 0x99, 0xab, 0xab, 0xab, 0xba, 0xba, 0xba, 0xcc, 0xcc, 0xcc, 0xde, 0xde, 0xde, 0xed, 0xed, 0xed, 0xff, 0xff, 0xff, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x04, 0x0c, 0x10, 0x04, 0x31, 0x48, 0x31, 0x07, 0x25, 0xb5, 0x58, 0x73, 0x4f, 0x04, 0x00, 0x3b, }; #define MaxCixels 92 static const char Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk" "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|"; char buffer[MaxTextExtent], basename[MaxTextExtent], name[MaxTextExtent], symbol[MaxTextExtent]; Image *picon, *map; ImageInfo *blob_info; long j, k, y; MagickBooleanType status, transparent; MagickPixelPacket pixel; RectangleInfo geometry; register const PixelPacket *p; register IndexPacket *indexes; register long i, x; register PixelPacket *q; unsigned long characters_per_pixel, colors; /* 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(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); if (image_info->colorspace == UndefinedColorspace) (void) SetImageColorspace(image,RGBColorspace); SetGeometry(image,&geometry); (void) ParseMetaGeometry(PiconGeometry,&geometry.x,&geometry.y, &geometry.width,&geometry.height); picon=ResizeImage(image,geometry.width,geometry.height,TriangleFilter,1.0, &image->exception); blob_info=CloneImageInfo(image_info); (void) AcquireUniqueFilename(blob_info->filename); if ((image_info->type != TrueColorType) && (IsGrayImage(image,&image->exception) != MagickFalse)) map=BlobToImage(blob_info,Graymap,GraymapExtent,&image->exception); else map=BlobToImage(blob_info,Colormap,ColormapExtent,&image->exception); (void) RelinquishUniqueFileResource(blob_info->filename); blob_info=DestroyImageInfo(blob_info); if ((picon == (Image *) NULL) || (map == (Image *) NULL)) return(MagickFalse); status=MapImage(picon,map,image_info->dither); map=DestroyImage(map); transparent=MagickFalse; if (picon->storage_class == PseudoClass) { CompressImageColormap(picon); if (picon->matte != MagickFalse) transparent=MagickTrue; } else { /* Convert DirectClass to PseudoClass picon. */ if (picon->matte != MagickFalse) { /* Map all the transparent pixels. */ for (y=0; y < (long) picon->rows; y++) { q=GetImagePixels(picon,0,y,picon->columns,1); if (q == (PixelPacket *) NULL) break; for (x=0; x < (long) picon->columns; x++) { if (q->opacity == (Quantum) TransparentOpacity) transparent=MagickTrue; else q->opacity=OpaqueOpacity; q++; } if (SyncImagePixels(picon) == MagickFalse) break; } } (void) SetImageType(picon,PaletteType); } colors=picon->colors; if (transparent != MagickFalse) { colors++; picon->colormap=(PixelPacket *) ResizeQuantumMemory((void **) picon->colormap,(size_t) colors,sizeof(*picon->colormap)); if (picon->colormap == (PixelPacket *) NULL) ThrowWriterException(ResourceLimitError,"MemoryAllocationError"); for (y=0; y < (long) picon->rows; y++) { q=GetImagePixels(picon,0,y,picon->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(picon); for (x=0; x < (long) picon->columns; x++) { if (q->opacity == (Quantum) TransparentOpacity) indexes[x]=(IndexPacket) picon->colors; q++; } if (SyncImagePixels(picon) == MagickFalse) break; } } /* Compute the character per pixel. */ characters_per_pixel=1; for (k=MaxCixels; (long) colors > k; k*=MaxCixels) characters_per_pixel++; /* XPM header. */ (void) WriteBlobString(image,"/* XPM */\n"); GetPathComponent(picon->filename,BasePath,basename); (void) FormatMagickString(buffer,MaxTextExtent, "static char *%s[] = {\n",basename); (void) WriteBlobString(image,buffer); (void) WriteBlobString(image,"/* columns rows colors chars-per-pixel */\n"); (void) FormatMagickString(buffer,MaxTextExtent,"\"%lu %lu %lu %ld\",\n", picon->columns,picon->rows,colors,characters_per_pixel); (void) WriteBlobString(image,buffer); GetMagickPixelPacket(image,&pixel); for (i=0; i < (long) colors; i++) { /* Define XPM color. */ SetMagickPixelPacket(image,picon->colormap+i,(IndexPacket *) NULL,&pixel); pixel.colorspace=RGBColorspace; pixel.depth=8; pixel.opacity=(MagickRealType) OpaqueOpacity; (void) QueryMagickColorname(image,&pixel,XPMCompliance,MagickFalse,name, &image->exception); if (LocaleNCompare(name,"rgb",3) == 0) (void) QueryMagickColorname(image,&pixel,XPMCompliance,MagickTrue,name, &image->exception); if (transparent != MagickFalse) { if (i == (long) (colors-1)) (void) CopyMagickString(name,"grey75",MaxTextExtent); } /* Write XPM color. */ k=i % MaxCixels; symbol[0]=Cixel[k]; for (j=1; j < (long) characters_per_pixel; j++) { k=((i-k)/MaxCixels) % MaxCixels; symbol[j]=Cixel[k]; } symbol[j]='\0'; (void) FormatMagickString(buffer,MaxTextExtent,"\"%s c %s\",\n", symbol,name); (void) WriteBlobString(image,buffer); } /* Define XPM pixels. */ (void) WriteBlobString(image,"/* pixels */\n"); for (y=0; y < (long) picon->rows; y++) { p=AcquireImagePixels(picon,0,y,picon->columns,1,&picon->exception); if (p == (const PixelPacket *) NULL) break; indexes=GetIndexes(picon); (void) WriteBlobString(image,"\""); for (x=0; x < (long) picon->columns; x++) { k=((long) indexes[x] % MaxCixels); symbol[0]=Cixel[k]; for (j=1; j < (long) characters_per_pixel; j++) { k=(((int) indexes[x]-k)/MaxCixels) % MaxCixels; symbol[j]=Cixel[k]; } symbol[j]='\0'; (void) CopyMagickString(buffer,symbol,MaxTextExtent); (void) WriteBlobString(image,buffer); } (void) FormatMagickString(buffer,MaxTextExtent,"\"%s\n", y == (long) (picon->rows-1) ? "" : ","); (void) WriteBlobString(image,buffer); if (QuantumTick(y,picon->rows) != MagickFalse) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,picon->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,picon->rows, image->client_data); if (status == MagickFalse) break; } } picon=DestroyImage(picon); (void) WriteBlobString(image,"};\n"); (void) CloseBlob(image); return(MagickTrue); }
MagickExport MagickPassFail GradientImage(Image *image, const PixelPacket *start_color, const PixelPacket *stop_color) { const unsigned long image_rows=image->rows, image_columns=image->columns; long y; unsigned long row_count=0; MagickPassFail status=MagickPass; /* Determine (Hue, Saturation, Brightness) gradient. */ assert(image != (const Image *) NULL); assert(image->signature == MagickSignature); assert(start_color != (const PixelPacket *) NULL); assert(stop_color != (const PixelPacket *) NULL); /* Generate gradient pixels. */ #if defined(HAVE_OPENMP) # pragma omp parallel for shared(row_count, status) #endif for (y=0; y < (long) image->rows; y++) { MagickPassFail thread_status; register long x; register PixelPacket *q; #if defined(HAVE_OPENMP) # pragma omp critical (GM_GradientImage) #endif thread_status=status; if (thread_status == MagickFail) continue; q=SetImagePixelsEx(image,0,y,image->columns,1,&image->exception); if (q == (PixelPacket *) NULL) thread_status=MagickFail; if (q != (PixelPacket *) NULL) { for (x=0; x < (long) image->columns; x++) { BlendCompositePixel(&q[x],start_color,stop_color,(double) MaxRGB*(y*image_columns+x)/(image_columns*image_rows)); } if (!SyncImagePixelsEx(image,&image->exception)) thread_status=MagickFail; } #if defined(HAVE_OPENMP) # pragma omp critical (GM_GradientImage) #endif { row_count++; if (QuantumTick(row_count,image->rows)) if (!MagickMonitorFormatted(row_count,image->rows,&image->exception, GradientImageText,image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } if (IsGray(*start_color) && IsGray(*stop_color)) image->is_grayscale=MagickTrue; if (IsMonochrome(*start_color) && ColorMatch(start_color,stop_color)) image->is_monochrome=MagickTrue; return(status); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d A V S I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadAVSImage() reads an AVS X image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadAVSImage method is: % % Image *ReadAVSImage(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 *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; long y; MagickBooleanType status; register long x; register PixelPacket *q; register unsigned char *p; ssize_t count; size_t length; unsigned char *pixels; unsigned long height, width; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", 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) { image=DestroyImageList(image); return((Image *) NULL); } /* Read AVS X image. */ width=ReadBlobMSBLong(image); height=ReadBlobMSBLong(image); if ((width == ~0UL) || (height == ~0UL)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); do { /* Convert AVS raster image to pixel packets. */ image->columns=width; image->rows=height; image->depth=8; if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } length=(size_t) image->columns; pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels)); if (pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); length*=4*sizeof(*pixels); for (y=0; y < (long) image->rows; y++) { count=ReadBlob(image,length,pixels); if ((size_t) count != length) ThrowReaderException(CorruptImageError,"UnableToReadImageData"); p=pixels; q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; for (x=0; x < (long) image->columns; x++) { q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++)); q->red=ScaleCharToQuantum(*p++); q->green=ScaleCharToQuantum(*p++); q->blue=ScaleCharToQuantum(*p++); if (q->opacity != OpaqueOpacity) image->matte=MagickTrue; q++; } if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } pixels=(unsigned char *) RelinquishMagickMemory(pixels); if (EOFBlob(image) != MagickFalse) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); break; } /* Proceed to next image. */ if (image_info->number_scenes != 0) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; width=ReadBlobMSBLong(image); height=ReadBlobMSBLong(image); if ((width != ~0UL) && (height != ~0UL)) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (GetNextImageInList(image) == (Image *) NULL) { image=DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImagesTag,TellBlob(image), GetBlobSize(image),image->client_data); if (status == MagickFalse) break; } } } while ((width != ~0UL) && (height != ~0UL)); CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d V I C A R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadVICARImage() reads a VICAR image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadVICARImage method is: % % Image *ReadVICARImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadVICARImage returns a pointer to the image after % reading. A null image is returned if there is a memory shortage or if % the image cannot be read. % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % % */ static Image *ReadVICARImage(const ImageInfo *image_info, ExceptionInfo *exception) { char keyword[MaxTextExtent], value[MaxTextExtent]; Image *image; int c; long y; MagickBooleanType status, value_expected; QuantumInfo quantum_info; register PixelPacket *q; ssize_t count; ssize_t length; unsigned char *scanline; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", 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) { image=DestroyImageList(image); return((Image *) NULL); } /* Decode image header. */ c=ReadBlobByte(image); count=1; if (c == EOF) { image=DestroyImage(image); return((Image *) NULL); } length=0; image->columns=0; image->rows=0; while (isgraph(c) && ((image->columns == 0) || (image->rows == 0))) { if (isalnum(c) == MagickFalse) { c=ReadBlobByte(image); count++; } else { register char *p; /* Determine a keyword and its value. */ p=keyword; do { if ((size_t) (p-keyword) < MaxTextExtent) *p++=c; c=ReadBlobByte(image); count++; } while (isalnum(c) || (c == '_')); *p='\0'; value_expected=MagickFalse; while ((isspace((int) ((unsigned char) c)) != 0) || (c == '=')) { if (c == '=') value_expected=MagickTrue; c=ReadBlobByte(image); count++; } if (value_expected == MagickFalse) continue; p=value; while (isalnum(c)) { if ((size_t) (p-value) < MaxTextExtent) *p++=c; c=ReadBlobByte(image); count++; } *p='\0'; /* Assign a value to the specified keyword. */ if (LocaleCompare(keyword,"Label_RECORDS") == 0) length=(ssize_t) atol(value); if (LocaleCompare(keyword,"LBLSIZE") == 0) length=(ssize_t) atol(value); if (LocaleCompare(keyword,"RECORD_BYTES") == 0) image->columns=1UL*atol(value); if (LocaleCompare(keyword,"NS") == 0) image->columns=1UL*atol(value); if (LocaleCompare(keyword,"LINES") == 0) image->rows=1UL*atol(value); if (LocaleCompare(keyword,"NL") == 0) image->rows=1UL*atol(value); } while (isspace((int) ((unsigned char) c)) != 0) { c=ReadBlobByte(image); count++; } } while (count < (ssize_t) length) { c=ReadBlobByte(image); count++; } if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); image->depth=8; if (AllocateImageColormap(image,256) == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); if (image_info->ping != MagickFalse) { (void) CloseBlob(image); return(GetFirstImageInList(image)); } /* Read VICAR pixels. */ if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } GetQuantumInfo(image_info,&quantum_info); scanline=(unsigned char *) AcquireQuantumMemory(image->columns, sizeof(*scanline)); if (scanline == (unsigned char *) NULL) ThrowReaderException(CorruptImageError,"UnableToReadImageData"); for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; count=ReadBlob(image,image->columns,scanline); (void) ExportQuantumPixels(image,&quantum_info,GrayQuantum,scanline); if (SyncImagePixels(image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } scanline=(unsigned char *) RelinquishMagickMemory(scanline); if (EOFBlob(image) != MagickFalse) ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); (void) CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e V I C A R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteVICARImage() writes an image in the VICAR rasterfile format. % Vicar files contain a text header, followed by one or more planes of binary % grayscale image data. Vicar files are designed to allow many planes to be % stacked together to form image cubes. This method only writes a single % grayscale plane. % % WriteVICARImage was written contributed by % [email protected]. % % The format of the WriteVICARImage method is: % % MagickBooleanType WriteVICARImage(const ImageInfo *image_info, % Image *image) % % A description of each parameter follows. % % o image_info: the image info. % % o image: The image. % % */ static MagickBooleanType WriteVICARImage(const ImageInfo *image_info, Image *image) { char header[MaxTextExtent]; int y; MagickBooleanType status; QuantumInfo quantum_info; register const PixelPacket *p; unsigned char *scanline; /* 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(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); if (image_info->colorspace == UndefinedColorspace) (void) SetImageColorspace(image,RGBColorspace); /* Write header. */ (void) ResetMagickMemory(header,' ',MaxTextExtent); (void) FormatMagickString(header,MaxTextExtent, "LBLSIZE=%lu FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 " "RECSIZE=%lu ORG='BSQ' NL=%lu NS=%lu NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 " "NLB=0 TASK='ImageMagick'",(unsigned long) MaxTextExtent,image->columns, image->rows,image->columns); (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header); /* Allocate memory for scanline. */ scanline=(unsigned char *) AcquireQuantumMemory(image->columns, sizeof(*scanline)); if (scanline == (unsigned char *) NULL) ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); /* Write VICAR scanline. */ GetQuantumInfo(image_info,&quantum_info); image->depth=8; for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; (void) ImportQuantumPixels(image,&quantum_info,GrayQuantum,scanline); (void) WriteBlob(image,image->columns,scanline); if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } scanline=(unsigned char *) RelinquishMagickMemory(scanline); (void) CloseBlob(image); return(MagickTrue); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % P i x e l I t e r a t e M o n o M o d i f y % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % PixelIterateMonoModify() iterates through a region of an image and invokes % a user-provided callback function (of type PixelIteratorMonoReadCallback) % for a region of pixels. This is useful to support simple operations such as % level shifting, colorspace translation, or thresholding. % % The format of the PixelIterateMonoModify method is: % % MagickPassFail PixelIterateMonoModify( % PixelIteratorMonoModifyCallback call_back, % const PixelIteratorOptions *options, % const char *description, % void *mutable_data, % const void *immutable_data, % const long x, % const long y, % const unsigned long columns, % const unsigned long rows, % Image *image, % ExceptionInfo *exception) % % A description of each parameter follows: % % o call_back: A user-provided C callback function which is passed the % address of pixels from each image. % % o options: Pixel iterator execution options (may be NULL). % % o description: textual description of operation being performed. % % o mutable_data: User-provided mutable context data. % % o immutable_data: User-provided immutable context data. % % o x: The horizontal ordinate of the top left corner of the region. % % o y: The vertical ordinate of the top left corner of the region. % % o columns: Width of pixel region % % o rows: Height of pixel region % % o image: The address of the Image. % % o exception: If an error is reported, this argument is updated with the reason. % */ MagickExport MagickPassFail PixelIterateMonoModify(PixelIteratorMonoModifyCallback call_back, const PixelIteratorOptions *options, const char *description, void *mutable_data, const void *immutable_data, const long x, const long y, const unsigned long columns, const unsigned long rows, Image *image, ExceptionInfo *exception) { MagickPassFail status = MagickPass; register long row; unsigned long row_count=0; int max_threads; max_threads=omp_get_max_threads(); (void) SetRegionThreads(max_threads,options,columns,rows); #if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(row_count, status) #endif for (row=y; row < (long) (y+rows); row++) { MagickBool thread_status; PixelPacket *pixels; IndexPacket *indexes; thread_status=status; if (thread_status == MagickFail) continue; pixels=GetImagePixelsEx(image, x, row, columns, 1, exception); if (!pixels) thread_status=MagickFail; indexes=AccessMutableIndexes(image); if (thread_status != MagickFail) thread_status=(call_back)(mutable_data,immutable_data,image,pixels,indexes,columns,exception); if (thread_status != MagickFail) if (!SyncImagePixelsEx(image,exception)) thread_status=MagickFail; #if defined(HAVE_OPENMP) # pragma omp critical (GM_PixelIterateMonoModify) #endif { row_count++; if (QuantumTick(row_count,rows)) if (!MagickMonitorFormatted(row_count,rows,exception, description,image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } omp_set_num_threads(max_threads); return (status); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e J P 2 I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method WriteJP2Image writes an image in the JPEG 2000 image format. % % JP2 support originally written by Nathan Brown, [email protected] % % The format of the WriteJP2Image method is: % % MagickPassFail WriteJP2Image(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o status: Method WriteJP2Image return MagickTrue if the image is written. % MagickFalse is returned is there is a memory shortage or if the image file % fails to write. % % o image_info: Specifies a pointer to a ImageInfo structure. % % o image: A pointer to an Image structure. % % */ static MagickPassFail WriteJP2Image(const ImageInfo *image_info,Image *image) { char magick[MaxTextExtent], option_keyval[MaxTextExtent], *options = NULL; int format; long y; jas_image_cmptparm_t component_info; jas_image_t *jp2_image; jas_matrix_t *jp2_pixels; jas_stream_t *jp2_stream; register const PixelPacket *p; register int x; unsigned int rate_specified=False, status; int component, number_components; unsigned short *lut; ImageCharacteristics characteristics; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == False) ThrowWriterException(FileOpenError,UnableToOpenFile,image); /* Ensure that image is in RGB space. */ (void) TransformColorspace(image,RGBColorspace); /* Analyze image to be written. */ if (!GetImageCharacteristics(image,&characteristics, (OptimizeType == image_info->type), &image->exception)) { CloseBlob(image); return MagickFail; } /* Obtain a JP2 stream. */ jp2_stream=JP2StreamManager(image); if (jp2_stream == (jas_stream_t *) NULL) ThrowWriterException(DelegateError,UnableToManageJP2Stream,image); number_components=image->matte ? 4 : 3; if ((image_info->type != TrueColorType) && (characteristics.grayscale)) number_components=1; jp2_image=jas_image_create0(); if (jp2_image == (jas_image_t *) NULL) ThrowWriterException(DelegateError,UnableToCreateImage,image); for (component=0; component < number_components; component++) { (void) memset((void *)&component_info,0,sizeof(jas_image_cmptparm_t)); component_info.tlx=0; /* top left x ordinate */ component_info.tly=0; /* top left y ordinate */ component_info.hstep=1; /* horizontal pixels per step */ component_info.vstep=1; /* vertical pixels per step */ component_info.width=(unsigned int) image->columns; component_info.height=(unsigned int) image->rows; component_info.prec=(unsigned int) Max(2,Min(image->depth,16)); /* bits in range */ component_info.sgnd = false; /* range is signed value? */ if (jas_image_addcmpt(jp2_image, component,&component_info)) { jas_image_destroy(jp2_image); ThrowWriterException(DelegateError,UnableToCreateImageComponent,image); } } /* Allocate and compute LUT. */ { unsigned long i, max_value; double scale_to_component; lut=MagickAllocateArray(unsigned short *,MaxMap+1,sizeof(*lut)); if (lut == (unsigned short *) NULL) { jas_image_destroy(jp2_image); ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image); } max_value=MaxValueGivenBits(component_info.prec); scale_to_component=max_value/MaxRGBDouble; for(i=0; i <= MaxMap; i++) lut[i]=scale_to_component*i+0.5; } if (number_components == 1) { /* FIXME: If image has an attached ICC profile, then the profile should be transferred and the image colorspace set to JAS_CLRSPC_GENGRAY */ /* sRGB Grayscale */ (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting SGRAY colorspace"); jas_image_setclrspc(jp2_image, JAS_CLRSPC_SGRAY); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting GRAY channel to channel 0"); jas_image_setcmpttype(jp2_image,0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); } else { /* FIXME: If image has an attached ICC profile, then the profile should be transferred and the image colorspace set to JAS_CLRSPC_GENRGB */ /* sRGB */ (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting SRGB colorspace"); jas_image_setclrspc(jp2_image, JAS_CLRSPC_SRGB); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting RED channel to channel 0"); jas_image_setcmpttype(jp2_image,0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting GREEN channel to channel 1"); jas_image_setcmpttype(jp2_image,1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting BLUE channel to channel 2"); jas_image_setcmpttype(jp2_image,2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); if (number_components == 4 ) { (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Setting OPACITY channel to channel 3"); jas_image_setcmpttype(jp2_image,3, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_OPACITY)); } } /* Convert to JPEG 2000 pixels. */ jp2_pixels=jas_matrix_create(1,(unsigned int) image->columns); if (jp2_pixels == (jas_matrix_t *) NULL) { MagickFreeMemory(lut); jas_image_destroy(jp2_image); ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,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; if (number_components == 1) { for (x=0; x < (long) image->columns; x++) jas_matrix_setv(jp2_pixels,x,lut[ScaleQuantumToMap(PixelIntensityToQuantum(&p[x]))]); (void) jas_image_writecmpt(jp2_image,0,0,(unsigned int) y, (unsigned int) image->columns,1,jp2_pixels); } else { for (x=0; x < (long) image->columns; x++) jas_matrix_setv(jp2_pixels,x,lut[ScaleQuantumToMap(p[x].red)]); (void) jas_image_writecmpt(jp2_image,0,0,(unsigned int) y, (unsigned int) image->columns,1,jp2_pixels); for (x=0; x < (long) image->columns; x++) jas_matrix_setv(jp2_pixels,x,lut[ScaleQuantumToMap(p[x].green)]); (void) jas_image_writecmpt(jp2_image,1,0,(unsigned int) y, (unsigned int) image->columns,1,jp2_pixels); for (x=0; x < (long) image->columns; x++) jas_matrix_setv(jp2_pixels,x,lut[ScaleQuantumToMap(p[x].blue)]); (void) jas_image_writecmpt(jp2_image,2,0,(unsigned int) y, (unsigned int) image->columns,1,jp2_pixels); if (number_components > 3) for (x=0; x < (long) image->columns; x++) jas_matrix_setv(jp2_pixels,x,lut[ScaleQuantumToMap(MaxRGB-p[x].opacity)]); (void) jas_image_writecmpt(jp2_image,3,0,(unsigned int) y, (unsigned int) image->columns,1,jp2_pixels); } if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,&image->exception, SaveImageText,image->filename, image->columns,image->rows)) break; } (void) strlcpy(magick,image_info->magick,MaxTextExtent); /* J2C is an alias for JPC but Jasper only supports "JPC". */ if (LocaleCompare(magick,"j2c") == 0) (void) strlcpy(magick,"jpc",sizeof(magick)); LocaleLower(magick); format=jas_image_strtofmt(magick); /* Support passing Jasper options. */ { const char **option_name; static const char *jasper_options[] = { "imgareatlx", "imgareatly", "tilegrdtlx", "tilegrdtly", "tilewidth", "tileheight", "prcwidth", "prcheight", "cblkwidth", "cblkheight", "mode", "ilyrrates", "prg", "nomct", "numrlvls", "sop", "eph", "lazy", "rate", "termall", "segsym", "vcausal", "pterm", "resetprob", "numgbits", NULL }; for (option_name = jasper_options; *option_name != NULL; option_name++) { const char *value; if ((value=AccessDefinition(image_info,"jp2",*option_name)) != NULL) { if(LocaleCompare(*option_name,"rate") == 0) rate_specified=True; FormatString(option_keyval,"%s=%.1024s ",*option_name,value); ConcatenateString(&options,option_keyval); } } } /* Provide an emulation of IJG JPEG "quality" by default. */ if (rate_specified == False) { double rate=1.0; /* A rough approximation to JPEG v1 quality using JPEG-2000. Default "quality" 75 results in a request for 16:1 compression, which results in image sizes approximating that of JPEG v1. */ if ((image_info->quality < 99.5) && (image->rows*image->columns > 2500)) { double header_size, current_size, target_size, d; d=115-image_info->quality; /* Best number is 110-115 */ rate=100.0/(d*d); header_size=550.0; /* Base file size. */ header_size+=(number_components-1)*142; /* Additional components */ /* FIXME: Need to account for any ICC profiles here */ current_size=(double)((image->rows*image->columns*image->depth)/8)* number_components; target_size=(current_size*rate)+header_size; rate=target_size/current_size; } FormatString(option_keyval,"%s=%g ","rate",rate); ConcatenateString(&options,option_keyval); (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Compression rate: %g (%3.2f:1)",rate,1.0/rate); } if (options) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Jasper options: \"%s\"", options); (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Encoding image"); status=jas_image_encode(jp2_image,jp2_stream,format,options); (void) jas_stream_close(jp2_stream); MagickFreeMemory(options); MagickFreeMemory(lut); jas_matrix_destroy(jp2_pixels); jas_image_destroy(jp2_image); if (status) ThrowWriterException(DelegateError,UnableToEncodeImageFile,image); return(True); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % P i x e l I t e r a t e D u a l R e a d % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % PixelIterateDualRead() iterates through pixel regions of two images and % invokes a user-provided callback function (of type % PixelIteratorDualReadCallback) for each row of pixels. This is useful to % support operations such as image comparison. % % The format of the PixelIterateDualModify method is: % % MagickPassFail PixelIterateDualRead( % PixelIteratorDualReadCallback call_back, % const PixelIteratorOptions *options, % const char *description, % void *mutable_data, % const void *immutable_data, % const unsigned long columns, % const unsigned long rows, % const Image *first_image, % const long first_x, % const long first_y, % const Image *second_image, % const long second_x, % const long second_y, % ExceptionInfo *exception); % % A description of each parameter follows: % % o call_back: A user-provided C callback function which is passed the % address of pixels from each image. % % o options: Pixel iterator execution options (may be NULL). % % o description: textual description of operation being performed. % % o mutable_data: User-provided mutable context data. % % o immutable_data: User-provided immutable context data. % % o columns: Width of pixel region % % o rows: Height of pixel region % % o first_image: The address of the first Image. % % o first_x: The horizontal ordinate of the top left corner of the first region. % % o first_y: The vertical ordinate of the top left corner of the first region. % % o second_image: The address of the second Image. % % o second_x: The horizontal ordinate of the top left corner of the second region. % % o second_y: The vertical ordinate of the top left corner of the second region. % % o exception: If an error is reported, this argument is updated with the reason. % */ MagickExport MagickPassFail PixelIterateDualRead(PixelIteratorDualReadCallback call_back, const PixelIteratorOptions *options, const char *description, void *mutable_data, const void *immutable_data, const unsigned long columns, const unsigned long rows, const Image *first_image, const long first_x, const long first_y, const Image *second_image, const long second_x, const long second_y, ExceptionInfo *exception) { MagickPassFail status = MagickPass; register long row; unsigned long row_count=0; int max_threads; max_threads=omp_get_max_threads(); (void) SetRegionThreads(max_threads,options,columns,rows); #if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(row_count, status) #endif for (row=0; row < (long) rows; row++) { MagickBool thread_status; long first_row, second_row; const PixelPacket *first_pixels, *second_pixels; const IndexPacket *first_indexes, *second_indexes; thread_status=status; if (thread_status == MagickFail) continue; first_row=first_y+row; second_row=second_y+row; first_pixels=AcquireImagePixels(first_image, first_x, first_row, columns, 1, exception); if (!first_pixels) thread_status=MagickFail; first_indexes=AccessImmutableIndexes(first_image); second_pixels=AcquireImagePixels(second_image, second_x, second_row, columns, 1, exception); if (!second_pixels) thread_status=MagickFail; second_indexes=AccessImmutableIndexes(second_image); if (thread_status != MagickFail) thread_status=(call_back)(mutable_data,immutable_data, first_image,first_pixels,first_indexes, second_image,second_pixels,second_indexes, columns, exception); #if defined(HAVE_OPENMP) # pragma omp critical (GM_PixelIterateDualRead) #endif { row_count++; if (QuantumTick(row_count,rows)) if (!MagickMonitorFormatted(row_count,rows,exception, description,first_image->filename, second_image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } omp_set_num_threads(max_threads); return (status); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e X P M I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Procedure WriteXPMImage() writes an image to a file in the X pixmap format. % % The format of the WriteXPMImage method is: % % MagickBooleanType WriteXPMImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o image_info: the image info. % % o image: The image. % % */ static MagickBooleanType WriteXPMImage(const ImageInfo *image_info,Image *image) { #define MaxCixels 92 static const char Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk" "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|"; char buffer[MaxTextExtent], basename[MaxTextExtent], name[MaxTextExtent], symbol[MaxTextExtent]; long j, k, opacity, y; MagickBooleanType status; MagickPixelPacket pixel; register const IndexPacket *indexes; register const PixelPacket *p; register long i, x; unsigned long characters_per_pixel; /* 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(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); if (image->colorspace != RGBColorspace) (void) TransformImageColorspace(image,RGBColorspace); opacity=(-1); if (image->matte == MagickFalse) { if ((image->storage_class == DirectClass) || (image->colors > 256)) (void) SetImageType(image,PaletteType); } else { MagickRealType alpha, beta; /* Identify transparent colormap index. */ if ((image->storage_class == DirectClass) || (image->colors > 256)) (void) SetImageType(image,PaletteBilevelMatteType); for (i=0; i < (long) image->colors; i++) if (image->colormap[i].opacity != OpaqueOpacity) { if (opacity < 0) { opacity=i; continue; } alpha=(Quantum) TransparentOpacity-(MagickRealType) image->colormap[i].opacity; beta=(Quantum) TransparentOpacity-(MagickRealType) image->colormap[opacity].opacity; if (alpha < beta) opacity=i; } if (opacity == -1) { (void) SetImageType(image,PaletteBilevelMatteType); for (i=0; i < (long) image->colors; i++) if (image->colormap[i].opacity != OpaqueOpacity) { if (opacity < 0) { opacity=i; continue; } alpha=(Quantum) TransparentOpacity-(MagickRealType) image->colormap[i].opacity; beta=(Quantum) TransparentOpacity-(MagickRealType) image->colormap[opacity].opacity; if (alpha < beta) opacity=i; } } if (opacity >= 0) { image->colormap[opacity].red=image->transparent_color.red; image->colormap[opacity].green=image->transparent_color.green; image->colormap[opacity].blue=image->transparent_color.blue; } } /* Compute the character per pixel. */ characters_per_pixel=1; for (k=MaxCixels; (long) image->colors > k; k*=MaxCixels) characters_per_pixel++; /* XPM header. */ (void) WriteBlobString(image,"/* XPM */\n"); GetPathComponent(image->filename,BasePath,basename); if (isalnum((int) ((unsigned char) *basename)) == 0) { (void) FormatMagickString(buffer,MaxTextExtent,"xpm_%s",basename); (void) CopyMagickString(basename,buffer,MaxTextExtent); } for (i=0; basename[i] != '\0'; i++) if (isalpha((int) ((unsigned char) basename[i])) == 0) basename[i]='_'; (void) FormatMagickString(buffer,MaxTextExtent, "static char *%s[] = {\n",basename); (void) WriteBlobString(image,buffer); (void) WriteBlobString(image,"/* columns rows colors chars-per-pixel */\n"); (void) FormatMagickString(buffer,MaxTextExtent,"\"%lu %lu %lu %ld\",\n", image->columns,image->rows,image->colors,characters_per_pixel); (void) WriteBlobString(image,buffer); GetMagickPixelPacket(image,&pixel); for (i=0; i < (long) image->colors; i++) { /* Define XPM color. */ SetMagickPixelPacket(image,image->colormap+i,(IndexPacket *) NULL,&pixel); pixel.colorspace=RGBColorspace; pixel.depth=8; pixel.opacity=(MagickRealType) OpaqueOpacity; (void) QueryMagickColorname(image,&pixel,XPMCompliance,name, &image->exception); if (i == opacity) (void) CopyMagickString(name,"None",MaxTextExtent); /* Write XPM color. */ k=i % MaxCixels; symbol[0]=Cixel[k]; for (j=1; j < (long) characters_per_pixel; j++) { k=((i-k)/MaxCixels) % MaxCixels; symbol[j]=Cixel[k]; } symbol[j]='\0'; (void) FormatMagickString(buffer,MaxTextExtent,"\"%s c %s\",\n",symbol, name); (void) WriteBlobString(image,buffer); } /* Define XPM pixels. */ (void) WriteBlobString(image,"/* pixels */\n"); for (y=0; y < (long) image->rows; y++) { p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; indexes=GetVirtualIndexQueue(image); (void) WriteBlobString(image,"\""); for (x=0; x < (long) image->columns; x++) { k=((long) indexes[x] % MaxCixels); symbol[0]=Cixel[k]; for (j=1; j < (long) characters_per_pixel; j++) { k=(((int) indexes[x]-k)/MaxCixels) % MaxCixels; symbol[j]=Cixel[k]; } symbol[j]='\0'; (void) CopyMagickString(buffer,symbol,MaxTextExtent); (void) WriteBlobString(image,buffer); } (void) FormatMagickString(buffer,MaxTextExtent,"\"%s\n", (y == (long) (image->rows-1) ? "" : ",")); (void) WriteBlobString(image,buffer); if (QuantumTick(y,image->rows) != MagickFalse) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } (void) WriteBlobString(image,"};\n"); (void) CloseBlob(image); return(MagickTrue); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % P i x e l I t e r a t e T r i p l e M o d i f y % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % PixelIterateTripleModify() iterates through pixel regions of three images % and invokes a user-provided callback function (of type % PixelIteratorTripleModifyCallback) for each row of pixels. The first two % images are read-only, while the third image is read-write for update. % Access of the first two images is done lock-step using the same coordinates. % This is useful to support operations such as image differencing. % % The format of the PixelIterateTripleModify method is: % % MagickPassFail PixelIterateTripleModify( % PixelIteratorTripleModifyCallback call_back, % const PixelIteratorOptions *options, % const char *description, % void *mutable_data, % const void *immutable_data, % const unsigned long columns, % const unsigned long rows, % const Image *source1_image, % const Image *source2_image, % const long source_x, % const long source_y, % Image *update_image, % const long update_x, % const long update_y, % ExceptionInfo *exception) % % A description of each parameter follows: % % o call_back: A user-provided C callback function which reads from % a region of source pixels and updates a region of destination pixels. % % o options: Pixel iterator execution options (may be NULL). % % o description: textual description of operation being performed. % % o mutable_data: User-provided mutable context data. % % o immutable_data: User-provided immutable context data. % % o columns: Width of pixel region % % o rows: Height of pixel region % % o source1_image: The address of the constant source 1 Image. % % o source2_image: The address of the constant source 2 Image. % % o source_x: The horizontal ordinate of the top left corner of the source regions. % % o source_y: The vertical ordinate of the top left corner of the source regions. % % o update_image: The address of the update Image. % % o update_x: The horizontal ordinate of the top left corner of the update region. % % o update_y: The vertical ordinate of the top left corner of the update region. % % o exception: If an error is reported, this argument is updated with the reason. % */ static MagickPassFail PixelIterateTripleImplementation(PixelIteratorTripleModifyCallback call_back, const PixelIteratorOptions *options, const char *description, void *mutable_data, const void *immutable_data, const unsigned long columns, const unsigned long rows, const Image *source1_image, const Image *source2_image, const long source_x, const long source_y, Image *update_image, const long update_x, const long update_y, ExceptionInfo *exception, MagickBool set) { MagickPassFail status = MagickPass; register long row; unsigned long row_count=0; int max_threads; max_threads=omp_get_max_threads(); (void) SetRegionThreads(max_threads,options,columns,rows); #if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(row_count, status) #endif for (row=0; row < (long) rows; row++) { MagickBool thread_status; const PixelPacket *source1_pixels, *source2_pixels; const IndexPacket *source1_indexes, *source2_indexes; PixelPacket *update_pixels; IndexPacket *update_indexes; long source_row, update_row; thread_status=status; if (thread_status == MagickFail) continue; source_row=source_y+row; update_row=update_y+row; /* First image (read only). */ source1_pixels=AcquireImagePixels(source1_image, source_x, source_row, columns, 1, exception); if (!source1_pixels) thread_status=MagickFail; source1_indexes=AccessImmutableIndexes(source1_image); /* Second image (read only). */ source2_pixels=AcquireImagePixels(source2_image, source_x, source_row, columns, 1, exception); if (!source2_pixels) thread_status=MagickFail; source2_indexes=AccessImmutableIndexes(source2_image); /* Third image (read/write). */ if (set) update_pixels=SetImagePixelsEx(update_image, update_x, update_row, columns, 1, exception); else update_pixels=GetImagePixelsEx(update_image, update_x, update_row, columns, 1, exception); if (!update_pixels) { thread_status=MagickFail; CopyException(exception,&update_image->exception); } update_indexes=AccessMutableIndexes(update_image); if (thread_status != MagickFail) status=(call_back)(mutable_data,immutable_data, source1_image,source1_pixels,source1_indexes, source2_image,source2_pixels,source2_indexes, update_image,update_pixels,update_indexes, columns,exception); if (!SyncImagePixelsEx(update_image,exception)) thread_status=MagickFail; #if defined(HAVE_OPENMP) # pragma omp critical (GM_PixelIterateTripleImplementation) #endif { row_count++; if (QuantumTick(row_count,rows)) if (!MagickMonitorFormatted(row_count,rows,exception,description, source1_image->filename, source2_image->filename, update_image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } omp_set_num_threads(max_threads); return (status); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d R G B I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadRGBImage() reads an image of raw red, green, and blue samples and % returns it. It allocates the memory necessary for the new Image structure % and returns a pointer to the new image. % % The format of the ReadRGBImage method is: % % Image *ReadRGBImage(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 *ReadRGBImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; long y; MagickBooleanType status; MagickOffsetType offset; QuantumInfo quantum_info; register long i; register PixelPacket *q; ssize_t count; size_t packet_size; unsigned char *pixels; unsigned long width; assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AllocateImage(image_info); if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(OptionError,"MustSpecifyImageSize"); if (image_info->interlace != PartitionInterlace) { /* Open image file. */ status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } for (i=0; i < image->offset; i++) if (ReadBlobByte(image) == EOF) { ThrowFileException(exception,CorruptImageError, "UnexpectedEndOfFile",image->filename); break; } } /* Allocate memory for a pixels. */ packet_size=(size_t) ((3*image->depth+7)/8); if ((LocaleCompare(image_info->magick,"RGBA") == 0) || (LocaleCompare(image_info->magick,"RGBO") == 0)) { packet_size+=(image->depth+7)/8; image->matte=MagickTrue; } pixels=(unsigned char *) AcquireQuantumMemory(image->extract_info.width, packet_size*sizeof(*pixels)); if (pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); if (image_info->number_scenes != 0) while (image->scene < image_info->scene) { /* Skip to next image. */ image->scene++; for (y=0; y < (long) image->rows; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } } offset=(MagickOffsetType) (packet_size*image->extract_info.x); do { /* Convert raster image to pixel packets. */ GetQuantumInfo(image_info,&quantum_info); if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } switch (image_info->interlace) { case NoInterlace: default: { /* No interlacing: RGBRGBRGBRGBRGBRGB... */ for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL)) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; if (image->matte == MagickFalse) (void) ExportQuantumPixels(image,&quantum_info,RGBQuantum, pixels+offset); else if (LocaleCompare(image_info->magick,"RGBA") == 0) (void) ExportQuantumPixels(image,&quantum_info,RGBAQuantum, pixels+offset); else (void) ExportQuantumPixels(image,&quantum_info,RGBOQuantum, pixels+offset); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } width=image->extract_info.height-image->rows-image->extract_info.y; for (i=0; i < (long) width; i++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } break; } case LineInterlace: { /* Line interlacing: RRR...GGG...BBB...RRR...GGG...BBB... */ packet_size=(size_t) ((image->depth+7)/8); for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL)) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ExportQuantumPixels(image,&quantum_info,RedQuantum, pixels+offset); count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; (void) ExportQuantumPixels(image,&quantum_info,GreenQuantum, pixels+offset); count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; (void) ExportQuantumPixels(image,&quantum_info,BlueQuantum, pixels+offset); if (image->matte != MagickFalse) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; if (LocaleCompare(image_info->magick,"RGBA") == 0) (void) ExportQuantumPixels(image,&quantum_info,AlphaQuantum, pixels+offset); else (void) ExportQuantumPixels(image,&quantum_info,OpacityQuantum, pixels+offset); } if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } width=image->extract_info.height-image->rows-image->extract_info.y; for (i=0; i < (long) width; i++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } break; } case PlaneInterlace: case PartitionInterlace: { unsigned long span; /* Plane interlacing: RRRRRR...GGGGGG...BBBBBB... */ if (image_info->interlace == PartitionInterlace) { AppendImageFormat("R",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } } packet_size=(size_t) ((image->depth+7)/8); for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } i=0; span=image->rows*(image->matte != MagickFalse ? 4 : 3); for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL)) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ExportQuantumPixels(image,&quantum_info,RedQuantum, pixels+offset); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(i,span) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,i,span, image->client_data); if (status == MagickFalse) break; } i++; } width=image->extract_info.height-image->rows-image->extract_info.y; for (i=0; i < (long) width; i++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("G",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } } for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } for (y=0; y < (long) image->rows; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ExportQuantumPixels(image,&quantum_info,GreenQuantum, pixels+offset); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(i,span) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,i,span, image->client_data); if (status == MagickFalse) break; } i++; } width=image->extract_info.height-image->rows-image->extract_info.y; for (i=0; i < (long) width; i++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("B",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } } for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } for (y=0; y < (long) image->rows; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ExportQuantumPixels(image,&quantum_info,BlueQuantum, pixels+offset); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(i,span) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,i,span, image->client_data); if (status == MagickFalse) break; } i++; } width=image->extract_info.height-image->rows-image->extract_info.y; for (i=0; i < (long) width; i++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } if (image->matte != MagickFalse) { /* Read matte channel. */ if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("A",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } } for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } for (y=0; y < (long) image->rows; y++) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; if (LocaleCompare(image_info->magick,"RGBA") == 0) (void) ExportQuantumPixels(image,&quantum_info,AlphaQuantum, pixels+offset); else (void) ExportQuantumPixels(image,&quantum_info,OpacityQuantum, pixels+offset); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(i,span) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,i,span, image->client_data); if (status == MagickFalse) break; } i++; } width=image->extract_info.height-image->rows-image->extract_info.y; for (i=0; i < (long) width; i++) { count=ReadBlob(image,packet_size*image->extract_info.width, pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } } if (image_info->interlace == PartitionInterlace) (void) CopyMagickString(image->filename,image_info->filename, MaxTextExtent); break; } } if (y < (long) image->rows) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); break; } /* Proceed to next image. */ if (image_info->number_scenes != 0) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; if (image_info->interlace == PartitionInterlace) break; count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count == (ssize_t) (packet_size*image->extract_info.width)) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (GetNextImageInList(image) == (Image *) NULL) { image=DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImagesTag,TellBlob(image), GetBlobSize(image),image->client_data); if (status == MagickFalse) break; } } } while ((size_t) count == (packet_size*image->extract_info.width)); pixels=(unsigned char *) RelinquishMagickMemory(pixels); CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + I n t e g r a l R o t a t e I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % IntegralRotateImage() rotates the image an integral of 90 degrees. It % allocates the memory necessary for the new Image structure and returns a % pointer to the rotated image. % % The format of the IntegralRotateImage method is: % % Image *IntegralRotateImage(const Image *image,unsigned long rotations, % ExceptionInfo *exception) % % A description of each parameter follows. % % o image: the image. % % o rotations: Specifies the number of 90 degree rotations. % % */ static Image *IntegralRotateImage(const Image *image,unsigned long rotations, ExceptionInfo *exception) { #define TileHeight 128 #define TileWidth 128 #define RotateImageTag "Rotate/Image" Image *rotate_image; long tile_x, tile_y, y; MagickBooleanType status; MagickPixelPacket pixel; RectangleInfo page; register IndexPacket *indexes, *rotate_indexes; register const PixelPacket *p, *tile_pixels; register long x; register PixelPacket *q; unsigned long tile_width, tile_height; /* Initialize rotated image attributes. */ assert(image != (Image *) NULL); page=image->page; rotations%=4; if ((rotations == 1) || (rotations == 3)) rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue, exception); else rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue, exception); if (rotate_image == (Image *) NULL) return((Image *) NULL); /* Integral rotate the image. */ GetMagickPixelPacket(image,&pixel); switch (rotations) { case 0: { /* Rotate 0 degrees. */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,exception); q=SetImagePixels(rotate_image,0,y,rotate_image->columns,1); if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) break; indexes=GetIndexes(image); rotate_indexes=GetIndexes(rotate_image); for (x=0; x < (long) image->columns; x++) { SetMagickPixelPacket(image,p,indexes+x,&pixel); SetPixelPacket(rotate_image,&pixel,q,rotate_indexes+x); p++; q++; } if (SyncImagePixels(rotate_image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(RotateImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } break; } case 1: { /* Rotate 90 degrees. */ for (tile_y=0; tile_y < (long) image->rows; tile_y+=TileHeight) { for (tile_x=0; tile_x < (long) image->columns; tile_x+=TileWidth) { tile_width=TileWidth; if ((tile_x+TileWidth) > (long) image->columns) tile_width=1UL*(TileWidth-(tile_x+TileWidth-image->columns)); tile_height=TileHeight; if ((tile_y+TileHeight) > (long) image->rows) tile_height=1UL*(TileHeight-(tile_y+TileHeight-image->rows)); tile_pixels=AcquireImagePixels(image,tile_x,tile_y,tile_width, tile_height,exception); if (tile_pixels == (const PixelPacket *) NULL) break; for (y=0; y < (long) tile_width; y++) { q=SetImagePixels(rotate_image,(long) rotate_image->columns-(tile_y+ tile_height),tile_x+y,tile_height,1); if (q == (PixelPacket *) NULL) break; rotate_indexes=GetIndexes(rotate_image); p=tile_pixels+(tile_height-1)*tile_width+y; indexes=GetIndexes(image)+(tile_height-1)*tile_width+y; for (x=0; x < (long) tile_height; x++) { SetMagickPixelPacket(image,p,indexes,&pixel); SetPixelPacket(rotate_image,&pixel,q,rotate_indexes+x); p-=tile_width; indexes-=tile_width; q++; } if (SyncImagePixels(rotate_image) == MagickFalse) break; } } if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(tile_y,image->rows) != MagickFalse)) { status=image->progress_monitor(RotateImageTag,tile_y,image->rows, image->client_data); if (status == MagickFalse) break; } } Swap(page.width,page.height); Swap(page.x,page.y); if (page.width != 0) page.x=(long) (page.width-rotate_image->columns-page.x); break; } case 2: { /* Rotate 180 degrees. */ for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,exception); q=SetImagePixels(rotate_image,0,(long) (image->rows-y-1), image->columns,1); if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) break; q+=image->columns; indexes=GetIndexes(image); rotate_indexes=GetIndexes(rotate_image); for (x=0; x < (long) image->columns; x++) { q--; SetMagickPixelPacket(image,p,indexes+x,&pixel); SetPixelPacket(rotate_image,&pixel,q,rotate_indexes+(image->columns- x-1)); p++; } if (SyncImagePixels(rotate_image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(RotateImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } if (page.width != 0) page.x=(long) (page.width-rotate_image->columns-page.x); if (page.height != 0) page.y=(long) (page.height-rotate_image->rows-page.y); break; } case 3: { /* Rotate 270 degrees. */ for (tile_y=0; tile_y < (long) image->rows; tile_y+=TileHeight) { for (tile_x=0; tile_x < (long) image->columns; tile_x+=TileWidth) { tile_width=TileWidth; if ((tile_x+TileWidth) > (long) image->columns) tile_width=1UL*(TileWidth-(tile_x+TileWidth-image->columns)); tile_height=TileHeight; if ((tile_y+TileHeight) > (long) image->rows) tile_height=1UL*(TileHeight-(tile_y+TileHeight-image->rows)); tile_pixels=AcquireImagePixels(image,tile_x,tile_y,tile_width, tile_height,exception); if (tile_pixels == (const PixelPacket *) NULL) break; for (y=0; y < (long) tile_width; y++) { q=SetImagePixels(rotate_image,tile_y,(long) rotate_image->rows- (tile_x+tile_width)+y,tile_height,1); if (q == (PixelPacket *) NULL) break; rotate_indexes=GetIndexes(rotate_image); p=tile_pixels+(tile_width-1)-y; indexes=GetIndexes(image)+(tile_width-1)-y; for (x=0; x < (long) tile_height; x++) { SetMagickPixelPacket(image,p,indexes,&pixel); SetPixelPacket(rotate_image,&pixel,q,rotate_indexes+x); p+=tile_width; indexes+=tile_width; q++; } if (SyncImagePixels(rotate_image) == MagickFalse) break; } } if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(tile_y,image->rows) != MagickFalse)) { status=image->progress_monitor(RotateImageTag,tile_y,image->rows, image->client_data); if (status == MagickFalse) break; } } Swap(page.width,page.height); Swap(page.x,page.y); if (page.height != 0) page.y=(long) (page.height-rotate_image->rows-page.y); break; } } rotate_image->page=page; return(rotate_image); }
static MagickBooleanType WriteJBIGImage(const ImageInfo *image_info, Image *image) { double version; long y; MagickBooleanType status; MagickOffsetType scene; register const PixelPacket *p; register IndexPacket *indexes; register long x; register unsigned char *q; struct jbg_enc_state jbig_info; unsigned char bit, byte, *pixels; unsigned long number_packets; /* Open 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(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); version=strtod(JBG_VERSION,(char **) NULL); scene=0; do { /* Allocate pixel data. */ if (image_info->colorspace == UndefinedColorspace) (void) SetImageColorspace(image,RGBColorspace); number_packets=(image->columns+7)/8; pixels=(unsigned char *) AcquireQuantumMemory(number_packets, image->rows*sizeof(*pixels)); if (pixels == (unsigned char *) NULL) ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); /* Convert pixels to a bitmap. */ (void) SetImageType(image,BilevelType); q=pixels; for (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; indexes=GetIndexes(image); bit=0; byte=0; for (x=0; x < (long) image->columns; x++) { byte<<=1; if (PixelIntensity(p) < (QuantumRange/2.0)) byte|=0x01; bit++; if (bit == 8) { *q++=byte; bit=0; byte=0; } p++; } if (bit != 0) *q++=byte << (8-bit); if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(SaveImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } /* Initialize JBIG info structure. */ jbg_enc_init(&jbig_info,image->columns,image->rows,1,&pixels, (void (*)(unsigned char *,size_t,void *)) JBIGEncode,image); if (image_info->scene != 0) jbg_enc_layers(&jbig_info,(int) image_info->scene); else { long sans_offset; unsigned long x_resolution, y_resolution; x_resolution=640; y_resolution=480; sans_offset=0; if (image_info->density != (char *) NULL) { GeometryInfo geometry_info; MagickStatusType flags; flags=ParseGeometry(image_info->density,&geometry_info); x_resolution=geometry_info.rho; y_resolution=geometry_info.sigma; if ((flags & SigmaValue) == 0) y_resolution=x_resolution; } if (image->units == PixelsPerCentimeterResolution) { x_resolution*=2.54; y_resolution*=2.54; } (void) jbg_enc_lrlmax(&jbig_info,x_resolution,y_resolution); } (void) jbg_enc_lrange(&jbig_info,-1,-1); jbg_enc_options(&jbig_info,JBG_ILEAVE | JBG_SMID,JBG_TPDON | JBG_TPBON | JBG_DPON,version < 1.6 ? -1 : 0,-1,-1); /* Write JBIG image. */ jbg_enc_out(&jbig_info); jbg_enc_free(&jbig_info); pixels=(unsigned char *) RelinquishMagickMemory(pixels); if (GetNextImageInList(image) == (Image *) NULL) break; image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(SaveImagesTag,scene, GetImageListLength(image),image->client_data); if (status == MagickFalse) break; } scene++; } while (image_info->adjoin != MagickFalse); (void) CloseBlob(image); return(MagickTrue); }
static void XShearImage(Image *image,const MagickRealType degrees, const unsigned long width,const unsigned long height,const long x_offset, long y_offset) { #define XShearImageTag "XShear/Image" enum {LEFT, RIGHT} direction; IndexPacket *indexes, *shear_indexes; long step, y; MagickBooleanType status; MagickPixelPacket background, pixel, source, destination; MagickRealType area, displacement; register long i; register PixelPacket *p, *q; assert(image != (Image *) NULL); assert(image->signature == MagickSignature); if (image->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); y_offset--; for (y=0; y < (long) height; y++) { y_offset++; displacement=degrees*(MagickRealType) (y-height/2.0); if (displacement == 0.0) continue; if (displacement > 0.0) direction=RIGHT; else { displacement*=(-1.0); direction=LEFT; } step=(long) floor((double) displacement); area=(MagickRealType) (displacement-step); step++; GetMagickPixelPacket(image,&background); SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL, &background); if (image->colorspace == CMYKColorspace) ConvertRGBToCMYK(&background); pixel=background; GetMagickPixelPacket(image,&source); GetMagickPixelPacket(image,&destination); switch (direction) { case LEFT: { /* Transfer pixels left-to-right. */ if (step > x_offset) break; p=GetImagePixels(image,0,y_offset,image->columns,1); if (p == (PixelPacket *) NULL) break; p+=x_offset; indexes=GetIndexes(image); indexes+=x_offset; q=p-step; shear_indexes=indexes-step; for (i=0; i < (long) width; i++) { if ((x_offset+i) < step) { SetMagickPixelPacket(image,++p,++indexes,&pixel); q++; shear_indexes++; continue; } SetMagickPixelPacket(image,p,indexes,&source); MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&source, (MagickRealType) p->opacity,area,&destination); SetPixelPacket(image,&destination,q++,shear_indexes++); SetMagickPixelPacket(image,p++,indexes++,&pixel); } MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&background, (MagickRealType) background.opacity,area,&destination); SetPixelPacket(image,&destination,q++,shear_indexes++); for (i=0; i < (step-1); i++) SetPixelPacket(image,&background,q++,shear_indexes++); break; } case RIGHT: { /* Transfer pixels right-to-left. */ p=GetImagePixels(image,0,y_offset,image->columns,1); if (p == (PixelPacket *) NULL) break; p+=x_offset+width; indexes=GetIndexes(image); indexes+=x_offset+width; q=p+step; shear_indexes=indexes+step; for (i=0; i < (long) width; i++) { p--; indexes--; q--; shear_indexes--; if ((unsigned long) (x_offset+width+step-i) >= image->columns) continue; SetMagickPixelPacket(image,p,indexes,&source); MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&source, (MagickRealType) p->opacity,area,&destination); SetPixelPacket(image,&destination,q,shear_indexes); SetMagickPixelPacket(image,p,indexes,&pixel); } MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&background, (MagickRealType) background.opacity,area,&destination); SetPixelPacket(image,&destination,--q,--shear_indexes); for (i=0; i < (step-1); i++) SetPixelPacket(image,&background,--q,--shear_indexes); break; } } if (SyncImagePixels(image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,height) != MagickFalse)) { status=image->progress_monitor(XShearImageTag,y,height, image->client_data); if (status == MagickFalse) break; } } }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d G R A Y I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadGRAYImage() reads an image of raw grayscale samples and returns % it. It allocates the memory necessary for the new Image structure and % returns a pointer to the new image. % % The format of the ReadGRAYImage method is: % % Image *ReadGRAYImage(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 *ReadGRAYImage(const ImageInfo *image_info, ExceptionInfo *exception) { Image *image; long j, y; MagickBooleanType status; MagickOffsetType offset; QuantumInfo quantum_info; register long i; register PixelPacket *q; ssize_t count; size_t packet_size; unsigned char *pixels; unsigned long width; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AllocateImage(image_info); if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(OptionError,"MustSpecifyImageSize"); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } for (i=0; i < image->offset; i++) if (ReadBlobByte(image) == EOF) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); break; } /* Allocate memory for a pixels. */ packet_size=(size_t) (image->depth+7)/8; pixels=(unsigned char *) AcquireQuantumMemory(image->extract_info.width, packet_size*sizeof(*pixels)); if (pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); if (image_info->number_scenes != 0) while (image->scene < image_info->scene) { /* Skip to next image. */ image->scene++; for (y=0; y < (long) image->rows; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } } offset=(MagickOffsetType) (packet_size*image->extract_info.x); do { /* Convert raster image to pixel packets. */ GetQuantumInfo(image_info,&quantum_info); if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } for (y=0; y < image->extract_info.y; y++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL)) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ExportQuantumPixels(image,&quantum_info,GrayQuantum,pixels+offset); if (SyncImagePixels(image) == MagickFalse) break; if (image->previous == (Image *) NULL) if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,image->rows) != MagickFalse)) { status=image->progress_monitor(LoadImageTag,y,image->rows, image->client_data); if (status == MagickFalse) break; } } width=image->extract_info.height-image->rows-image->extract_info.y; for (j=0; j < (long) width; j++) { count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count != (ssize_t) (packet_size*image->extract_info.width)) break; } if (y < (long) image->rows) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); break; } /* Proceed to next image. */ if (image_info->number_scenes != 0) if (image->scene >= (image_info->scene+image_info->number_scenes-1)) break; count=ReadBlob(image,packet_size*image->extract_info.width,pixels); if (count == (ssize_t) (packet_size*image->extract_info.width)) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (GetNextImageInList(image) == (Image *) NULL) { image=DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); if (image->progress_monitor != (MagickProgressMonitor) NULL) { status=image->progress_monitor(LoadImagesTag,TellBlob(image), GetBlobSize(image),image->client_data); if (status == MagickFalse) break; } } } while (count == (ssize_t) (packet_size*image->extract_info.width)); pixels=(unsigned char *) RelinquishMagickMemory(pixels); CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + Y S h e a r I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % YShearImage shears the image in the Y direction with a shear angle of % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and % negative angles shear clockwise. Angles are measured relative to a % horizontal X-axis. Y shears will increase the height of an image creating % 'empty' triangles on the top and bottom of the source image. % % The format of the YShearImage method is: % % void YShearImage(Image *image,const MagickRealType degrees, % const unsigned long width,const unsigned long height,long x_offset, % const long y_offset) % % A description of each parameter follows. % % o image: the image. % % o degrees: A MagickRealType representing the shearing angle along the Y % axis. % % o width, height, x_offset, y_offset: Defines a region of the image % to shear. % */ static void YShearImage(Image *image,const MagickRealType degrees, const unsigned long width,const unsigned long height,long x_offset, const long y_offset) { #define YShearImageTag "YShear/Image" enum {UP, DOWN} direction; IndexPacket *indexes, *shear_indexes; long step, y; MagickBooleanType status; MagickPixelPacket background, pixel, source, destination; MagickRealType area, displacement; register PixelPacket *p, *q; register long i; assert(image != (Image *) NULL); x_offset--; for (y=0; y < (long) width; y++) { x_offset++; displacement=degrees*(MagickRealType) (y-width/2.0); if (displacement == 0.0) continue; if (displacement > 0.0) direction=DOWN; else { displacement*=(-1.0); direction=UP; } step=(long) floor((double) displacement); area=(MagickRealType) (displacement-step); step++; GetMagickPixelPacket(image,&background); SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL, &background); if (image->colorspace == CMYKColorspace) ConvertRGBToCMYK(&background); pixel=background; GetMagickPixelPacket(image,&source); GetMagickPixelPacket(image,&destination); switch (direction) { case UP: { /* Transfer pixels top-to-bottom. */ if (step > y_offset) break; p=GetImagePixels(image,x_offset,0,1,image->rows); if (p == (PixelPacket *) NULL) break; p+=y_offset; indexes=GetIndexes(image); indexes+=y_offset; q=p-step; shear_indexes=indexes-step; for (i=0; i < (long) height; i++) { if ((y_offset+i) < step) { SetMagickPixelPacket(image,++p,++indexes,&pixel); q++; shear_indexes++; continue; } SetMagickPixelPacket(image,p,indexes,&source); MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&source, (MagickRealType) p->opacity,area,&destination); SetPixelPacket(image,&destination,q++,shear_indexes++); SetMagickPixelPacket(image,p++,indexes++,&pixel); } MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&background, (MagickRealType) background.opacity,area,&destination); SetPixelPacket(image,&destination,q++,shear_indexes++); for (i=0; i < (step-1); i++) SetPixelPacket(image,&background,q++,shear_indexes++); break; } case DOWN: { /* Transfer pixels bottom-to-top. */ p=GetImagePixels(image,x_offset,0,1,image->rows); if (p == (PixelPacket *) NULL) break; p+=y_offset+height; indexes=GetIndexes(image); indexes+=y_offset+height; q=p+step; shear_indexes=indexes+step; for (i=0; i < (long) height; i++) { p--; indexes--; q--; shear_indexes--; if ((unsigned long) (y_offset+height+step-i) >= image->rows) continue; SetMagickPixelPacket(image,p,indexes,&source); MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&source, (MagickRealType) p->opacity,area,&destination); SetPixelPacket(image,&destination,q,shear_indexes); SetMagickPixelPacket(image,p,indexes,&pixel); } MagickCompositeBlend(&pixel,(MagickRealType) pixel.opacity,&background, (MagickRealType) background.opacity,area,&destination); SetPixelPacket(image,&destination,--q,--shear_indexes); for (i=0; i < (step-1); i++) SetPixelPacket(image,&background,--q,--shear_indexes); break; } } if (SyncImagePixels(image) == MagickFalse) break; if ((image->progress_monitor != (MagickProgressMonitor) NULL) && (QuantumTick(y,width) != MagickFalse)) { status=image->progress_monitor(XShearImageTag,y,width, image->client_data); if (status == MagickFalse) break; } } }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d C M Y K I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadCMYKImage reads an image of raw cyan, magenta, yellow, and black % samples and returns it. It allocates the memory necessary for the new % Image structure and returns a pointer to the new image. % % The format of the ReadCMYKImage method is: % % Image *ReadCMYKImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadCMYKImage returns a pointer to the image after % reading. A null image is returned if there is a memory shortage or % if the image cannot be read. % % o image_info: Specifies a pointer to a ImageInfo structure. % % o exception: return any errors or warnings in this structure. % % */ static Image *ReadCMYKImage(const ImageInfo *image_info, ExceptionInfo *exception) { Image *image; long y; register long i, x; register PixelPacket *q; size_t count; unsigned char *scanline; unsigned int status; unsigned int packet_size, quantum_size; ImportPixelAreaOptions import_options; assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AllocateImage(image_info); if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(OptionError,MustSpecifyImageSize,image); if (image_info->interlace != PartitionInterlace) { /* Open image file. */ status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); for (i=0; i < image->offset; i++) { if (EOF == ReadBlobByte(image)) ThrowException(exception,CorruptImageError,UnexpectedEndOfFile, image->filename); } } if (image->logging) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Tile %lux%lu%+ld%+ld", image->tile_info.width,image->tile_info.height, image->tile_info.x,image->tile_info.y); /* Allocate memory for a scanline. */ if (image->depth <= 8) quantum_size=8; else if (image->depth <= 16) quantum_size=16; else quantum_size=32; packet_size=(quantum_size*4)/8; if (LocaleCompare(image_info->magick,"CMYKA") == 0) { image->matte=True; packet_size=(quantum_size*5)/8; } scanline=MagickAllocateArray(unsigned char *, packet_size,image->tile_info.width); if (scanline == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); /* Initialize import options. */ ImportPixelAreaOptionsInit(&import_options); if (image_info->endian != UndefinedEndian) import_options.endian=image_info->endian; if (image->logging) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "Depth %u bits, Endian %s, Interlace %s", quantum_size, EndianTypeToString(import_options.endian), InterlaceTypeToString(image_info->interlace)); /* Support starting at intermediate image frame. */ if (image_info->subrange != 0) while (image->scene < image_info->subimage) { /* Skip to next image. */ image->scene++; for (y=0; y < (long) image->rows; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); } x=(long) (packet_size*image->tile_info.x); do { /* Convert raster image to pixel packets. */ image->colorspace=CMYKColorspace; if (image_info->ping && (image_info->subrange != 0)) if (image->scene >= (image_info->subimage+image_info->subrange-1)) break; switch (image_info->interlace) { case NoInterlace: default: { /* No interlacing: CMYKCMYKCMYKCMYKCMYKCMYK... */ for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (image->previous == (Image *) NULL)) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; if (!image->matte) (void) ImportImagePixelArea(image,CMYKQuantum,quantum_size,scanline+x, &import_options,0); else (void) ImportImagePixelArea(image,CMYKAQuantum,quantum_size,scanline+x, &import_options,0); if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,exception, LoadImageText,image->filename, image->columns,image->rows)) break; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); break; } case LineInterlace: { /* Line interlacing: CCC...MMM...YYY...KKK...CCC...MMM...YYY...KKK... */ packet_size=(quantum_size)/8; for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (image->previous == (Image *) NULL)) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ImportImagePixelArea(image,CyanQuantum,quantum_size,scanline+x, &import_options,0); (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); (void) ImportImagePixelArea(image,MagentaQuantum,quantum_size,scanline+x, &import_options,0); (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); (void) ImportImagePixelArea(image,YellowQuantum,quantum_size,scanline+x, &import_options,0); (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); (void) ImportImagePixelArea(image,BlackQuantum,quantum_size,scanline+x, &import_options,0); if (image->matte) { (void) ReadBlob(image,packet_size*image->tile_info.width, scanline); (void) ImportImagePixelArea(image,AlphaQuantum,quantum_size,scanline+x, &import_options,0); } if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,exception, LoadImageText,image->filename, image->columns,image->rows)) break; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); break; } case PlaneInterlace: case PartitionInterlace: { unsigned long span; /* Plane interlacing: CCCCCC...MMMMMM...YYYYYY...KKKKKK... */ if (image_info->interlace == PartitionInterlace) { AppendImageFormat("C",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); } packet_size=(quantum_size)/8; for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); i=0; span=image->rows*(image->matte ? 5 : 4); for (y=0; y < (long) image->rows; y++) { if ((y > 0) || (image->previous == (Image *) NULL)) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ImportImagePixelArea(image,CyanQuantum,quantum_size,scanline+x, &import_options,0); if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(i,span)) if (!MagickMonitorFormatted(i,span,&image->exception, LoadImageText,image->filename, image->columns,image->rows)) break; i++; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("M",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); } for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); for (y=0; y < (long) image->rows; y++) { (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ImportImagePixelArea(image,MagentaQuantum,quantum_size,scanline+x, &import_options,0); if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(i,span)) if (!MagickMonitorFormatted(i,span,&image->exception, LoadImageText,image->filename, image->columns,image->rows)) break; i++; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("Y",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); } for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); for (y=0; y < (long) image->rows; y++) { (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ImportImagePixelArea(image,YellowQuantum,quantum_size,scanline+x, &import_options,0); if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(i,span)) if (!MagickMonitorFormatted(i,span,&image->exception, LoadImageText,image->filename, image->columns,image->rows)) break; i++; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("K",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); } for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); for (y=0; y < (long) image->rows; y++) { (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ImportImagePixelArea(image,BlackQuantum,quantum_size,scanline+x, &import_options,0); if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(i,span)) if (!MagickMonitorFormatted(i,span,&image->exception, LoadImageText,image->filename, image->columns,image->rows)) break; i++; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width,scanline); if (image->matte) { /* Read matte channel. */ if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("A",image->filename); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); } for (y=0; y < image->tile_info.y; y++) (void) ReadBlob(image,packet_size*image->tile_info.width, scanline); for (y=0; y < (long) image->rows; y++) { (void) ReadBlob(image,packet_size*image->tile_info.width, scanline); q=GetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; (void) ImportImagePixelArea(image,AlphaQuantum,quantum_size,scanline+x, &import_options,0); if (!SyncImagePixels(image)) break; if (image->previous == (Image *) NULL) if (QuantumTick(i,span)) if (!MagickMonitorFormatted(i,span,&image->exception, LoadImageText,image->filename, image->columns,image->rows)) break; i++; } count=image->tile_info.height-image->rows-image->tile_info.y; for (i=0; i < (long) count; i++) (void) ReadBlob(image,packet_size*image->tile_info.width, scanline); } if (image_info->interlace == PartitionInterlace) (void) strlcpy(image->filename,image_info->filename,MaxTextExtent); break; } } if (EOFBlob(image)) { ThrowException(exception,CorruptImageError,UnexpectedEndOfFile, image->filename); break; } /* Proceed to next image. */ if (image_info->subrange != 0) if (image->scene >= (image_info->subimage+image_info->subrange-1)) break; if (image_info->interlace == PartitionInterlace) break; count=ReadBlob(image,packet_size*image->tile_info.width,scanline); if (count != 0) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (image->next == (Image *) NULL) { DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); status=MagickMonitorFormatted(TellBlob(image),GetBlobSize(image), exception,LoadImagesText, image->filename); if (status == False) break; } } while (count != 0); MagickFreeMemory(scanline); while (image->previous != (Image *) NULL) image=image->previous; CloseBlob(image); return(image); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e Y U V I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method WriteYUVImage writes an image to a file in the digital YUV % (CCIR 601 4:1:1) format. % % The format of the WriteYUVImage method is: % % unsigned int WriteYUVImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o status: Method WriteYUVImage return True if the image is written. % False is returned is there is a memory shortage or if the image file % fails to write. % % o image_info: Specifies a pointer to an ImageInfo structure. % % o image: A pointer to a Image structure. % % */ Export unsigned int WriteYUVImage(const ImageInfo *image_info,Image *image) { Image *chroma_image, *yuv_image; int y; register int x; register PixelPacket *p; unsigned int height, scene, status, width; if (image_info->interlace != PartitionInterlace) { /* Open output image file. */ status=OpenBlob(image_info,image,WriteBinaryType); if (status == False) WriterExit(FileOpenWarning,"Unable to open file",image); } if (image_info->interlace == PartitionInterlace) { AppendImageFormat("Y",image->filename); status=OpenBlob(image_info,image,WriteBinaryType); if (status == False) WriterExit(FileOpenWarning,"Unable to open file",image); } scene=0; do { /* Sample image to an even width and height. */ TransformRGBImage(image,RGBColorspace); width=image->columns+(image->columns & 0x01); height=image->rows+(image->rows & 0x01); image->orphan=True; yuv_image=SampleImage(image,width,height); if (yuv_image == (Image *) NULL) WriterExit(ResourceLimitWarning,"Unable to zoom image",image); RGBTransformImage(yuv_image,YCbCrColorspace); /* Initialize Y channel. */ for (y=0; y < (int) yuv_image->rows; y++) { p=GetPixelCache(yuv_image,0,y,yuv_image->columns,1); if (p == (PixelPacket *) NULL) break; for (x=0; x < (int) yuv_image->columns; x++) { (void) WriteByte(image,DownScale(p->red)); p++; } if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) ProgressMonitor(SaveImageText,y,image->rows); } DestroyImage(yuv_image); /* Downsample image. */ image->orphan=True; chroma_image=SampleImage(image,width/2,height/2); if (chroma_image == (Image *) NULL) WriterExit(ResourceLimitWarning,"Unable to zoom image",image); RGBTransformImage(chroma_image,YCbCrColorspace); /* Initialize U channel. */ if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("U",image->filename); status=OpenBlob(image_info,image,WriteBinaryType); if (status == False) WriterExit(FileOpenWarning,"Unable to open file",image); } for (y=0; y < (int) chroma_image->rows; y++) { p=GetPixelCache(chroma_image,0,y,chroma_image->columns,1); if (p == (PixelPacket *) NULL) break; for (x=0; x < (int) chroma_image->columns; x++) { (void) WriteByte(image,DownScale(p->green)); p++; } } /* Initialize V channel. */ if (image_info->interlace == PartitionInterlace) { CloseBlob(image); AppendImageFormat("V",image->filename); status=OpenBlob(image_info,image,WriteBinaryType); if (status == False) WriterExit(FileOpenWarning,"Unable to open file",image); } for (y=0; y < (int) chroma_image->rows; y++) { p=GetPixelCache(chroma_image,0,y,chroma_image->columns,1); if (p == (PixelPacket *) NULL) break; for (x=0; x < (int) chroma_image->columns; x++) { (void) WriteByte(image,DownScale(p->blue)); p++; } } DestroyImage(chroma_image); if (image_info->interlace == PartitionInterlace) (void) strcpy(image->filename,image_info->filename); if (image->next == (Image *) NULL) break; image=GetNextImage(image); ProgressMonitor(SaveImagesText,scene++,GetNumberScenes(image)); } while (image_info->adjoin); if (image_info->adjoin) while (image->previous != (Image *) NULL) image=image->previous; CloseBlob(image); return(True); }