/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % P i x e l I t e r a t e T r i p l e M o d i f y % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % PixelIterateTripleModify() iterates through pixel regions of three images % and invokes a user-provided callback function (of type % PixelIteratorTripleModifyCallback) for each row of pixels. The first two % images are read-only, while the third image is read-write for update. % Access of the first two images is done lock-step using the same coordinates. % This is useful to support operations such as image differencing. % % The format of the PixelIterateTripleModify method is: % % MagickPassFail PixelIterateTripleModify( % PixelIteratorTripleModifyCallback call_back, % const PixelIteratorOptions *options, % const char *description, % void *mutable_data, % const void *immutable_data, % const unsigned long columns, % const unsigned long rows, % const Image *source1_image, % const Image *source2_image, % const long source_x, % const long source_y, % Image *update_image, % const long update_x, % const long update_y, % ExceptionInfo *exception) % % A description of each parameter follows: % % o call_back: A user-provided C callback function which reads from % a region of source pixels and updates a region of destination pixels. % % o options: Pixel iterator execution options (may be NULL). % % o description: textual description of operation being performed. % % o mutable_data: User-provided mutable context data. % % o immutable_data: User-provided immutable context data. % % o columns: Width of pixel region % % o rows: Height of pixel region % % o source1_image: The address of the constant source 1 Image. % % o source2_image: The address of the constant source 2 Image. % % o source_x: The horizontal ordinate of the top left corner of the source regions. % % o source_y: The vertical ordinate of the top left corner of the source regions. % % o update_image: The address of the update Image. % % o update_x: The horizontal ordinate of the top left corner of the update region. % % o update_y: The vertical ordinate of the top left corner of the update region. % % o exception: If an error is reported, this argument is updated with the reason. % */ static MagickPassFail PixelIterateTripleImplementation(PixelIteratorTripleModifyCallback call_back, const PixelIteratorOptions *options, const char *description, void *mutable_data, const void *immutable_data, const unsigned long columns, const unsigned long rows, const Image *source1_image, const Image *source2_image, const long source_x, const long source_y, Image *update_image, const long update_x, const long update_y, ExceptionInfo *exception, MagickBool set) { MagickPassFail status = MagickPass; register long row; unsigned long row_count=0; int max_threads; max_threads=omp_get_max_threads(); (void) SetRegionThreads(max_threads,options,columns,rows); #if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(row_count, status) #endif for (row=0; row < (long) rows; row++) { MagickBool thread_status; const PixelPacket *source1_pixels, *source2_pixels; const IndexPacket *source1_indexes, *source2_indexes; PixelPacket *update_pixels; IndexPacket *update_indexes; long source_row, update_row; thread_status=status; if (thread_status == MagickFail) continue; source_row=source_y+row; update_row=update_y+row; /* First image (read only). */ source1_pixels=AcquireImagePixels(source1_image, source_x, source_row, columns, 1, exception); if (!source1_pixels) thread_status=MagickFail; source1_indexes=AccessImmutableIndexes(source1_image); /* Second image (read only). */ source2_pixels=AcquireImagePixels(source2_image, source_x, source_row, columns, 1, exception); if (!source2_pixels) thread_status=MagickFail; source2_indexes=AccessImmutableIndexes(source2_image); /* Third image (read/write). */ if (set) update_pixels=SetImagePixelsEx(update_image, update_x, update_row, columns, 1, exception); else update_pixels=GetImagePixelsEx(update_image, update_x, update_row, columns, 1, exception); if (!update_pixels) { thread_status=MagickFail; CopyException(exception,&update_image->exception); } update_indexes=AccessMutableIndexes(update_image); if (thread_status != MagickFail) status=(call_back)(mutable_data,immutable_data, source1_image,source1_pixels,source1_indexes, source2_image,source2_pixels,source2_indexes, update_image,update_pixels,update_indexes, columns,exception); if (!SyncImagePixelsEx(update_image,exception)) thread_status=MagickFail; #if defined(HAVE_OPENMP) # pragma omp critical (GM_PixelIterateTripleImplementation) #endif { row_count++; if (QuantumTick(row_count,rows)) if (!MagickMonitorFormatted(row_count,rows,exception,description, source1_image->filename, source2_image->filename, update_image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } omp_set_num_threads(max_threads); return (status); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % P i x e l I t e r a t e M o n o M o d i f y % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % PixelIterateMonoModify() iterates through a region of an image and invokes % a user-provided callback function (of type PixelIteratorMonoReadCallback) % for a region of pixels. This is useful to support simple operations such as % level shifting, colorspace translation, or thresholding. % % The format of the PixelIterateMonoModify method is: % % MagickPassFail PixelIterateMonoModify( % PixelIteratorMonoModifyCallback call_back, % const PixelIteratorOptions *options, % const char *description, % void *mutable_data, % const void *immutable_data, % const long x, % const long y, % const unsigned long columns, % const unsigned long rows, % Image *image, % ExceptionInfo *exception) % % A description of each parameter follows: % % o call_back: A user-provided C callback function which is passed the % address of pixels from each image. % % o options: Pixel iterator execution options (may be NULL). % % o description: textual description of operation being performed. % % o mutable_data: User-provided mutable context data. % % o immutable_data: User-provided immutable context data. % % o x: The horizontal ordinate of the top left corner of the region. % % o y: The vertical ordinate of the top left corner of the region. % % o columns: Width of pixel region % % o rows: Height of pixel region % % o image: The address of the Image. % % o exception: If an error is reported, this argument is updated with the reason. % */ MagickExport MagickPassFail PixelIterateMonoModify(PixelIteratorMonoModifyCallback call_back, const PixelIteratorOptions *options, const char *description, void *mutable_data, const void *immutable_data, const long x, const long y, const unsigned long columns, const unsigned long rows, Image *image, ExceptionInfo *exception) { MagickPassFail status = MagickPass; register long row; unsigned long row_count=0; int max_threads; max_threads=omp_get_max_threads(); (void) SetRegionThreads(max_threads,options,columns,rows); #if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(row_count, status) #endif for (row=y; row < (long) (y+rows); row++) { MagickBool thread_status; PixelPacket *pixels; IndexPacket *indexes; thread_status=status; if (thread_status == MagickFail) continue; pixels=GetImagePixelsEx(image, x, row, columns, 1, exception); if (!pixels) thread_status=MagickFail; indexes=AccessMutableIndexes(image); if (thread_status != MagickFail) thread_status=(call_back)(mutable_data,immutable_data,image,pixels,indexes,columns,exception); if (thread_status != MagickFail) if (!SyncImagePixelsEx(image,exception)) thread_status=MagickFail; #if defined(HAVE_OPENMP) # pragma omp critical (GM_PixelIterateMonoModify) #endif { row_count++; if (QuantumTick(row_count,rows)) if (!MagickMonitorFormatted(row_count,rows,exception, description,image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } omp_set_num_threads(max_threads); return (status); }
static Image *IntegralRotateImage(const Image *image,unsigned int rotations, ExceptionInfo *exception) { char message[MaxTextExtent]; Image *rotate_image; RectangleInfo page; long tile_width_max, tile_height_max; MagickPassFail status=MagickPass; /* Initialize rotated image attributes. */ assert(image != (Image *) NULL); page=image->page; rotations%=4; { /* Clone appropriately to create rotate image. */ unsigned long clone_columns=0, clone_rows=0; switch (rotations) { case 0: clone_columns=0; clone_rows=0; break; case 2: clone_columns=image->columns; clone_rows=image->rows; break; case 1: case 3: clone_columns=image->rows; clone_rows=image->columns; break; } rotate_image=CloneImage(image,clone_columns,clone_rows,True,exception); if (rotate_image == (Image *) NULL) return((Image *) NULL); if (rotations != 0) if (ModifyCache(rotate_image,exception) != MagickPass) { DestroyImage(rotate_image); return (Image *) NULL; } } tile_height_max=tile_width_max=2048/sizeof(PixelPacket); /* 2k x 2k = 4MB */ if ((rotations == 1) || (rotations == 3)) { /* Allow override of tile geometry for testing. */ const char * value; if (!GetPixelCacheInCore(image) || !GetPixelCacheInCore(rotate_image)) tile_height_max=tile_width_max=8192/sizeof(PixelPacket); /* 8k x 8k = 64MB */ if ((value=getenv("MAGICK_ROTATE_TILE_GEOMETRY"))) { double width, height; if (GetMagickDimension(value,&width,&height,NULL,NULL) == 2) { tile_height_max=(unsigned long) height; tile_width_max=(unsigned long) width; } } } /* Integral rotate the image. */ switch (rotations) { case 0: { /* Rotate 0 degrees (nothing more to do). */ (void) strlcpy(message,"[%s] Rotate: 0 degrees...",sizeof(message)); if (!MagickMonitorFormatted(image->rows-1,image->rows,exception, message,image->filename)) status=MagickFail; break; } case 1: { /* Rotate 90 degrees. */ magick_int64_t tile; magick_uint64_t total_tiles; long tile_y; (void) strlcpy(message,"[%s] Rotate: 90 degrees...",sizeof(message)); total_tiles=(((image->rows/tile_height_max)+1)* ((image->columns/tile_width_max)+1)); tile=0; #if defined(IntegralRotateImageUseOpenMP) # if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(status, tile) # endif #endif for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height_max) { long tile_x; MagickPassFail thread_status; thread_status=status; if (thread_status == MagickFail) continue; for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width_max) { long dest_tile_x, dest_tile_y; long tile_width, tile_height; const PixelPacket *tile_pixels; long y; /* Compute image region corresponding to tile. */ if ((unsigned long) tile_x+tile_width_max > image->columns) tile_width=(tile_width_max-(tile_x+tile_width_max-image->columns)); else tile_width=tile_width_max; if ((unsigned long) tile_y+tile_height_max > image->rows) tile_height=(tile_height_max-(tile_y+tile_height_max-image->rows)); else tile_height=tile_height_max; /* Acquire tile */ tile_pixels=AcquireImagePixels(image,tile_x,tile_y, tile_width,tile_height,exception); if (tile_pixels == (const PixelPacket *) NULL) { thread_status=MagickFail; break; } /* Compute destination tile coordinates. */ dest_tile_x=rotate_image->columns-(tile_y+tile_height); dest_tile_y=tile_x; /* Rotate tile */ for (y=0; y < tile_width; y++) { register const PixelPacket *p; register PixelPacket *q; register const IndexPacket *indexes; IndexPacket *rotate_indexes; register long x; q=SetImagePixelsEx(rotate_image,dest_tile_x,dest_tile_y+y, tile_height,1,exception); if (q == (PixelPacket *) NULL) { thread_status=MagickFail; break; } /* DirectClass pixels */ p=tile_pixels+(tile_height-1)*tile_width + y; for (x=tile_height; x != 0; x--) { *q = *p; q++; p-=tile_width; } /* Indexes */ indexes=AccessImmutableIndexes(image); if (indexes != (IndexPacket *) NULL) { rotate_indexes=AccessMutableIndexes(rotate_image); if (rotate_indexes != (IndexPacket *) NULL) { register IndexPacket *iq; register const IndexPacket *ip; iq=rotate_indexes; ip=indexes+(tile_height-1)*tile_width + y; for (x=tile_height; x != 0; x--) { *iq = *ip; iq++; ip -= tile_width; } } } if (!SyncImagePixelsEx(rotate_image,exception)) { thread_status=MagickFail; break; } } #if defined(IntegralRotateImageUseOpenMP) # if defined(HAVE_OPENMP) # pragma omp critical (GM_IntegralRotateImage) # endif #endif { tile++; if (QuantumTick(tile,total_tiles)) if (!MagickMonitorFormatted(tile,total_tiles,exception, message,image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } } Swap(page.width,page.height); Swap(page.x,page.y); page.x=(long) (page.width-rotate_image->columns-page.x); break; } case 2: { /* Rotate 180 degrees. */ long y; unsigned long row_count=0; (void) strlcpy(message,"[%s] Rotate: 180 degrees...",sizeof(message)); #if defined(IntegralRotateImageUseOpenMP) # if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,8) shared(row_count, status) # endif #endif for (y=0; y < (long) image->rows; y++) { register const PixelPacket *p; register PixelPacket *q; register const IndexPacket *indexes; IndexPacket *rotate_indexes; register long x; MagickPassFail thread_status; thread_status=status; if (thread_status == MagickFail) continue; p=AcquireImagePixels(image,0,y,image->columns,1,exception); q=SetImagePixelsEx(rotate_image,0,(long) (image->rows-y-1), image->columns,1,exception); if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) thread_status=MagickFail; if (thread_status != MagickFail) { q+=image->columns; indexes=AccessImmutableIndexes(image); rotate_indexes=AccessMutableIndexes(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 (!SyncImagePixelsEx(rotate_image,exception)) thread_status=MagickFail; } #if defined(IntegralRotateImageUseOpenMP) # if defined(HAVE_OPENMP) # pragma omp critical (GM_IntegralRotateImage) # endif #endif { row_count++; if (QuantumTick(row_count,image->rows)) if (!MagickMonitorFormatted(row_count,image->rows,exception, message,image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } page.x=(long) (page.width-rotate_image->columns-page.x); page.y=(long) (page.height-rotate_image->rows-page.y); break; } case 3: { /* Rotate 270 degrees. */ magick_int64_t tile; magick_uint64_t total_tiles; long tile_y; (void) strlcpy(message,"[%s] Rotate: 270 degrees...",sizeof(message)); total_tiles=(((image->rows/tile_height_max)+1)* ((image->columns/tile_width_max)+1)); tile=0; #if defined(IntegralRotateImageUseOpenMP) # if defined(HAVE_OPENMP) # pragma omp parallel for schedule(static,1) shared(status, tile) # endif #endif for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height_max) { long tile_x; MagickPassFail thread_status; thread_status=status; if (thread_status == MagickFail) continue; for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width_max) { long tile_width, tile_height; long dest_tile_x, dest_tile_y; long y; const PixelPacket *tile_pixels; /* Compute image region corresponding to tile. */ if ((unsigned long) tile_x+tile_width_max > image->columns) tile_width=(tile_width_max-(tile_x+tile_width_max-image->columns)); else tile_width=tile_width_max; if ((unsigned long) tile_y+tile_height_max > image->rows) tile_height=(tile_height_max-(tile_y+tile_height_max-image->rows)); else tile_height=tile_height_max; /* Acquire tile */ tile_pixels=AcquireImagePixels(image,tile_x,tile_y, tile_width,tile_height,exception); if (tile_pixels == (const PixelPacket *) NULL) { thread_status=MagickFail; break; } /* Compute destination tile coordinates. */ dest_tile_x=tile_y; dest_tile_y=rotate_image->rows-(tile_x+tile_width); /* Rotate tile */ for (y=0; y < tile_width; y++) { register const PixelPacket *p; register PixelPacket *q; register const IndexPacket *indexes; register long x; IndexPacket *rotate_indexes; q=SetImagePixelsEx(rotate_image,dest_tile_x,dest_tile_y+y, tile_height,1,exception); if (q == (PixelPacket *) NULL) { thread_status=MagickFail; break; } /* DirectClass pixels */ p=tile_pixels+(tile_width-1-y); for (x=tile_height; x != 0; x--) { *q = *p; q++; p += tile_width; } /* Indexes */ indexes=AccessImmutableIndexes(image); if (indexes != (IndexPacket *) NULL) { rotate_indexes=AccessMutableIndexes(rotate_image); if (rotate_indexes != (IndexPacket *) NULL) { register IndexPacket *iq; register const IndexPacket *ip; iq=rotate_indexes; ip=indexes+(tile_width-1-y); for (x=tile_height; x != 0; x--) { *iq = *ip; iq++; ip += tile_width; } } } if (!SyncImagePixelsEx(rotate_image,exception)) { thread_status=MagickFail; break; } } #if defined(IntegralRotateImageUseOpenMP) # if defined(HAVE_OPENMP) # pragma omp critical (GM_IntegralRotateImage) # endif #endif { tile++; if (QuantumTick(tile,total_tiles)) if (!MagickMonitorFormatted(tile,total_tiles,exception, message,image->filename)) thread_status=MagickFail; } if (thread_status == MagickFail) { status=MagickFail; break; } } } Swap(page.width,page.height); Swap(page.x,page.y); page.y=(long) (page.height-rotate_image->rows-page.y); break; } } rotate_image->page=page; rotate_image->is_grayscale=image->is_grayscale; rotate_image->is_monochrome=image->is_monochrome; return(rotate_image); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d T I M I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method ReadTIMImage reads a PSX TIM image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % Contributed by [email protected]. % % The format of the ReadTIMImage method is: % % Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image: Method ReadTIMImage 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 *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception) { typedef struct _TIMInfo { unsigned long id, flag; } TIMInfo; TIMInfo tim_info; Image *image; int bits_per_pixel, has_clut; long y; register IndexPacket *indexes; register long x; register PixelPacket *q; register long i; register unsigned char *p; unsigned char *tim_data, *tim_pixels; unsigned short word; unsigned int status; size_t bytes_per_line, image_size; unsigned long height, pixel_mode, width; /* 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); /* Determine if this is a TIM file. */ tim_info.id=ReadBlobLSBLong(image); do { /* Verify TIM identifier. */ if (tim_info.id != 0x00000010) ThrowReaderException(CorruptImageError,ImproperImageHeader,image); tim_info.flag=ReadBlobLSBLong(image); has_clut=!!(tim_info.flag & (1 << 3)); pixel_mode=tim_info.flag & 0x07; switch ((int) pixel_mode) { case 0: bits_per_pixel=4; break; case 1: bits_per_pixel=8; break; case 2: bits_per_pixel=16; break; case 3: bits_per_pixel=24; break; default: bits_per_pixel=4; break; } image->depth=8; if (has_clut) { unsigned char *tim_colormap; /* Read TIM raster colormap. */ (void)ReadBlobLSBLong(image); (void)ReadBlobLSBShort(image); (void)ReadBlobLSBShort(image); /* width= */ (void)ReadBlobLSBShort(image); /* height= */ (void)ReadBlobLSBShort(image); if (!AllocateImageColormap(image,pixel_mode == 1 ? 256 : 16)) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); tim_colormap=MagickAllocateMemory(unsigned char *,image->colors*2); if (tim_colormap == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed, image); (void) ReadBlob(image,2*image->colors,(char *) tim_colormap); p=tim_colormap; for (i=0; i < (long) image->colors; i++) { word=(*p++); word|=(unsigned short) (*p++ << 8U); image->colormap[i].blue=ScaleCharToQuantum(ScaleColor5to8((word >> 10U) & 0x1fU)); image->colormap[i].green=ScaleCharToQuantum(ScaleColor5to8((word >> 5U) & 0x1fU)); image->colormap[i].red=ScaleCharToQuantum(ScaleColor5to8(word & 0x1fU)); } MagickFreeMemory(tim_colormap); } /* Read image data. */ (void) ReadBlobLSBLong(image); (void) ReadBlobLSBShort(image); (void) ReadBlobLSBShort(image); if (EOFBlob(image)) ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image); width=ReadBlobLSBShort(image); height=ReadBlobLSBShort(image); image_size=MagickArraySize(2,MagickArraySize(width,height)); bytes_per_line=MagickArraySize(width,2); width=(unsigned long)(MagickArraySize(width,16))/bits_per_pixel; /* Initialize image structure. */ image->columns=width; image->rows=height; if (image_info->ping && (image_info->subrange != 0)) if (image->scene >= (image_info->subimage+image_info->subrange-1)) break; if (CheckImagePixelLimits(image, exception) != MagickPass) ThrowReaderException(ResourceLimitError,ImagePixelLimitExceeded,image); tim_data=MagickAllocateMemory(unsigned char *,image_size); if (tim_data == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image); (void) ReadBlob(image,image_size,(char *) tim_data); tim_pixels=tim_data; /* Convert TIM raster image to pixel packets. */ switch (bits_per_pixel) { case 4: { /* Convert PseudoColor scanline. */ for (y=(long) image->rows-1; y >= 0; y--) { q=SetImagePixelsEx(image,0,y,image->columns,1,exception); if (q == (PixelPacket *) NULL) break; indexes=AccessMutableIndexes(image); p=tim_pixels+y*bytes_per_line; for (x=0; x < ((long) image->columns-1); x+=2) { indexes[x]=(*p) & 0xf; indexes[x+1]=(*p >> 4) & 0xf; p++; } if ((image->columns % 2) != 0) { indexes[x]=(*p >> 4) & 0xf; p++; } if (!SyncImagePixelsEx(image,exception)) break; if (QuantumTick(y,image->rows)) { status=MagickMonitorFormatted(image->rows-y-1,image->rows, exception,LoadImageText, image->filename, image->columns,image->rows); if (status == False) break; } } break; } case 8: { /* Convert PseudoColor scanline. */ for (y=(long) image->rows-1; y >= 0; y--) { q=SetImagePixelsEx(image,0,y,image->columns,1,exception); if (q == (PixelPacket *) NULL) break; indexes=AccessMutableIndexes(image); p=tim_pixels+y*bytes_per_line; for (x=0; x < (long) image->columns; x++) indexes[x]=(*p++); if (!SyncImagePixelsEx(image,exception)) break; if (QuantumTick(y,image->rows)) { status=MagickMonitorFormatted(image->rows-y-1,image->rows, exception,LoadImageText, image->filename, image->columns,image->rows); if (status == False) break; } } break; } case 16: { /* Convert DirectColor scanline. */ for (y=(long) image->rows-1; y >= 0; y--) { p=tim_pixels+y*bytes_per_line; q=SetImagePixelsEx(image,0,y,image->columns,1,exception); if (q == (PixelPacket *) NULL) break; for (x=0; x < (long) image->columns; x++) { word=(*p++); word|=(*p++ << 8); q->blue=ScaleCharToQuantum(ScaleColor5to8((word >> 10) & 0x1f)); q->green=ScaleCharToQuantum(ScaleColor5to8((word >> 5) & 0x1f)); q->red=ScaleCharToQuantum(ScaleColor5to8(word & 0x1f)); q++; } if (!SyncImagePixelsEx(image,exception)) break; if (QuantumTick(y,image->rows)) { status=MagickMonitorFormatted(image->rows-y-1,image->rows, exception,LoadImageText, image->filename, image->columns,image->rows); if (status == False) break; } } break; } case 24: { /* Convert DirectColor scanline. */ for (y=(long) image->rows-1; y >= 0; y--) { p=tim_pixels+y*bytes_per_line; q=SetImagePixelsEx(image,0,y,image->columns,1,exception); 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 (!SyncImagePixelsEx(image,exception)) break; if (QuantumTick(y,image->rows)) { status=MagickMonitorFormatted(image->rows-y-1,image->rows, exception,LoadImageText, image->filename, image->columns,image->rows); if (status == False) break; } } break; } default: ThrowReaderException(CorruptImageError,ImproperImageHeader,image) } if (image->storage_class == PseudoClass) (void) SyncImage(image); MagickFreeMemory(tim_pixels); if (EOFBlob(image)) { ThrowException(exception,CorruptImageError,UnexpectedEndOfFile, image->filename); break; } /* Proceed to next image. */ tim_info.id=ReadBlobLSBLong(image); if (tim_info.id == 0x00000010) { /* 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 (tim_info.id == 0x00000010);
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % 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);