ext/RMagick/rmimage.c in rmagick-1.10.0 vs ext/RMagick/rmimage.c in rmagick-1.10.1

- old
+ new

@@ -1,6 +1,6 @@ -/* $Id: rmimage.c,v 1.139 2006/01/18 00:22:33 rmagick Exp $ */ +/* $Id: rmimage.c,v 1.142 2006/02/24 00:15:03 rmagick Exp $ */ /*============================================================================\ | Copyright (C) 2006 by Timothy P. Hunter | Name: rmimage.c | Author: Tim Hunter | Purpose: Image class method definitions for RMagick @@ -1265,11 +1265,11 @@ char *str; StringInfo *str_info; Data_Get_Struct(self, Image, image); - str_info = GetImageProfile(image, "icc"); + str_info = (StringInfo *)GetImageProfile(image, "icc"); if (!str_info) { profile = Qnil; } else @@ -1812,12 +1812,12 @@ Image *comp_image; CompositeOperator operator; GravityType gravity; MagickEnum *magick_enum; ExceptionInfo exception; - long x_offset; - long y_offset; + signed long x_offset; + signed long y_offset; Data_Get_Struct(self, Image, image); switch (argc) { @@ -1833,42 +1833,42 @@ case NorthWestGravity: x_offset = 0; y_offset = 0; break; case NorthGravity: - x_offset = (image->columns - comp_image->columns) / 2; + x_offset = ((long)(image->columns) - (long)(comp_image->columns)) / 2; y_offset = 0; break; case NorthEastGravity: - x_offset = image->columns - comp_image->columns; + x_offset = (long)(image->columns) - (long)(comp_image->columns); y_offset = 0; break; case WestGravity: x_offset = 0; - y_offset = (image->rows - comp_image->rows) / 2; + y_offset = ((long)(image->rows) - (long)(comp_image->rows)) / 2; break; case StaticGravity: case CenterGravity: default: - x_offset = (image->columns - comp_image->columns) / 2; - y_offset = (image->rows - comp_image->rows) / 2; + x_offset = ((long)(image->columns) - (long)(comp_image->columns)) / 2; + y_offset = ((long)(image->rows) - (long)(comp_image->rows)) / 2; break; case EastGravity: - x_offset = image->columns - comp_image->columns; - y_offset = (image->rows - comp_image->rows) / 2; + x_offset = (long)(image->columns) - (long)(comp_image->columns); + y_offset = ((long)(image->rows) - (long)(comp_image->rows)) / 2; break; case SouthWestGravity: x_offset = 0; - y_offset = image->rows - comp_image->rows; + y_offset = (long)(image->rows) - (long)(comp_image->rows); break; case SouthGravity: - x_offset = (image->columns - comp_image->columns) / 2; - y_offset = image->rows - comp_image->rows; + x_offset = ((long)(image->columns) - (long)(comp_image->columns)) / 2; + y_offset = (long)(image->rows) - (long)(comp_image->rows); break; case SouthEastGravity: - x_offset = image->columns - comp_image->columns; - y_offset = image->rows - comp_image->rows; + x_offset = (long)(image->columns) - (long)(comp_image->columns); + y_offset = (long)(image->rows) - (long)(comp_image->rows); break; } break; case 4: // argv[1], argv[2] is x_off, y_off, @@ -1888,19 +1888,19 @@ switch(gravity) { case NorthEastGravity: case EastGravity: - x_offset = image->columns - comp_image->columns - x_offset; + x_offset = (long)(image->columns) - (long)(comp_image->columns) - x_offset; break; case SouthWestGravity: case SouthGravity: - y_offset = image->rows - comp_image->rows - y_offset; + y_offset = (long)(image->rows) - (long)(comp_image->rows) - y_offset; break; case SouthEastGravity: - x_offset = image->columns - comp_image->columns - x_offset; - y_offset = image->rows - comp_image->rows - y_offset; + x_offset = (long)(image->columns) - (long)(comp_image->columns) - x_offset; + y_offset = (long)(image->rows) - (long)(comp_image->rows) - y_offset; break; default: Data_Get_Struct(argv[1], MagickEnum, magick_enum); rb_warning("gravity type `%s' has no effect", rb_id2name(magick_enum->id)); break; @@ -1910,20 +1910,10 @@ default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 3, 4, or 5)", argc); break; } - // CompositeImage doesn't react well to negative offsets! - if (x_offset < 0) - { - x_offset = 0; - } - if (y_offset < 0) - { - y_offset = 0; - } - if (bang) { rm_check_frozen(self); (void) CompositeImage(image, operator, comp_image, x_offset, y_offset); HANDLE_ERROR_IMG(image) @@ -2568,11 +2558,11 @@ boolean okay; ExceptionInfo exception; union { volatile Quantum *i; - volatile float *f; + volatile double *f; volatile void *v; } pixels; if (argc < 5 || argc > 6) { @@ -2584,17 +2574,17 @@ columns = NUM2ULONG(argv[2]); rows = NUM2ULONG(argv[3]); map = STRING_PTR_LEN(argv[4], mapL); if (argc == 6) { - stg_type = RTEST(argv[5]) ? FloatPixel : FIX_STG_TYPE; + stg_type = RTEST(argv[5]) ? DoublePixel : FIX_STG_TYPE; } // Compute the size of the pixel array and allocate the memory. npixels = columns * rows * mapL; pixels.v = stg_type == FIX_STG_TYPE ? (void *) ALLOC_N(Quantum, npixels) - : (void *) ALLOC_N(float, npixels); + : (void *) ALLOC_N(double, npixels); // Create the Ruby array for the pixels. Return this even if DispatchImage fails. pixels_ary = rb_ary_new(); Data_Get_Struct(self, Image, image); @@ -2625,13 +2615,11 @@ } else { for (n = 0; n < npixels; n++) { - // The ImageMagick doc for DispatchImage says that the returned pixel data - // is normalized, but it isn't, so we have to normalize it here. - rb_ary_push(pixels_ary, rb_float_new((double)pixels.f[n]/((double)MaxRGB))); + rb_ary_push(pixels_ary, rb_float_new((double)pixels.f[n])); } } xfree((void *)pixels.v); return pixels_ary; @@ -2824,11 +2812,11 @@ name = GetNextImageProfile(image); while (name) { rb_ary_store(ary, 0, rb_str_new2(name)); - str_info = GetImageProfile(image, name); + str_info = (StringInfo *)GetImageProfile(image, name); if (str_info) { str = StringInfoToString(str_info); rb_ary_store(ary, 1, rb_str_new2(str)); DestroyString(str); @@ -3933,10 +3921,11 @@ char *map; volatile VALUE pixel_arg, pixel_ary; StorageType stg_type = CharPixel; size_t type_sz, map_l; volatile int *pixels = NULL; + volatile double *fpixels = NULL; volatile void *buffer; unsigned int okay; rm_check_frozen(self); @@ -3984,10 +3973,16 @@ type_sz = sizeof(unsigned int); break; case LongPixel: type_sz = sizeof(unsigned long); break; + case DoublePixel: + type_sz = sizeof(double); + break; + case FloatPixel: + type_sz = sizeof(float); + break; #if defined(HAVE_QUANTUMPIXEL) case QuantumPixel: type_sz = sizeof(Quantum); break; #endif @@ -4002,11 +3997,11 @@ } if ((buffer_l / type_sz) % map_l != 0) { rb_raise(rb_eArgError, "pixel buffer must contain an exact multiple of the map length"); } - if (buffer_l/type_sz < npixels) + if (buffer_l / type_sz < npixels) { rb_raise(rb_eArgError, "pixel buffer too small (need %lu channel values, got %ld)" , npixels, buffer_l/type_sz); } } @@ -4026,36 +4021,50 @@ { rb_raise(rb_eArgError, "pixel array too small (need %lu elements, got %ld)" , npixels, RARRAY(pixel_ary)->len); } - // Get array for integer pixels. Use Ruby's memory so GC will clean up after us - // in case of an exception. - pixels = ALLOC_N(int, npixels); - if (!pixels) // app recovered from exception... + if (stg_type == DoublePixel || stg_type == FloatPixel) { - return self; + // Get an array for double pixels. Use Ruby's memory so GC will clean up after + // us in case of an exception. + fpixels = ALLOC_N(double, npixels); + for (n = 0; n < npixels; n++) + { + fpixels[n] = NUM2DBL(rb_ary_entry(pixel_ary, n)); + } + buffer = (void *) fpixels; + stg_type = DoublePixel; } - - for (n = 0; n < npixels; n++) + else { - volatile VALUE p = rb_ary_entry(pixel_ary, n); - long q = ScaleQuantumToLong(NUM2LONG(p)); - pixels[n] = (int) q; + // Get array for integer pixels. Use Ruby's memory so GC will clean up after us + // in case of an exception. + pixels = ALLOC_N(int, npixels); + for (n = 0; n < npixels; n++) + { + volatile VALUE p = rb_ary_entry(pixel_ary, n); + long q = ScaleQuantumToLong(NUM2LONG(p)); + pixels[n] = (int) q; + } + buffer = (void *) pixels; + stg_type = IntegerPixel; } - buffer = (void *) pixels; - stg_type = IntegerPixel; } okay = ImportImagePixels(image, x_off, y_off, cols, rows, map, stg_type, (const void *)buffer); // Free pixel array before checking for errors. if (pixels) { xfree((void *)pixels); } + if (fpixels) + { + xfree((void *)fpixels); + } if (!okay) { HANDLE_ERROR_IMG(image) // Shouldn't get here... @@ -4252,10 +4261,10 @@ Data_Get_Struct(self, Image, image); profile = Qnil; - str_info = GetImageProfile(image, "iptc"); + str_info = (StringInfo *)GetImageProfile(image, "iptc"); if (str_info) { str = StringInfoToString(str_info); profile = rb_str_new2(str); DestroyString(str);