/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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); }
//Фильтр "Гамма-коррекция" //Параметры: // hDC DC назначения // lW Ширина DC // lH Высота DC // dblGamma Степень гаммы. Допустимые значения: от 0.1 до 1.9 // pRC Указатель на структуру RECT, определяющую область изображения для изменения // hWndCallback Окно уведомлений о ходе работы (опционально) //Возвращаемое значение: TRUE в случае успеха, FALSE в случае ошибки BOOL GammaCorrection(HDC hDC, ULONG lW, ULONG lH, double dblGamma, LPRECT pRC, HWND hWndCallback) { LPBYTE pPixels = NULL; ULONG lBytesCnt = 0; LPBITMAPINFO pBMI = NULL; ULONG lColor, lR, lG, lB; LONG i, j; volatile ONPROGRESSPARAMS ONPP = {}; if (!GetImagePixels(hDC, lW, lH, &pPixels, &lBytesCnt, &pBMI)) { if (pPixels) delete[] pPixels; if (pBMI) delete pBMI; return FALSE; } for (j = pRC->top; j < pRC->bottom; j++) { for (i = pRC->left; i < pRC->right; i++) { lColor = GetPixel(pPixels, pBMI, i, j); lR = R_BGRA(lColor); lG = G_BGRA(lColor); lB = B_BGRA(lColor); lR = (ULONG)CheckBounds((LONG)((255.0 * pow((double)lR / 255.0, 1.0 / dblGamma)) + 0.5), (LONG)0, (LONG)255); lG = (ULONG)CheckBounds((LONG)((255.0 * pow((double)lG / 255.0, 1.0 / dblGamma)) + 0.5), (LONG)0, (LONG)255); lB = (ULONG)CheckBounds((LONG)((255.0 * pow((double)lB / 255.0, 1.0 / dblGamma)) + 0.5), (LONG)0, (LONG)255); SetPixel(pPixels, pBMI, i, j, BGR(lB, lG, lR)); } if (hWndCallback) { ONPP.dwPercents = (DWORD)(((double)j / (double)pRC->bottom) * 100); SendMessage(hWndCallback, WM_GRAPHICSEVENT, MAKEWPARAM(EVENT_ON_PROGRESS, 0), (LPARAM)&ONPP); } } SetImagePixels(hDC, lW, lH, pPixels, pBMI); delete[] pPixels; delete pBMI; return TRUE; }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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)); }
static Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception) { /* Declare variables */ Image *image; MagickBooleanType status; long y,c; register PixelPacket *q; unsigned char magick[12], *pixels; char buff[80]; ssize_t count; unsigned long t_count=0; size_t length; IPLInfo ipl_info; QuantumInfo *quantum_info; /* Open Image */ 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 IPL image */ /* Set default resolution */ image->x_resolution=1; image->y_resolution=1; /* Determine endianness If we get back "iiii", we have LSB,"mmmm", MSB */ count=ReadBlob(image,4,magick); if((LocaleNCompare((char *) magick,"iiii",4) == 0)) image->endian=LSBEndian; else{ if((LocaleNCompare((char *) magick,"mmmm",4) == 0)) image->endian=MSBEndian; else{ ThrowReaderException(CorruptImageError, "ImproperImageHeader"); } } /* Skip o'er the next 8 bytes (garbage) */ count=ReadBlob(image, 8, magick); /* Excellent, now we read the header unimpeded. */ count=ReadBlob(image,4,magick); if((LocaleNCompare((char *) magick,"data",4) != 0)) ThrowReaderException(CorruptImageError, "ImproperImageHeader"); ipl_info.size=ReadBlobLong(image); ipl_info.width=ReadBlobLong(image); ipl_info.height=ReadBlobLong(image); if((ipl_info.width == ~0UL) || (ipl_info.height == ~0UL)) ThrowReaderException(CorruptImageError, "ImproperImageHeader"); ipl_info.colors=ReadBlobLong(image); if(ipl_info.colors == 3){ image->colorspace=RGBColorspace;} else { image->colorspace = GRAYColorspace; } ipl_info.z=ReadBlobLong(image); ipl_info.time=ReadBlobLong(image); ipl_info.byteType=ReadBlobLong(image); quantum_info = AcquireQuantumInfo(image_info); GetQuantumInfo(image_info, quantum_info); switch (ipl_info.byteType) { case 0: ipl_info.depth=8; quantum_info->format=UnsignedQuantumFormat; quantum_info->minimum = 0; quantum_info->maximum = 255; quantum_info->scale=1.0; (void) SetImageProperty(image, "quantum:format", "UnsignedQuantumFormat"); (void) SetImageProperty(image, "quantum:minimum", "0"); (void) SetImageProperty(image, "quantum:maximum", "255"); break; case 1: ipl_info.depth=16; quantum_info->format=SignedQuantumFormat; quantum_info->minimum = -32767; quantum_info->maximum = 32767; (void) SetImageProperty(image, "quantum:format", "SignedQuantumFormat"); (void) SetImageProperty(image, "quantum:minimum", "-32767"); (void) SetImageProperty(image, "quantum:maximum", "32767"); break; case 2: ipl_info.depth=16; quantum_info->format=UnsignedQuantumFormat; quantum_info->minimum = 0; quantum_info->maximum = 65535; (void) SetImageProperty(image, "quantum:format", "UnsignedQuantumFormat"); (void) SetImageProperty(image, "quantum:minimum", "0"); (void) SetImageProperty(image, "quantum:maximum", "65535"); break; case 3: ipl_info.depth=32; quantum_info->format=SignedQuantumFormat; quantum_info->minimum = -2147483647; quantum_info->maximum = 2147483647; (void) SetImageProperty(image, "quantum:format", "SignedQuantumFormat"); (void) SetImageProperty(image, "quantum:minimum", "-2147483647"); (void) SetImageProperty(image, "quantum:maximum", "2147483647"); break; case 4: ipl_info.depth=32; quantum_info->format=FloatingPointQuantumFormat; quantum_info->minimum = 0.0000000; quantum_info->maximum = 1.0000000; quantum_info->scale = QuantumRange; (void) SetImageProperty(image, "quantum:format", "FloatingPointQuantumFormat"); (void) SetImageProperty(image, "quantum:minimum", "0.0000000"); (void) SetImageProperty(image, "quantum:maximum", "1.0000000"); break; case 5: ipl_info.depth=8; (void) SetImageProperty(image, "quantum:format", "UnsignedQuantumFormat"); break; case 6: ipl_info.depth=16; (void) SetImageProperty(image, "quantum:format", "UnsignedQuantumFormat"); break; case 10: ipl_info.depth=64; quantum_info->format=FloatingPointQuantumFormat; quantum_info->minimum = 0.0000000; quantum_info->maximum = 1.0000000; quantum_info->scale = 1.000000/QuantumRange; (void) SetImageProperty(image, "quantum:format", "FloatingPointQuantumFormat"); (void) SetImageProperty(image, "quantum:minimum", "0.0000000"); (void) SetImageProperty(image, "quantum:maximum", "1.0000000"); break; default: ipl_info.depth=16; quantum_info->format=UnsignedQuantumFormat; quantum_info->minimum = 0; quantum_info->maximum = 65535; /* (void) SetImageProperty(image, "quantum:format", "UnsignedQuantumFormat");*/ break; } /* Set number of scenes of image */ (void) FormatMagickString(buff, MaxTextExtent, "%lu", ipl_info.z * ipl_info.time ); (void) SetImageProperty(image, "number_scenes", buff); /* Thats all we need if we are pinging. */ if (image_info->ping != MagickFalse) { CloseBlob(image); return(GetFirstImageInList(image)); } image->columns=ipl_info.width; image->rows=ipl_info.height; image->depth=ipl_info.depth; if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } length=image->columns; pixels=(unsigned char *) AcquireQuantumMemory(length,(image->depth/8)* sizeof(*pixels)); if(pixels == (unsigned char *)NULL) ThrowReaderException(ResourceLimitError, "MemoryAllocationFailed"); do { /* Covert IPL binary to pixel packets */ for (c=0; c < (long) ipl_info.colors; c++){ for(y = 0; y < (long) image->rows; y++){ (void) ReadBlob(image, length, pixels); q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; if(ipl_info.colors == 1){ (void) ExportQuantumPixels(image, quantum_info, GrayQuantum, pixels); if (SyncImagePixels(image) == MagickFalse) break; /* for(x = 0; x < image->columns; x++){ for( j= 0; j < 4; j++){ printf("%2x", (unsigned int)pixels[4*x + j]); } printf("\t"); } printf("\n"); */ } else{ switch(c){ case 0: (void) ExportQuantumPixels(image, quantum_info, RedQuantum, pixels); case 1: (void) ExportQuantumPixels(image, quantum_info, GreenQuantum, pixels); default: (void) ExportQuantumPixels(image, quantum_info, BlueQuantum, pixels); } if (SyncImagePixels(image) == MagickFalse) break; } } } t_count++; if (EOFBlob(image) != MagickFalse) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); break; } if(t_count < ipl_info.z * ipl_info.time){ /* Proceed to next image. */ 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 (t_count < ipl_info.z*ipl_info.time); CloseBlob(image); pixels = (unsigned char *) RelinquishMagickMemory(pixels); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % + 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); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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)); }
static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info) { DDSColors colors; long j, y; PixelPacket *q; register long i, x; unsigned char code; unsigned short c0, c1; unsigned long bits; for (y = 0; y < (long) dds_info->height; y += 4) { for (x = 0; x < (long) dds_info->width; x += 4) { /* Get 4x4 patch of pixels to write on */ q = SetImagePixels(image, x, y, Min(4, dds_info->width - x), Min(4, dds_info->height - y)); if (q == (PixelPacket *) NULL) return MagickFalse; /* Read 8 bytes of data from the image */ c0 = ReadBlobLSBShort(image); c1 = ReadBlobLSBShort(image); bits = ReadBlobLSBLong(image); CalculateColors(c0, c1, &colors, MagickFalse); /* Write the pixels */ for (j = 0; j < 4; j++) { for (i = 0; i < 4; i++) { if ((x + i) < (long) dds_info->width && (y + j) < (long) dds_info->height) { code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3); q->red = ScaleCharToQuantum( colors.r[code] ); q->green = ScaleCharToQuantum( colors.g[code] ); q->blue = ScaleCharToQuantum( colors.b[code] ); q->opacity = ScaleCharToQuantum( colors.a[code] ); if (colors.a[code] && image->matte == MagickFalse) /* Correct matte */ image->matte = MagickTrue; q++; } } } if (SyncImagePixels(image) == MagickFalse) return MagickFalse; } }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d T X T I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadTXTImage reads a text file and returns it as an image. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadTXTImage method is: % % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadTXTImage 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 *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception) { char filename[MaxTextExtent], geometry[MaxTextExtent], *p, text[MaxTextExtent]; double dx_resolution, dy_resolution; DrawInfo *draw_info; Image *image, *texture; long count, offset; RectangleInfo page; TypeMetric metrics; unsigned int status; int logging; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter"); image=AllocateImage(image_info); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); p = ReadBlobString(image,text); status = IsTXT((unsigned char *)p,strlen(p)); if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), "File type: %d", status); if(status) { unsigned x,y; unsigned x_min,x_max,y_curr; int ch; unsigned long max,i; char NumOfPlanes; unsigned char *BImgBuff; magick_uint16_t *WImgBuff; magick_uint32_t *DImgBuff; magick_uint32_t R,G,B,A; const PixelPacket *q; ImportPixelAreaOptions import_options; (void) SeekBlob(image,0,SEEK_SET); A=0; x=0; y=0; max=0; switch(status) { case TXT_GM8B_HEX: case TXT_GM8B_HEX_Q: max=255; break; case TXT_GM16B_HEX: case TXT_GM16B_HEX_Q: max=65535; break; case TXT_GM32B_HEX: case TXT_GM32B_HEX_Q: max=65536; break; } if(!strncmp(p,"# ImageMagick pixel enumeration:",32)) { if(sscanf(p+32,"%u,%u,%u",&x_min,&y_curr,&x_max)==3) { if(strstr(p+32,",rgb")!=NULL) { x = x_min-1; y = y_curr-1; max = x_max; } if(strstr(p+32,",rgba")!=NULL) { status = IMAGEMAGICK_TXT_Q; } } } ch=0; if(x==0 && y==0) while(!EOFBlob(image)) /* auto detect sizes and num of planes */ { while(!(ch>='0' && ch<='9')) { /* go to the begin of number */ ch = ReadBlobByte(image); if(ch==EOF) goto EndReading; if(ch=='#') {readln(image,&ch); continue;} if(ch==0 || ch>128 || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) { TXT_FAIL: /* not a text data */ ThrowReaderException(CoderError,ImageTypeNotSupported,image); } } /* x,y: (R,G,B) */ i = ReadInt(image,&ch); /* x */ if(i>x) x=i; while(ch!=',') { ch = ReadBlobByte(image); if(ch==EOF) break; if(ch==10 || ch==13) goto TXT_FAIL; } ch=0; i=ReadInt(image,&ch); /* y */ if(i>y) y=i; while(ch!=':') { ch = ReadBlobByte(image); if(ch==10 || ch==13) goto TXT_FAIL; if(ch==EOF) break; } if(status!=TXT_GM8B_PLAIN2_Q) while(ch!='(') { ch = ReadBlobByte(image); if(ch==10 || ch==13) goto TXT_FAIL; if(ch==EOF) break; } ch=0; R = ReadInt(image,&ch); /* R */ if(R>max) max=R; while(ch!=',') { ch = ReadBlobByte(image); if(ch==10 || ch==13) goto TXT_FAIL; if(ch==EOF) break; } ch=0; G = ReadInt(image,&ch); /* G */ if(G>max) max=G; while(ch!=',') { ch = ReadBlobByte(image); if(ch==10 || ch==13) goto TXT_FAIL; if(ch==EOF) break; } ch=0; B = ReadInt(image,&ch); /* B */ if(B>max) max=B; if(status>16) { while(ch!=',') { ch = ReadBlobByte(image); if(ch==10 || ch==13) goto TXT_FAIL; if(ch==EOF) break; } ch=0; A = ReadInt(image,&ch); /* A */ if(A>max) max=A; } if(status!=TXT_GM8B_PLAIN2_Q) while(ch!=')') { ch = ReadBlobByte(image); if(ch==10 || ch==13) goto TXT_FAIL; if(ch==EOF) break; } readln(image,&ch); } EndReading: x_min = 1; x_max = 0; y_curr = 0; NumOfPlanes=8; /* if(max>= 2) i=2; */ /* if(max>= 4) i=4; */ /* if(max>= 16) i=8; */ if(max>= 256) NumOfPlanes=16; if(max>=65536) NumOfPlanes=32; if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), "Image detected [%u * %u]: %d", x, y, NumOfPlanes); image->depth = Min(QuantumDepth,NumOfPlanes); ImportPixelAreaOptionsInit(&import_options); import_options.endian = NativeEndian; BImgBuff = MagickAllocateMemory(unsigned char *, (size_t)(x+1) * ( ((status>16)?4:3) * NumOfPlanes/8)); WImgBuff = (magick_uint16_t *)BImgBuff; DImgBuff = (magick_uint32_t *)BImgBuff; if(BImgBuff==NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); image->columns = x+1; image->rows = y+1; (void) SeekBlob(image,0,SEEK_SET); while(!EOFBlob(image)) /* load picture data */ { x=0; while(!(ch >= '0' && ch <= '9')) { /* move to the beginning of number */ if(EOFBlob(image)) goto FINISH; ch = ReadBlobByte(image); if(ch=='#') {readln(image,&ch); continue;} } x = ReadInt(image,&ch); /* x */ while(ch!=',') { ch = ReadBlobByte(image); if(ch==EOF) break; } ch = 0; y = ReadInt(image,&ch); /* y */ while(ch!=':') { ch = ReadBlobByte(image); if(ch==EOF) break; } while(ch!='(') { ch = ReadBlobByte(image); if(ch==EOF) break; } ch=0; R = ReadInt(image,&ch); /* R */ while(ch!=',') { ch = ReadBlobByte(image); if(ch==EOF) break; } ch=0; G = ReadInt(image,&ch); /* G */ while(ch!=',') { ch = ReadBlobByte(image); if(ch==EOF) break; } ch=0; B = ReadInt(image,&ch); /* B */ if(status>16) { while(ch!=',') { ch = ReadBlobByte(image); if(ch==EOF) break; } ch=0; A = ReadInt(image,&ch); /* A */ if(A>max) max=A; } while(ch!=')') { ch = ReadBlobByte(image); if(ch==EOF) break; } /* a new line has been detected */ if(y!=y_curr) { q = SetImagePixels(image,x_min,y_curr,x_max-x_min+1,1); if (q == (PixelPacket *)NULL) break; if(status>16) (void)ImportImagePixelArea(image,RGBAQuantum,NumOfPlanes, BImgBuff + 4*x_min*(NumOfPlanes/8),&import_options,0); else (void)ImportImagePixelArea(image,RGBQuantum,NumOfPlanes, BImgBuff + 3*x_min*(NumOfPlanes/8),&import_options,0); if (!SyncImagePixels(image)) break; x_min = 1; x_max = 0; y_curr=y; } if(x<image->columns) { if(status>16) { switch(NumOfPlanes) { case 8: BImgBuff[0+4*x] = R; BImgBuff[1+4*x] = G; BImgBuff[2+4*x] = B; BImgBuff[3+4*x] = A; break; case 16:WImgBuff[0+4*x] = R; WImgBuff[1+4*x] = G; WImgBuff[2+4*x] = B; WImgBuff[3+4*x] = A; break; case 32:DImgBuff[0+4*x] = R; DImgBuff[1+4*x] = G; DImgBuff[2+4*x] = B; DImgBuff[3+4*x] = A; break; } } else { switch(NumOfPlanes) { case 8: BImgBuff[0+3*x] = R; BImgBuff[1+3*x] = G; BImgBuff[2+3*x] = B; break; case 16:WImgBuff[0+3*x] = R; WImgBuff[1+3*x] = G; WImgBuff[2+3*x] = B; break; case 32:DImgBuff[0+3*x] = R; DImgBuff[1+3*x] = G; DImgBuff[2+3*x] = B; break; } } if(x_min>x_max) x_max=x_min=x; else { if(x<x_min) x_min=x; if(x>x_max) x_max=x; } } readln(image,&ch); } FINISH: if(x_min<=x_max) { q = SetImagePixels(image,x_min,y_curr,x_max-x_min+1,1); if (q != (PixelPacket *)NULL) { if(status>16) (void)ImportImagePixelArea(image, RGBAQuantum, NumOfPlanes, BImgBuff + 4*x_min*(NumOfPlanes/8), &import_options, 0); else (void)ImportImagePixelArea(image, RGBQuantum, NumOfPlanes, BImgBuff + 3*x_min*(NumOfPlanes/8), &import_options, 0); if(!SyncImagePixels(image)) { if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), " TXT failed to sync image pixels for a row %u", y_curr); } } } MagickFreeMemory(BImgBuff); goto TXT_FINISH; } /* Set the page geometry. */ dx_resolution=72.0; dy_resolution=72.0; if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0)) { char density[MaxTextExtent]; (void) strcpy(density,PSDensityGeometry); count=GetMagickDimension(density,&image->x_resolution, &image->y_resolution,NULL,NULL); if (count != 2) image->y_resolution=image->x_resolution; } SetGeometry(image,&page); page.width=612; page.height=792; (void) GetGeometry("612x792+43+43",&page.x,&page.y,&page.width,&page.height); if (image_info->page != (char *) NULL) (void) GetGeometry(image_info->page,&page.x,&page.y,&page.width, &page.height); /* Initialize Image structure. */ image->columns=(unsigned long) ceil(((page.width*image->x_resolution)/dx_resolution)-0.5); image->rows=(unsigned long) ceil(((page.height*image->y_resolution)/dy_resolution)-0.5); texture=(Image *) NULL; if (image_info->texture != (char *) NULL) { ImageInfo *clone_info; clone_info=CloneImageInfo(image_info); clone_info->blob=(void *) NULL; clone_info->length=0; (void) strlcpy(clone_info->filename,image_info->texture,MaxTextExtent); texture=ReadImage(clone_info,exception); DestroyImageInfo(clone_info); } /* Annotate the text image. */ (void) SetImage(image,OpaqueOpacity); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); draw_info->fill=image_info->pen; (void) CloneString(&draw_info->text,image_info->filename); FormatString(geometry,"0x0%+ld%+ld",page.x,page.y); (void) CloneString(&draw_info->geometry,geometry); status=GetTypeMetrics(image,draw_info,&metrics); if (status == False) ThrowReaderException(TypeError,UnableToGetTypeMetrics,image); (void) strlcpy(filename,image_info->filename,MaxTextExtent); if (draw_info->text != '\0') *draw_info->text='\0'; for (offset=2*page.y; p != (char *) NULL; ) { /* Annotate image with text. */ (void) ConcatenateString(&draw_info->text,text); (void) ConcatenateString(&draw_info->text,"\\n"); offset+=(long) (metrics.ascent-metrics.descent); if (image->previous == (Image *) NULL) if (QuantumTick(offset,image->rows)) if (!MagickMonitorFormatted(offset,image->rows,&image->exception, LoadImageText,image->filename, image->columns,image->rows)) break; p=ReadBlobString(image,text); if ((offset < (long) image->rows) && (p != (char *) NULL)) continue; if (texture != (Image *) NULL) { MonitorHandler handler; handler=SetMonitorHandler((MonitorHandler) NULL); (void) TextureImage(image,texture); (void) SetMonitorHandler(handler); } (void) AnnotateImage(image,draw_info); if (p == (char *) NULL) break; /* Page is full-- allocate next image structure. */ *draw_info->text='\0'; offset=2*page.y; AllocateNextImage(image_info,image); if (image->next == (Image *) NULL) { DestroyImageList(image); return((Image *) NULL); } image->next->columns=image->columns; image->next->rows=image->rows; image=SyncNextImageInList(image); (void) strlcpy(image->filename,filename,MaxTextExtent); (void) SetImage(image,OpaqueOpacity); if (!MagickMonitorFormatted(TellBlob(image),GetBlobSize(image),exception, LoadImagesText,image->filename)) break; } if (texture != (Image *) NULL) { MonitorHandler handler; handler=SetMonitorHandler((MonitorHandler) NULL); (void) TextureImage(image,texture); (void) SetMonitorHandler(handler); } (void) AnnotateImage(image,draw_info); if (texture != (Image *) NULL) DestroyImage(texture); DestroyDrawInfo(draw_info); while (image->previous != (Image *) NULL) image=image->previous; TXT_FINISH: CloseBlob(image); return(image); }
static Image *ReadEMFImage(const ImageInfo *image_info, ExceptionInfo *exception) { BITMAPINFO DIBinfo; HBITMAP hBitmap, hOldBitmap; HDC hDC; HENHMETAFILE hemf; Image *image; long height, width, y; RECT rect; register long x; register PixelPacket *q; RGBQUAD *pBits, *ppBits; image=AllocateImage(image_info); hemf=ReadEnhMetaFile(image_info->filename,&width,&height); if (!hemf) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); if ((image->columns == 0) || (image->rows == 0)) { double y_resolution = 72.0, x_resolution = 72.0; if (image->y_resolution > 0) { y_resolution=image->y_resolution; if (image->units == PixelsPerCentimeterResolution) y_resolution*=CENTIMETERS_INCH; } if (image->x_resolution > 0) { x_resolution=image->x_resolution; if (image->units == PixelsPerCentimeterResolution) x_resolution*=CENTIMETERS_INCH; } image->rows=ceil((height*CENTIMETERS_INCH)/1000*y_resolution); image->columns=ceil((width*CENTIMETERS_INCH)/1000*x_resolution); } if (image_info->size != (char *) NULL) { long x; image->columns=width; image->rows=height; x=0; y=0; (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows); } if (image_info->page != (char *) NULL) { char *geometry; long sans; register char *p; /* unsigned int */ /* flags; */ geometry=GetPageGeometry(image_info->page); p=strchr(geometry,'>'); if (!p) { /*flags=*/ (void) GetMagickGeometry(geometry,&sans,&sans,&image->columns, &image->rows); if (image->x_resolution != 0.0) image->columns=(unsigned int) ((image->columns*image->x_resolution)+0.5); if (image->y_resolution != 0.0) image->rows=(unsigned int) ((image->rows*image->y_resolution)+0.5); } else { *p='\0'; /*flags=*/ (void) GetMagickGeometry(geometry,&sans,&sans,&image->columns, &image->rows); if (image->x_resolution != 0.0) image->columns=(unsigned int) (((image->columns*image->x_resolution)/72.0)+0.5); if (image->y_resolution != 0.0) image->rows=(unsigned int) (((image->rows*image->y_resolution)/72.0)+0.5); } MagickFreeMemory(geometry); } hDC=GetDC(NULL); if (!hDC) ThrowReaderException(CoderError,UnableToCreateADC,image); /* Initialize the bitmap header info. */ (void) memset(&DIBinfo,0,sizeof(BITMAPINFO)); DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); DIBinfo.bmiHeader.biWidth=image->columns; DIBinfo.bmiHeader.biHeight=(-1)*image->rows; DIBinfo.bmiHeader.biPlanes=1; DIBinfo.bmiHeader.biBitCount=32; DIBinfo.bmiHeader.biCompression=BI_RGB; hBitmap= CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,0); ReleaseDC(NULL,hDC); if (!hBitmap) ThrowReaderException(CoderError,UnableToCreateBitmap,image); hDC=CreateCompatibleDC(NULL); if (!hDC) { DeleteObject(hBitmap); ThrowReaderException(CoderError,UnableToCreateADC,image); } hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap); if (!hOldBitmap) { DeleteDC(hDC); DeleteObject(hBitmap); ThrowReaderException(CoderError,UnableToCreateBitmap,image); } /* Initialize the bitmap to the image background color. */ pBits=ppBits; for (y=0; y < (long) image->rows; y++) { for (x=0; x < (long) image->columns; x++) { pBits->rgbRed=ScaleQuantumToChar(image->background_color.red); pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green); pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue); pBits++; } } rect.top=0; rect.left=0; rect.right=image->columns; rect.bottom=image->rows; /* Convert metafile pixels. */ PlayEnhMetaFile(hDC,hemf,&rect); pBits=ppBits; 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(pBits->rgbRed); q->green=ScaleCharToQuantum(pBits->rgbGreen); q->blue=ScaleCharToQuantum(pBits->rgbBlue); q->opacity=OpaqueOpacity; pBits++; q++; } if (!SyncImagePixels(image)) break; } if (hemf) DeleteEnhMetaFile(hemf); SelectObject(hDC,hOldBitmap); DeleteDC(hDC); DeleteObject(hBitmap); return(image); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d M T V I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadMTVImage reads a MTV 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 ReadMTVImage method is: % % Image *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadMTVImage 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 *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception) { char buffer[MaxTextExtent]; Image *image; long count, y; register long x; register PixelPacket *q; register unsigned char *p; unsigned char *pixels; unsigned int status; unsigned long columns, rows; /* 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); /* Read MTV image. */ (void) ReadBlobString(image,buffer); columns=0; rows=0; count=sscanf(buffer,"%lu %lu\n",&columns,&rows); if (count != 2) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); do { size_t row_size; /* Initialize image structure. */ image->columns=columns; image->rows=rows; image->depth=8; if (image_info->ping && (image_info->subrange != 0)) if (image->scene >= (image_info->subimage+image_info->subrange-1)) break; /* Convert MTV raster image to pixel packets. */ pixels=MagickAllocateArray(unsigned char *,image->columns,3); if (pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); row_size=image->columns*3; for (y=0; y < (long) image->rows; y++) { if (ReadBlob(image,row_size,pixels) != row_size) ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image); p=pixels; 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++); q->blue=ScaleCharToQuantum(*p++); q++; } 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; } MagickFreeMemory(pixels); 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; *buffer='\0'; (void) ReadBlobString(image,buffer); count=sscanf(buffer,"%lu %lu\n",&columns,&rows); if (count == 2) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (image->next == (Image *) NULL) { DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); if (!MagickMonitorFormatted(TellBlob(image),GetBlobSize(image), exception,LoadImagesText, image->filename)) break; } } while (count == 2); while (image->previous != (Image *) NULL) image=image->previous; CloseBlob(image); return(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 RotateImageTag "Rotate/Image" Image *rotate_image; long y; MagickBooleanType status; RectangleInfo page; register IndexPacket *indexes, *rotate_indexes; register const PixelPacket *p; register long x; register PixelPacket *q; /* 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. */ 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; (void) CopyMagickMemory(q,p,(size_t) image->columns*sizeof(*q)); indexes=GetIndexes(image); rotate_indexes=GetIndexes(rotate_image); if ((indexes != (IndexPacket *) NULL) && (rotate_indexes != (IndexPacket *) NULL)) (void) CopyMagickMemory(rotate_indexes,indexes,(size_t) image->columns*sizeof(*rotate_indexes)); 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 (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,exception); q=SetImagePixels(rotate_image,(long) (image->rows-y-1),0,1, rotate_image->rows); if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) break; (void) CopyMagickMemory(q,p,(size_t) image->columns*sizeof(*q)); indexes=GetIndexes(image); rotate_indexes=GetIndexes(rotate_image); if ((indexes != (IndexPacket *) NULL) && (rotate_indexes != (IndexPacket *) NULL)) (void) CopyMagickMemory(rotate_indexes,indexes,(size_t) image->columns*sizeof(*rotate_indexes)); 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; } } 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); if ((indexes != (IndexPacket *) NULL) && (rotate_indexes != (IndexPacket *) NULL)) for (x=0; x < (long) image->columns; x++) rotate_indexes[image->columns-x-1]=indexes[x]; for (x=0; x < (long) image->columns; x++) *--q=(*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 (y=0; y < (long) image->rows; y++) { p=AcquireImagePixels(image,0,y,image->columns,1,exception); q=SetImagePixels(rotate_image,y,0,1,rotate_image->rows); if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) break; q+=image->columns; for (x=0; x < (long) image->columns; x++) *--q=(*p++); indexes=GetIndexes(image); rotate_indexes=GetIndexes(rotate_image); if ((indexes != (IndexPacket *) NULL) && (rotate_indexes != (IndexPacket *) NULL)) for (x=0; x < (long) image->columns; x++) rotate_indexes[image->columns-x-1]=indexes[x]; 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; } } 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); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d S U N I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadSUNImage reads a SUN 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 ReadSUNImage method is: % % Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadSUNImage 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 *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; int bit; long y; register IndexPacket *indexes; register long x; register PixelPacket *q; register long i; register unsigned char *p; size_t bytes_per_image, bytes_per_line, count, sun_data_length; SUNInfo sun_info; unsigned char *sun_data, *sun_pixels; unsigned int index; 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); /* Read SUN raster header. */ (void) memset(&sun_info,0,sizeof(sun_info)); sun_info.magic=ReadBlobMSBLong(image); do { /* Verify SUN identifier. */ if (sun_info.magic != 0x59a66a95) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); sun_info.width=ReadBlobMSBLong(image); sun_info.height=ReadBlobMSBLong(image); sun_info.depth=ReadBlobMSBLong(image); sun_info.length=ReadBlobMSBLong(image); sun_info.type=ReadBlobMSBLong(image); sun_info.maptype=ReadBlobMSBLong(image); sun_info.maplength=ReadBlobMSBLong(image); LogSUNInfo(&sun_info); if (EOFBlob(image)) ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image); /* Verify that header values are in positive numeric range of a 32-bit 'int' even though we store them in an unsigned value. */ if ((sun_info.magic | sun_info.width | sun_info.height | sun_info.depth | sun_info.type | sun_info.maptype | sun_info.maplength) & (1U << 31)) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); /* Verify that we support the image sub-type */ if ((sun_info.type != RT_STANDARD) && (sun_info.type != RT_ENCODED) && (sun_info.type != RT_FORMAT_RGB)) ThrowReaderException(CoderError,DataEncodingSchemeIsNotSupported,image); /* Verify that we support the colormap type */ if ((sun_info.maptype != RMT_NONE) && (sun_info.maptype != RMT_EQUAL_RGB)) ThrowReaderException(CoderError,ColormapTypeNotSupported,image); /* Insist that map length is zero if there is no colormap. */ if ((sun_info.maptype == RMT_NONE) && (sun_info.maplength != 0)) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); /* Insist on a supported depth */ if ((sun_info.depth != 1) && (sun_info.depth != 8) && (sun_info.depth != 24) && (sun_info.depth != 32)) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); image->columns=sun_info.width; image->rows=sun_info.height; if (((unsigned long) ((long) image->columns) != image->columns) || ((unsigned long) ((long) image->rows) != image->rows)) ThrowReaderException(CoderError,ImageColumnOrRowSizeIsNotSupported,image); if (CheckImagePixelLimits(image, exception) != MagickPass) ThrowReaderException(ResourceLimitError,ImagePixelLimitExceeded,image); image->depth=sun_info.depth <= 8 ? 8 : QuantumDepth; if (sun_info.depth < 24) { image->colors=sun_info.maplength; if (sun_info.maptype == RMT_NONE) image->colors=1 << sun_info.depth; if (sun_info.maptype == RMT_EQUAL_RGB) image->colors=sun_info.maplength/3; } switch (sun_info.maptype) { case RMT_NONE: { if (sun_info.depth < 24) { /* Create linear color ramp. */ if (!AllocateImageColormap(image,image->colors)) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); } break; } case RMT_EQUAL_RGB: { unsigned char *sun_colormap; /* Read SUN raster colormap. */ if (!AllocateImageColormap(image,image->colors)) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); sun_colormap=MagickAllocateMemory(unsigned char *,image->colors); if (sun_colormap == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); do { if (ReadBlob(image,image->colors,(char *) sun_colormap) != image->colors) { status = MagickFail; break; } for (i=0; i < (long) image->colors; i++) image->colormap[i].red=ScaleCharToQuantum(sun_colormap[i]); if (ReadBlob(image,image->colors,(char *) sun_colormap) != image->colors) { status = MagickFail; break; } for (i=0; i < (long) image->colors; i++) image->colormap[i].green=ScaleCharToQuantum(sun_colormap[i]); if (ReadBlob(image,image->colors,(char *) sun_colormap) != image->colors) { status = MagickFail; break; } for (i=0; i < (long) image->colors; i++) image->colormap[i].blue=ScaleCharToQuantum(sun_colormap[i]); break; } while (1); MagickFreeMemory(sun_colormap); if (MagickFail == status) ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image); break; } case RMT_RAW: { unsigned char *sun_colormap; /* Read SUN raster colormap. */ if (!AllocateImageColormap(image,image->colors)) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); sun_colormap=MagickAllocateMemory(unsigned char *,sun_info.maplength); if (sun_colormap == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); if (ReadBlob(image,sun_info.maplength,(char *) sun_colormap) != sun_info.maplength) status = MagickFail; MagickFreeMemory(sun_colormap); if (MagickFail == status) ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image); break; } default: ThrowReaderException(CoderError,ColormapTypeNotSupported,image) } image->matte=(sun_info.depth == 32); image->columns=sun_info.width; image->rows=sun_info.height; image->depth=8; if (sun_info.depth < 8) image->depth=sun_info.depth; /* Compute bytes per line and bytes per image for an unencoded image. "The width of a scan line is always 16-bits, padded when necessary." */ bytes_per_line=MagickArraySize(sun_info.width,sun_info.depth)/8; if ((bytes_per_line != 0) && (sun_info.depth == 1)) bytes_per_line += sun_info.width % 8 ? 1 : 0; if (bytes_per_line != 0) bytes_per_line=RoundUpToAlignment(bytes_per_line,2); bytes_per_image=MagickArraySize(sun_info.height,bytes_per_line); if (bytes_per_line == 0) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); if (bytes_per_image == 0) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); if ((sun_info.type == RT_STANDARD) || (sun_info.type == RT_FORMAT_RGB)) if (bytes_per_image > sun_info.length) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); if (image_info->ping) { CloseBlob(image); return(image); } if (sun_info.type == RT_ENCODED) sun_data_length=(size_t) sun_info.length; else sun_data_length=bytes_per_image; sun_data=MagickAllocateMemory(unsigned char *,sun_data_length); if (sun_data == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); if ((count=ReadBlob(image,sun_data_length,(char *) sun_data)) != sun_data_length) { MagickFreeMemory(sun_data); ThrowReaderException(CorruptImageError,UnableToReadImageData,image); } sun_pixels=sun_data; if (sun_info.type == RT_ENCODED) { /* Read run-length encoded raster pixels (padded to 16-bit boundary). */ sun_pixels=MagickAllocateMemory(unsigned char *,bytes_per_image); if (sun_pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); status &= DecodeImage(sun_data,sun_data_length,sun_pixels,bytes_per_image); MagickFreeMemory(sun_data); if (status != MagickPass) { MagickFreeMemory(sun_pixels); ThrowReaderException(CorruptImageError,UnableToRunlengthDecodeImage,image); } } /* Convert SUN raster image to pixel packets. */ p=sun_pixels; if (sun_info.depth == 1) /* Bilevel */ for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=AccessMutableIndexes(image); for (x=0; x < ((long) image->columns-7); x+=8) { for (bit=7; bit >= 0; bit--) { index=((*p) & (0x01 << bit) ? 0x01 : 0x00); indexes[x+7-bit]=index; q[x+7-bit]=image->colormap[index]; } p++; } if ((image->columns % 8) != 0) { for (bit=7; bit >= (long) (8-(image->columns % 8)); bit--) { index=((*p) & (0x01 << bit) ? 0x01 : 0x00); indexes[x+7-bit]=index; q[x+7-bit]=image->colormap[index]; } p++; } if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0) p++; 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; } else if (image->storage_class == PseudoClass) { /* Colormapped */ for (y=0; y < (long) image->rows; y++) { q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=AccessMutableIndexes(image); for (x=0; x < (long) image->columns; x++) { index=(*p++); VerifyColormapIndex(image,index); indexes[x]=index; q[x]=image->colormap[index]; } if ((image->columns % 2) != 0) p++; 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; } } else { /* (A)BGR or (A)RGB */ 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++) { if (image->matte) q->opacity=(Quantum) (MaxRGB-ScaleCharToQuantum(*p++)); if (sun_info.type == RT_STANDARD) { q->blue=ScaleCharToQuantum(*p++); q->green=ScaleCharToQuantum(*p++); q->red=ScaleCharToQuantum(*p++); } else { q->red=ScaleCharToQuantum(*p++); q->green=ScaleCharToQuantum(*p++); q->blue=ScaleCharToQuantum(*p++); } if (image->colors != 0) { q->red=image->colormap[q->red].red; q->green=image->colormap[q->green].green; q->blue=image->colormap[q->blue].blue; } q++; } if (((image->columns % 2) != 0) && (image->matte == False)) p++; 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; } } MagickFreeMemory(sun_pixels); 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; sun_info.magic=ReadBlobMSBLong(image); if (sun_info.magic == 0x59a66a95) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if (image->next == (Image *) NULL) { DestroyImageList(image); return((Image *) NULL); } image=SyncNextImageInList(image); if (!MagickMonitorFormatted(TellBlob(image),GetBlobSize(image), exception,LoadImagesText, image->filename)) break; } } while (sun_info.magic == 0x59a66a95);
static Image *ReadXPMImage(const ImageInfo *image_info,ExceptionInfo *exception) { char key[MaxTextExtent], target[MaxTextExtent], *xpm_buffer; Image *image; long j, y; MagickBooleanType active, status; register char *p, *q, *next; register IndexPacket *indexes; register long i, x; register PixelPacket *r; size_t length; SplayTreeInfo *xpm_colors; ssize_t count; 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); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } /* Read XPM file. */ length=MaxTextExtent; xpm_buffer=(char *) AcquireQuantumMemory((size_t) length,sizeof(*xpm_buffer)); p=xpm_buffer; if (xpm_buffer != (char *) NULL) while (ReadBlobString(image,p) != (char *) NULL) { if ((*p == '#') && ((p == xpm_buffer) || (*(p-1) == '\n'))) continue; if ((*p == '}') && (*(p+1) == ';')) break; p+=strlen(p); if ((size_t) (p-xpm_buffer+MaxTextExtent) < length) continue; length<<=1; xpm_buffer=(char *) ResizeQuantumMemory(xpm_buffer,length+MaxTextExtent, sizeof(*xpm_buffer)); if (xpm_buffer == (char *) NULL) break; p=xpm_buffer+strlen(xpm_buffer); } if (xpm_buffer == (char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); /* Remove comments. */ count=0; for (p=xpm_buffer; *p != '\0'; p++) { if (*p != '"') continue; count=(ssize_t) sscanf(p+1,"%lu %lu %lu %lu",&image->columns,&image->rows, &image->colors,&width); if (count == 4) break; } if ((count != 4) || (width > 10) || (image->columns == 0) || (image->rows == 0) || (image->colors == 0)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); image->depth=16; /* Remove unquoted characters. */ i=0; active=MagickFalse; q=xpm_buffer; while (*p != '\0') { if (*p++ == '"') { if (active != MagickFalse) *q++='\n'; active=active != MagickFalse ? MagickFalse : MagickTrue; } if (active != MagickFalse) *q++=(*p); } *q='\0'; /* Initialize image structure. */ xpm_colors=NewSplayTree(CompareXPMColor,RelinquishMagickMemory, (void *(*)(void *)) NULL); if (AllocateImageColormap(image,image->colors) == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); /* Read image colormap. */ i=1; next=NextXPMLine(xpm_buffer); for (j=0; (j < (long) image->colors) && (next != (char*) NULL); j++) { p=next; next=NextXPMLine(p); (void) CopyXPMColor(key,p,MagickMin((size_t) width,MaxTextExtent)); status=AddValueToSplayTree(xpm_colors,ConstantString(key),(void *) j); /* Parse color. */ (void) CopyMagickString(target,"gray",MaxTextExtent); q=ParseXPMColor(p+width); if (q != (char *) NULL) { while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0')) q++; if (next != (char *) NULL) (void) CopyXPMColor(target,q,MagickMin((size_t) (next-q), MaxTextExtent)); else (void) CopyMagickString(target,q,MaxTextExtent); q=ParseXPMColor(target); if (q != (char *) NULL) *q='\0'; } StripString(target); if (LocaleCompare(target,"none") == 0) { image->storage_class=DirectClass; image->matte=MagickTrue; } if (QueryColorDatabase(target,&image->colormap[j],exception) == MagickFalse) break; } if (j < (long) image->colors) ThrowReaderException(CorruptImageError,"CorruptImage"); j=0; if (image_info->ping == MagickFalse) { /* Read image pixels. */ if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } for (y=0; y < (long) image->rows; y++) { p=NextXPMLine(p); if (p == (char *) NULL) break; r=SetImagePixels(image,0,y,image->columns,1); if (r == (PixelPacket *) NULL) break; indexes=GetIndexes(image); for (x=0; x < (long) image->columns; x++) { (void) CopyXPMColor(key,p,(size_t) width); j=(long) GetValueFromSplayTree(xpm_colors,key); if (image->storage_class == PseudoClass) indexes[x]=(IndexPacket) j; *r=image->colormap[j]; r++; p+=width; } if (SyncImagePixels(image) == MagickFalse) break; } if (y < (long) image->rows) ThrowReaderException(CorruptImageError,"NotEnoughPixelData"); } /* Relinquish resources. */ xpm_colors=DestroySplayTree(xpm_colors); (void) CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d V I C A R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method 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: Specifies a pointer to a ImageInfo structure. % % 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, y; long count; unsigned char *scanline; unsigned int status, value_expected; unsigned long length; /* 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); /* Decode image header. */ c=ReadBlobByte(image); count=1; if (c == EOF) { 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)) { c=ReadBlobByte(image); count++; } else { register char *p; /* Determine a keyword and its value. */ p=keyword; do { if ((p-keyword) < (MaxTextExtent-1)) *p++=c; c=ReadBlobByte(image); count++; } while (isalnum(c) || (c == '_')); *p='\0'; value_expected=False; while (isspace(c) || (c == '=')) { if (c == '=') value_expected=True; c=ReadBlobByte(image); count++; } if (value_expected == False) continue; p=value; while (isalnum(c)) { if ((p-value) < (MaxTextExtent-1)) *p++=c; c=ReadBlobByte(image); count++; } *p='\0'; /* Assign a value to the specified keyword. */ if (LocaleCompare(keyword,"Label_RECORDS") == 0) length=MagickAtoL(value); if (LocaleCompare(keyword,"LBLSIZE") == 0) length=MagickAtoL(value); if (LocaleCompare(keyword,"RECORD_BYTES") == 0) image->columns= MagickAtoL(value); if (LocaleCompare(keyword,"NS") == 0) image->columns= MagickAtoL(value); if (LocaleCompare(keyword,"LINES") == 0) image->rows= MagickAtoL(value); if (LocaleCompare(keyword,"NL") == 0) image->rows= MagickAtoL(value); } while (isspace(c)) { c=ReadBlobByte(image); count++; } } while (count < (long) length) { c=ReadBlobByte(image); count++; } if ((image->columns == 0) || (image->rows == 0)) ThrowReaderException(CorruptImageError,NegativeOrZeroImageSize,image); image->depth=8; if (!AllocateImageColormap(image,256)) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); if (image_info->ping) { CloseBlob(image); return(image); } /* Read VICAR pixels. */ scanline=MagickAllocateMemory(unsigned char *,image->columns); if (scanline == (unsigned char *) NULL) ThrowReaderException(CorruptImageError,UnableToReadImageData,image); for (y=0; y < (long) image->rows; y++) { if (!SetImagePixels(image,0,y,image->columns,1)) break; (void) ReadBlob(image,image->columns,scanline); (void) ImportImagePixelArea(image,GrayQuantum,image->depth,scanline,0,0); if (!SyncImagePixels(image)) break; if (QuantumTick(y,image->rows)) if (!MagickMonitorFormatted(y,image->rows,exception,LoadImageText, image->filename, image->columns,image->rows)) break; } MagickFreeMemory(scanline); if (EOFBlob(image)) ThrowException(exception,CorruptImageError,UnexpectedEndOfFile, image->filename); CloseBlob(image); return(image); }
/* * DjVu advertised readiness to provide bitmap: So get it! * we use the RGB format! */ static void get_page_image(LoadContext *lc, ddjvu_page_t *page, int x, int y, int w, int h, QuantumInfo* quantum_info) { ddjvu_rect_t rect; rect.x = x; rect.y = y; rect.w = (unsigned int) w; /* /10 */ rect.h = (unsigned int) h; /* /10 */ Image* image = lc->image; ddjvu_page_type_t type = ddjvu_page_get_type(lc->page); /* stride of this temporary buffer: */ int stride = (type == DDJVU_PAGETYPE_BITONAL)? (lc->image->columns + 7)/8: lc->image->columns *3; unsigned char* q = malloc(lc->image->rows * stride); ddjvu_format_t *format = ddjvu_format_create( (type == DDJVU_PAGETYPE_BITONAL)?DDJVU_FORMAT_LSBTOMSB : DDJVU_FORMAT_RGB24, /* DDJVU_FORMAT_RGB24 * DDJVU_FORMAT_RGBMASK32*/ /* DDJVU_FORMAT_RGBMASK32 */ 0, NULL); #if 0 /* fixme: ThrowReaderException is a macro, which uses `exception' variable */ if (format == NULL) { abort(); /* ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); */ } #endif ddjvu_format_set_row_order(format, 1); ddjvu_format_set_y_direction(format, 1); int ret = ddjvu_page_render(page, DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */ &rect, &rect, /* mmc: ?? */ format, stride, /* ?? */ (char*)q); ddjvu_format_release(format); if (type == DDJVU_PAGETYPE_BITONAL) { /* */ #if DEBUG printf("%s: expanding BITONAL page/image\n", __FUNCTION__); #endif register IndexPacket *indexes; unsigned long bit, byte; for (y=0; y < (long) image->rows; y++) { PixelPacket * o = SetImagePixels(image,0,y,image->columns,1); if (o == (PixelPacket *) NULL) break; indexes=GetIndexes(image); bit=0; byte=0; /* fixme: the non-aligned, last =<7 bits ! that's ok!!!*/ for (x= 0; x < (long) image->columns; x++) { if (bit == 0) byte= (unsigned long) q[(y * stride) + (x / 8)]; indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01); bit++; if (bit == 8) bit=0; byte>>=1; } if (SyncImagePixels(image) == MagickFalse) break; } SyncImage(image); } else {
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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));
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d S C R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadSCRImage() reads a Scitex 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 ReadSCRImage method is: % % Image *ReadSCRImage(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 *ReadSCRImage(const ImageInfo *image_info,ExceptionInfo *exception) { char zxscr[6144]; char zxattr[768]; int octetnr; int octetline; int zoneline; int zonenr; int octet_val; int attr_nr; int pix; int piy; int binar[8]; int attrbin[8]; int *pbin; int *abin; int z; int one_nr; int ink; int paper; int bright; unsigned char colour_palette[] = { 000,000,000, 000,000,192, 192,000,000, 192,000,192, 000,192,000, 000,192,192, 192,192,000, 192,192,192, 000,000,000, 000,000,255, 255,000,000, 255,000,255, 000,255,000, 000,255,255, 255,255,000, 255,255,255 }; Image *image; MagickBooleanType status; register PixelPacket *q; ssize_t count; /* 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); } image->columns = 256; image->rows = 192; if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } count=ReadBlob(image,6144,(unsigned char *) zxscr); count=ReadBlob(image,768,(unsigned char *) zxattr); for(zonenr=0;zonenr<3;zonenr++) { for(zoneline=0;zoneline<8;zoneline++) { for(octetline=0;octetline<8;octetline++) { for(octetnr=(zoneline*32);octetnr<((zoneline*32)+32);octetnr++) { octet_val = zxscr[octetnr+(256*octetline)+(zonenr*2048)]; attr_nr = zxattr[octetnr+(256*zonenr)]; pix = (((8*octetnr)-(256*zoneline))); piy = ((octetline+(8*zoneline)+(zonenr*64))); pbin = binar; abin = attrbin; one_nr=1; for(z=0;z<8;z++) { if(octet_val&one_nr) { *pbin = 1; } else { *pbin = 0; } one_nr=one_nr*2; pbin++; } one_nr = 1; for(z=0;z<8;z++) { if(attr_nr&one_nr) { *abin = 1; } else { *abin = 0; } one_nr=one_nr*2; abin++; } ink = (attrbin[0]+(2*attrbin[1])+(4*attrbin[2])); paper = (attrbin[3]+(2*attrbin[4])+(4*attrbin[5])); bright = attrbin[6]; if(bright) { ink=ink+8; paper=paper+8; } for(z=7;z>-1;z--) { q=SetImagePixels(image,pix,piy,1,1); if(binar[z]) { q->red=(Quantum) QuantumRange*(colour_palette[3*ink]); q->green=(Quantum) QuantumRange*(colour_palette[1+(3*ink)]); q->blue=(Quantum) QuantumRange*(colour_palette[2+(3*ink)]); } else { q->red=(Quantum) QuantumRange*(colour_palette[3*paper]); q->green=(Quantum) QuantumRange*(colour_palette[1+(3*paper)]); q->blue=(Quantum) QuantumRange*(colour_palette[2+(3*paper)]); } pix++; } } } } } CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d A R T I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadARTImage reads an ART 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 ReadARTImage method is: % % Image *ReadARTImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadARTImage 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 *ReadARTImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; int i; unsigned width,height,dummy; long ldblk; unsigned char *BImgBuff=NULL; unsigned char Padding; unsigned int status; const PixelPacket *q; /* 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); /* Read ART image. */ dummy=ReadBlobLSBShort(image); width=ReadBlobLSBShort(image); dummy=ReadBlobLSBShort(image); height=ReadBlobLSBShort(image); ldblk=(long) ((width+7) / 8); Padding=(unsigned char) ((-ldblk) & 0x01); if(GetBlobSize(image)!=(8+((long)ldblk+Padding)*height)) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); image->columns=width; image->rows=height; image->depth=1; image->colors=1l << image->depth; /* printf("ART header checked OK %d,%d\n",image->colors,image->depth); */ if (!AllocateImageColormap(image,image->colors)) goto NoMemory; /* If ping is true, then only set image size and colors without reading any image data. */ if (image_info->ping) goto DONE_READING; /* ----- Load RLE compressed raster ----- */ BImgBuff=MagickAllocateMemory(unsigned char *,((size_t) ldblk)); /*Ldblk was set in the check phase*/ if(BImgBuff==NULL) NoMemory: ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); for(i=0; i<(int)height; i++) { (void) ReadBlob(image,(size_t)ldblk,(char *)BImgBuff); (void) ReadBlob(image,Padding,(char *)&dummy); q=SetImagePixels(image,0,i,image->columns,1); if (q == (PixelPacket *)NULL) break; (void)ImportImagePixelArea(image,GrayQuantum,1,BImgBuff,NULL,0); if (!SyncImagePixels(image)) break; } if(BImgBuff!=NULL) MagickFreeMemory(BImgBuff); if (EOFBlob(image)) ThrowException(exception,CorruptImageError,UnexpectedEndOfFile, image->filename); DONE_READING: CloseBlob(image); return(image); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d T T F I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadTTFImage() reads a TrueType font 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 ReadTTFImage method is: % % Image *ReadTTFImage(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 *ReadTTFImage(const ImageInfo *image_info,ExceptionInfo *exception) { char buffer[MaxTextExtent]; const char *Text = (char *) "The quick brown fox jumps over the lazy dog 0123456789\n" "ABCDEFGHIJKLMNOPQRSTUVWXYZ &#~\"\\'(-`_^@)=+\260\n" "$\243^\250*\265\371%!\247:/;.,?<> " "\342\352\356\373\364\344\353\357\366\374\377\340\371\351\350\347 "; const TypeInfo *type_info; DrawInfo *draw_info; Image *image; long y; MagickBooleanType status; PixelPacket background_color; register long i, x; register PixelPacket *q; /* 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); image->columns=800; image->rows=480; type_info=GetTypeInfo(image_info->filename,exception); if ((type_info != (const TypeInfo *) NULL) && (type_info->glyphs != (char *) NULL)) (void) CopyMagickString(image->filename,type_info->glyphs,MaxTextExtent); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } /* Color canvas with background color */ background_color=image_info->background_color; 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++=background_color; if (SyncImagePixels(image) == MagickFalse) break; } (void) CopyMagickString(image->magick,image_info->magick,MaxTextExtent); (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent); /* Prepare drawing commands */ y=20; draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); draw_info->font=AcquireString(image->filename); draw_info->fill=image_info->pen; ConcatenateString(&draw_info->primitive,"push graphic-context\n"); (void) FormatMagickString(buffer,MaxTextExtent," viewbox 0 0 %lu %lu\n", image->columns,image->rows); ConcatenateString(&draw_info->primitive,buffer); ConcatenateString(&draw_info->primitive," font-size 18\n"); (void) FormatMagickString(buffer,MaxTextExtent," text 10,%ld '",y); ConcatenateString(&draw_info->primitive,buffer); ConcatenateString(&draw_info->primitive,Text); (void) FormatMagickString(buffer,MaxTextExtent,"'\n"); ConcatenateString(&draw_info->primitive,buffer); y+=20*MultilineCensus((char *) Text)+20; for (i=12; i <= 72; i+=6) { y+=i+12; ConcatenateString(&draw_info->primitive," font-size 18\n"); (void) FormatMagickString(buffer,MaxTextExtent," text 10,%ld '%ld'\n",y,i); ConcatenateString(&draw_info->primitive,buffer); (void) FormatMagickString(buffer,MaxTextExtent," font-size %ld\n",i); ConcatenateString(&draw_info->primitive,buffer); (void) FormatMagickString(buffer,MaxTextExtent," text 50,%ld " "'That which does not destroy me, only makes me stronger.'\n",y); ConcatenateString(&draw_info->primitive,buffer); if (i >= 24) i+=6; } ConcatenateString(&draw_info->primitive,"pop graphic-context"); (void) DrawImage(image,draw_info); /* Free resources. */ draw_info=DestroyDrawInfo(draw_info); CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d M A P I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadMAPImage() reads an image of raw RGB colormap and colormap index % bytes 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 ReadMAPImage method is: % % Image *ReadMAPImage(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 *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; IndexPacket index; long y; MagickBooleanType status; register IndexPacket *indexes; register long x; register PixelPacket *q; register long i; register unsigned char *p; size_t packet_size; ssize_t count; unsigned char *colormap, *pixels; unsigned long depth, quantum; /* 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); } /* Initialize image structure. */ image->storage_class=PseudoClass; status=AllocateImageColormap(image,(unsigned long) (image->offset != 0 ? image->offset : 256)); if (status == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); depth=GetImageQuantumDepth(image,MagickTrue); packet_size=(size_t) (depth/8); pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size* sizeof(*pixels)); packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL); colormap=(unsigned char *) AcquireQuantumMemory(image->colors,packet_size* sizeof(*colormap)); if ((pixels == (unsigned char *) NULL) || (colormap == (unsigned char *) NULL)) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); /* Read image colormap. */ count=ReadBlob(image,packet_size*image->colors,colormap); if (count != (ssize_t) (packet_size*image->colors)) ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile"); p=colormap; if (image->depth <= 8) for (i=0; i < (long) image->colors; i++) { image->colormap[i].red=ScaleCharToQuantum(*p++); image->colormap[i].green=ScaleCharToQuantum(*p++); image->colormap[i].blue=ScaleCharToQuantum(*p++); } else for (i=0; i < (long) image->colors; i++) { quantum=(*p++ << 8); quantum|=(*p++); image->colormap[i].red=(Quantum) quantum; quantum=(*p++ << 8); quantum|=(*p++); image->colormap[i].green=(Quantum) quantum; quantum=(*p++ << 8); quantum|=(*p++); image->colormap[i].blue=(Quantum) quantum; } colormap=(unsigned char *) RelinquishMagickMemory(colormap); if (image_info->ping != MagickFalse) { CloseBlob(image); return(GetFirstImageInList(image)); } /* Read image pixels. */ if (SetImageExtent(image,0,0) == MagickFalse) { InheritException(exception,&image->exception); return(DestroyImageList(image)); } packet_size=(size_t) (depth/8); for (y=0; y < (long) image->rows; y++) { p=pixels; q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(image); count=ReadBlob(image,(size_t) packet_size*image->columns,pixels); if (count != (ssize_t) (packet_size*image->columns)) break; for (x=0; x < (long) image->columns; x++) { index=ConstrainColormapIndex(image,*p); p++; if (image->colors > 256) { index=ConstrainColormapIndex(image,((unsigned long) index << 8)+(*p)); p++; } indexes[x]=(IndexPacket) index; *q++=image->colormap[(long) index]; } if (SyncImagePixels(image) == MagickFalse) break; } pixels=(unsigned char *) RelinquishMagickMemory(pixels); if (y < (long) image->rows) ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", image->filename); CloseBlob(image); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d T X T I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadTXTImage reads a text file and returns it as an image. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadTXTImage method is: % % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadTXTImage 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 *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception) { char *p, text[MaxTextExtent]; Image *image; TXT_TYPE txt_subformat; MagickPassFail status; MagickBool logging; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); logging = IsEventLogging(); image=AllocateImage(image_info); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == False) ThrowReaderException(FileOpenError,UnableToOpenFile,image); p = ReadBlobString(image,text); if (p == NULL) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); txt_subformat = IsTXT((unsigned char *)p,strlen(p)); if (txt_subformat != NO_TXT) { unsigned x, y; unsigned x_min, x_max, y_curr; int ch; unsigned long max, i; char NumOfPlanes; unsigned char *BImgBuff; magick_uint16_t *WImgBuff; magick_uint32_t *DImgBuff; magick_uint32_t R, G, B, A; const PixelPacket *q; ExtendedSignedIntegralType NextImagePos = 0; ImportPixelAreaOptions import_options; if (logging) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "File RAW TXT type: %d", (int) txt_subformat); do { (void) SeekBlob(image,NextImagePos,SEEK_SET); if(NextImagePos!=0) { /* Allocate next image structure. */ AllocateNextImage(image_info,image); if(image->next == (Image *)NULL) break; image = SyncNextImageInList(image); image->columns = image->rows = 0; image->colors=0; } A=0; x=0; y=0; max=0; switch(txt_subformat) { case TXT_GM8B_HEX: case TXT_GM8B_HEX_Q: max=255; break; case TXT_GM16B_HEX: case TXT_GM16B_HEX_Q: max=65535; break; case TXT_GM32B_HEX: case TXT_GM32B_HEX_Q: max=65536; break; default: break; } /* Set opacity flag. */ image->matte=MagickFalse; if (txt_subformat >= IMAGEMAGICK_TXT_Q) image->matte=MagickTrue; if (!strncmp(p,"# ImageMagick pixel enumeration:",32)) { if (sscanf(p+32,"%u,%u,%u",&x_min,&y_curr,&x_max) == 3) { if (strstr(p+32,",rgb")!=NULL) { x = x_min-1; y = y_curr-1; max = x_max; } if (strstr(p+32,",rgba")!=NULL) { txt_subformat = IMAGEMAGICK_TXT_Q; } } } ch=0; if (x == 0 && y == 0) while (!EOFBlob(image)) /* auto detect sizes and num of planes */ { while (!(ch >= '0' && ch <= '9')) { /* go to the begin of number */ ch = ReadBlobByte(image); if (ch == EOF) goto EndReading; if (ch == '#') { readln(image,&ch); continue; } if (ch == 0 || ch > 128 || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { TXT_FAIL: /* not a text data */ ThrowReaderException(CoderError,ImageTypeNotSupported,image); } } /* x,y: (R,G,B) */ x_min = ReadInt(image,&ch); /* x */ if (x_min > x) x = x_min; while (ch != ',') { ch = ReadBlobByte(image); if (ch==EOF) break; if (ch == 10 || ch == 13) goto TXT_FAIL; } ch=0; i=ReadInt(image,&ch); /* y */ /* Check for next image start. */ if(x_min==0 && i==0 && x>0 && y>0) goto EndReading; if (i > y) y=i; while (ch != ':') { ch = ReadBlobByte(image); if (ch == 10 || ch == 13) goto TXT_FAIL; if (ch == EOF) break; } if (txt_subformat != TXT_GM8B_PLAIN2_Q) while (ch != '(') { ch = ReadBlobByte(image); if (ch == 10 || ch == 13) goto TXT_FAIL; if (ch == EOF) break; } ch=0; R = ReadInt(image,&ch); /* R */ if (R > max) max=R; while (ch != ',') { ch = ReadBlobByte(image); if (ch == 10 || ch == 13) goto TXT_FAIL; if (ch == EOF) break; } ch=0; G = ReadInt(image,&ch); /* G */ if (G > max) max=G; while (ch != ',') { ch = ReadBlobByte(image); if (ch == 10 || ch == 13) goto TXT_FAIL; if (ch == EOF) break; } ch=0; B = ReadInt(image,&ch); /* B */ if (B > max) max=B; if (txt_subformat >= IMAGEMAGICK_TXT_Q) { while (ch != ',') { ch = ReadBlobByte(image); if (ch == 10 || ch == 13) goto TXT_FAIL; if (ch == EOF) break; } ch=0; A = ReadInt(image,&ch); /* A */ if (A > max) max=A; } if (txt_subformat != TXT_GM8B_PLAIN2_Q) while (ch != ')') { ch = ReadBlobByte(image); if (ch == 10 || ch == 13) goto TXT_FAIL; if (ch == EOF) break; } readln(image,&ch); } EndReading: x_min = 1; x_max = 0; y_curr = 0; NumOfPlanes=8; /* if (max>= 2) NumOfPlanes=2; */ /* if (max>= 4) NumOfPlanes=4; */ /* if (max>= 16) NumOfPlanes=8; */ if (max >= 256) NumOfPlanes=16; if (max >=65536) NumOfPlanes=32; if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), "Image detected [%u * %u]: %d", x, y, NumOfPlanes); image->depth = Min(QuantumDepth,NumOfPlanes); ImportPixelAreaOptionsInit(&import_options); import_options.endian = NativeEndian; BImgBuff = MagickAllocateArray(unsigned char *, (size_t)(x+1), ( ((image->matte) ? 4 : 3) * NumOfPlanes/8)); WImgBuff = (magick_uint16_t *)BImgBuff; DImgBuff = (magick_uint32_t *)BImgBuff; if (BImgBuff == NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); image->columns = x+1; image->rows = y+1; (void) SeekBlob(image,NextImagePos,SEEK_SET); NextImagePos = 0; /* Load picture data */ while (!EOFBlob(image)) { x=0; while (!(ch >= '0' && ch <= '9')) { /* move to the beginning of number */ if (EOFBlob(image)) goto FINISH; ch = ReadBlobByte(image); if (ch == '#') { readln(image,&ch); continue; } } x = ReadInt(image,&ch); /* x */ while (ch != ',') { ch = ReadBlobByte(image); if (ch == EOF) break; } ch = 0; y = ReadInt(image,&ch); /* y */ /* New image detected. */ if(x==0 && y==0 && y_curr>0 && x_max>0) { NextImagePos = TellBlob(image) - 4; break; } while (ch != ':') { ch = ReadBlobByte(image); if (ch == EOF) break; } while (ch != '(') { ch = ReadBlobByte(image); if (ch == EOF) break; } ch=0; R = ReadInt(image,&ch); /* R */ while (ch != ',') { ch = ReadBlobByte(image); if (ch == EOF) break; } ch=0; G = ReadInt(image,&ch); /* G */ while (ch != ',') { ch = ReadBlobByte(image); if (ch == EOF) break; } ch=0; B = ReadInt(image,&ch); /* B */ if (image->matte) { while (ch != ',') { ch = ReadBlobByte(image); if (ch == EOF) break; } ch=0; A = ReadInt(image,&ch); /* A */ if (A > max) max=A; } while (ch != ')') { ch = ReadBlobByte(image); if (ch == EOF) break; } /* A new line has been detected */ if (y != y_curr) { q = SetImagePixels(image,x_min,y_curr,x_max-x_min+1,1); if (q == (PixelPacket *)NULL) break; if (image->matte) (void)ImportImagePixelArea(image,RGBAQuantum,NumOfPlanes, BImgBuff + 4*x_min*(NumOfPlanes/8), &import_options,0); else (void)ImportImagePixelArea(image,RGBQuantum,NumOfPlanes, BImgBuff + 3*x_min*(NumOfPlanes/8), &import_options,0); if (!SyncImagePixels(image)) break; x_min = 1; x_max = 0; y_curr=y; } if (x < image->columns) { if (image->matte) { switch(NumOfPlanes) { case 8: BImgBuff[0+4*x] = R; BImgBuff[1+4*x] = G; BImgBuff[2+4*x] = B; BImgBuff[3+4*x] = 255U-A; break; case 16: WImgBuff[0+4*x] = R; WImgBuff[1+4*x] = G; WImgBuff[2+4*x] = B; WImgBuff[3+4*x] = 65535U-A; break; case 32: DImgBuff[0+4*x] = R; DImgBuff[1+4*x] = G; DImgBuff[2+4*x] = B; DImgBuff[3+4*x] = 4294967295U-A; break; } } else { switch(NumOfPlanes) { case 8: BImgBuff[0+3*x] = R; BImgBuff[1+3*x] = G; BImgBuff[2+3*x] = B; break; case 16: WImgBuff[0+3*x] = R; WImgBuff[1+3*x] = G; WImgBuff[2+3*x] = B; break; case 32: DImgBuff[0+3*x] = R; DImgBuff[1+3*x] = G; DImgBuff[2+3*x] = B; break; } } if (x_min > x_max) { x_max=x_min=x; } else { if (x < x_min) x_min=x; if (x > x_max) x_max=x; } } readln(image,&ch); } FINISH: if (x_min <= x_max) { q = SetImagePixels(image,x_min,y_curr,x_max-x_min+1,1); if (q != (PixelPacket *)NULL) { if (image->matte) (void)ImportImagePixelArea(image, RGBAQuantum, NumOfPlanes, BImgBuff + 4*x_min*(NumOfPlanes/8), &import_options, 0); else (void)ImportImagePixelArea(image, RGBQuantum, NumOfPlanes, BImgBuff + 3*x_min*(NumOfPlanes/8), &import_options, 0); if (!SyncImagePixels(image)) { if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), " TXT failed to sync image pixels for a row %u", y_curr); } } } MagickFreeMemory(BImgBuff); } while(!EOFBlob(image) && NextImagePos>0); goto FINISH_TXT; }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d M A C I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadMACImage reads an MAC 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 ReadMACImage method is: % % Image *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadMACImage 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 *ReadMACImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; unsigned int y; unsigned char x8, rep, b; long ldblk; unsigned char *BImgBuff = NULL; unsigned char *DataPtr; unsigned int status; const PixelPacket *q; /* 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); /* Read MAC image. */ ldblk = ReadBlobLSBShort(image); if((ldblk & 0xFF)!=0) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); if(ldblk==0) /* ???? don't know why */ SeekBlob(image,0x200,SEEK_SET); else SeekBlob(image,0x280,SEEK_SET); image->columns = 576; image->rows = 720; image->depth = 1; image->colors = 1l << image->depth; if (!AllocateImageColormap(image,image->colors)) goto NoMemory; /* If ping is true, then only set image size and colors without reading any image data. */ if (image_info->ping) goto DONE_READING; /* ----- Load RLE compressed raster ----- */ ldblk = (image->depth*image->columns) /8; BImgBuff = MagickAllocateMemory(unsigned char *, ((size_t)ldblk)); if(BImgBuff==NULL) NoMemory: ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); DataPtr = BImgBuff; x8=0; y=0; while(y<image->rows) { rep = ReadBlobByte(image); if(EOFBlob(image)) break; if( rep>=128 || rep<=0) { b = ~ReadBlobByte(image);; rep = ~rep + 2; while(rep>0) { *DataPtr++ = b; x8++; rep--; if(x8>=ldblk) { x8=0; q = SetImagePixels(image,0,y,image->columns,1); if(q == (PixelPacket *)NULL) break; (void)ImportImagePixelArea(image,GrayQuantum,1,BImgBuff,NULL,0); if(!SyncImagePixels(image)) break; DataPtr = BImgBuff; y++; if(y>=image->rows) { break; } } } } else { rep++; while(rep>0) { b = ~ReadBlobByte(image); *DataPtr++ = b; x8++; rep--; if(x8>=ldblk) { x8=0; q = SetImagePixels(image,0,y,image->columns,1); if(q == (PixelPacket *)NULL) break; (void)ImportImagePixelArea(image,GrayQuantum,1,BImgBuff,NULL,0); if (!SyncImagePixels(image)) break; DataPtr = BImgBuff; y++; if(y>=image->rows) { break; } } } } } if(BImgBuff!=NULL) MagickFreeMemory(BImgBuff); if(EOFBlob(image)) ThrowException(exception,CorruptImageError,UnexpectedEndOfFile,image->filename); DONE_READING: CloseBlob(image); return(image); }
static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception) { Image *image; long y, z; MagickBooleanType status; MagickSizeType number_pixels; register IndexPacket *indexes; register long i, x; register PixelPacket *q; register unsigned char *p; ssize_t count; SGIInfo iris_info; size_t bytes_per_pixel; unsigned char *iris_pixels; unsigned long quantum; /* 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 SGI raster header. */ iris_info.magic=ReadBlobMSBShort(image); do { /* Verify SGI identifier. */ if (iris_info.magic != 0x01DA) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); iris_info.storage=(unsigned char) ReadBlobByte(image); switch (iris_info.storage) { case 0x00: image->compression=NoCompression; break; case 0x01: image->compression=RLECompression; break; default: ThrowReaderException(CorruptImageError,"ImproperImageHeader"); } iris_info.bytes_per_pixel=(unsigned char) ReadBlobByte(image); if ((iris_info.bytes_per_pixel == 0) || (iris_info.bytes_per_pixel > 2)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); iris_info.dimension=ReadBlobMSBShort(image); iris_info.columns=ReadBlobMSBShort(image); iris_info.rows=ReadBlobMSBShort(image); iris_info.depth=ReadBlobMSBShort(image); if ((iris_info.depth == 0) || (iris_info.depth > 4)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); iris_info.minimum_value=ReadBlobMSBLong(image); iris_info.maximum_value=ReadBlobMSBLong(image); iris_info.sans=ReadBlobMSBLong(image); (void) ReadBlob(image,sizeof(iris_info.name),(unsigned char *) iris_info.name); iris_info.name[sizeof(iris_info.name)-1]='\0'; if (*iris_info.name != '\0') (void) SetImageProperty(image,"label",iris_info.name); iris_info.pixel_format=ReadBlobMSBLong(image); if (iris_info.pixel_format != 0) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); count=ReadBlob(image,sizeof(iris_info.filler),iris_info.filler); image->columns=iris_info.columns; image->rows=iris_info.rows; image->depth=(unsigned long) MagickMin(iris_info.depth,MAGICKCORE_QUANTUM_DEPTH); if (iris_info.pixel_format == 0) image->depth=(unsigned long) MagickMin((size_t) 8* iris_info.bytes_per_pixel,MAGICKCORE_QUANTUM_DEPTH); if (iris_info.depth < 3) { image->storage_class=PseudoClass; image->colors=256; } 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 SGI pixels. */ bytes_per_pixel=(size_t) iris_info.bytes_per_pixel; number_pixels=(MagickSizeType) iris_info.columns*iris_info.rows; if ((4*bytes_per_pixel*number_pixels) != ((MagickSizeType) (size_t) (4*bytes_per_pixel*number_pixels))) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); iris_pixels=(unsigned char *) AcquireQuantumMemory(iris_info.columns, iris_info.rows*4*bytes_per_pixel*sizeof(*iris_pixels)); if (iris_pixels == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); if ((int) iris_info.storage != 0x01) { unsigned char *scanline; /* Read standard image format. */ scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns, bytes_per_pixel*sizeof(*scanline)); if (scanline == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); for (z=0; z < (long) iris_info.depth; z++) { p=iris_pixels+bytes_per_pixel*z; for (y=0; y < (long) iris_info.rows; y++) { count=ReadBlob(image,bytes_per_pixel*iris_info.columns,scanline); if (EOFBlob(image) != MagickFalse) break; if (bytes_per_pixel == 2) for (x=0; x < (long) iris_info.columns; x++) { *p=scanline[2*x]; *(p+1)=scanline[2*x+1]; p+=8; } else for (x=0; x < (long) iris_info.columns; x++) { *p=scanline[x]; p+=4; } } } scanline=(unsigned char *) RelinquishMagickMemory(scanline); } else { ssize_t offset, *offsets; unsigned char *packets; unsigned int data_order; unsigned long *runlength; /* Read runlength-encoded image format. */ offsets=(ssize_t *) AcquireQuantumMemory((size_t) iris_info.rows, iris_info.depth*sizeof(*offsets)); packets=(unsigned char *) AcquireQuantumMemory((size_t) iris_info.columns+10UL,4UL*sizeof(*packets)); runlength=(unsigned long *) AcquireQuantumMemory(iris_info.rows, iris_info.depth*sizeof(*runlength)); if ((offsets == (ssize_t *) NULL) || (packets == (unsigned char *) NULL) || (runlength == (unsigned long *) NULL)) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); for (i=0; i < (long) (iris_info.rows*iris_info.depth); i++) offsets[i]=(ssize_t) ReadBlobMSBLong(image); for (i=0; i < (long) (iris_info.rows*iris_info.depth); i++) { runlength[i]=ReadBlobMSBLong(image); if (runlength[i] > (4*(size_t) iris_info.columns+10)) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); } /* Check data order. */ offset=0; data_order=0; for (y=0; ((y < (long) iris_info.rows) && (data_order == 0)); y++) for (z=0; ((z < (long) iris_info.depth) && (data_order == 0)); z++) { if (offsets[y+z*iris_info.rows] < offset) data_order=1; offset=offsets[y+z*iris_info.rows]; } offset=(ssize_t) TellBlob(image); if (data_order == 1) { for (z=0; z < (long) iris_info.depth; z++) { p=iris_pixels; for (y=0; y < (long) iris_info.rows; y++) { if (offset != offsets[y+z*iris_info.rows]) { offset=offsets[y+z*iris_info.rows]; offset=(ssize_t) SeekBlob(image,(long) offset,SEEK_SET); } count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows], packets); if (EOFBlob(image) != MagickFalse) break; offset+=runlength[y+z*iris_info.rows]; status=SGIDecode(bytes_per_pixel,(long) (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets, 1L*iris_info.columns,p+bytes_per_pixel*z); if (status == MagickFalse) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); p+=(iris_info.columns*4*bytes_per_pixel); } } } else { MagickOffsetType position; position=TellBlob(image); p=iris_pixels; for (y=0; y < (long) iris_info.rows; y++) { for (z=0; z < (long) iris_info.depth; z++) { if (offset != offsets[y+z*iris_info.rows]) { offset=offsets[y+z*iris_info.rows]; offset=(ssize_t) SeekBlob(image,(long) offset,SEEK_SET); } count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows], packets); if (EOFBlob(image) != MagickFalse) break; offset+=runlength[y+z*iris_info.rows]; status=SGIDecode(bytes_per_pixel,(long) (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets, 1L*iris_info.columns,p+bytes_per_pixel*z); if (status == MagickFalse) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); } p+=(iris_info.columns*4*bytes_per_pixel); } offset=(ssize_t) SeekBlob(image,position,SEEK_SET); } runlength=(unsigned long *) RelinquishMagickMemory(runlength); packets=(unsigned char *) RelinquishMagickMemory(packets); offsets=(ssize_t *) RelinquishMagickMemory(offsets); } /* Initialize image structure. */ image->matte=iris_info.depth == 4 ? MagickTrue : MagickFalse; image->columns=iris_info.columns; image->rows=iris_info.rows; /* Convert SGI raster image to pixel packets. */ if (image->storage_class == DirectClass) { /* Convert SGI image to DirectClass pixel packets. */ if (bytes_per_pixel == 2) { for (y=0; y < (long) image->rows; y++) { p=iris_pixels+(image->rows-y-1)*8*image->columns; q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; for (x=0; x < (long) image->columns; x++) { q->red=ScaleShortToQuantum((unsigned short) ((*(p+0) << 8) | (*(p+1)))); q->green=ScaleShortToQuantum((unsigned short) ((*(p+2) << 8) | (*(p+3)))); q->blue=ScaleShortToQuantum((unsigned short) ((*(p+4) << 8) | (*(p+5)))); q->opacity=OpaqueOpacity; if (image->matte != MagickFalse) q->opacity=(Quantum) (QuantumRange-ScaleShortToQuantum( (unsigned short) ((*(p+6) << 8) | (*(p+7))))); p+=8; 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; } } } else for (y=0; y < (long) image->rows; y++) { p=iris_pixels+(image->rows-y-1)*4*image->columns; 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+1)); q->blue=ScaleCharToQuantum(*(p+2)); q->opacity=OpaqueOpacity; if (image->matte != MagickFalse) q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*(p+3))); p+=4; 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; } } } else { /* Create grayscale map. */ if (AllocateImageColormap(image,image->colors) == MagickFalse) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); /* Convert SGI image to PseudoClass pixel packets. */ if (bytes_per_pixel == 2) { for (y=0; y < (long) image->rows; y++) { p=iris_pixels+(image->rows-y-1)*8*image->columns; q=SetImagePixels(image,0,y,image->columns,1); if (q == (PixelPacket *) NULL) break; indexes=GetIndexes(image); for (x=0; x < (long) image->columns; x++) { quantum=(*p << 8); quantum|=(*(p+1)); indexes[x]=(IndexPacket) quantum; p+=8; 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; } } } else for (y=0; y < (long) image->rows; y++) { p=iris_pixels+(image->rows-y-1)*4*image->columns; 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); p+=4; 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; } } (void) SyncImage(image); } iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_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; iris_info.magic=ReadBlobMSBShort(image); if (iris_info.magic == 0x01DA) { /* 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 (iris_info.magic == 0x01DA); CloseBlob(image); return(GetFirstImageInList(image)); }