ext/RMagick/rmutil.c in rmagick-1.14.1 vs ext/RMagick/rmutil.c in rmagick-1.15.0
- old
+ new
@@ -1,8 +1,8 @@
-/* $Id: rmutil.c,v 1.84 2006/10/21 13:41:32 rmagick Exp $ */
+/* $Id: rmutil.c,v 1.90 2007/01/15 23:32:31 rmagick Exp $ */
/*============================================================================\
-| Copyright (C) 2006 by Timothy P. Hunter
+| Copyright (C) 2007 by Timothy P. Hunter
| Name: rmutil.c
| Author: Tim Hunter
| Purpose: Utility functions for RMagick
\============================================================================*/
@@ -18,52 +18,79 @@
static void handle_exception(ExceptionInfo *, Image *, ErrorRetention);
#if defined(HAVE_NEW_COLORINFO)
static VALUE Pixel_from_MagickPixelPacket(MagickPixelPacket *);
#endif
+#if !defined(HAVE_ACQUIREMAGICKMEMORY)
/*
+ Dummy versions of ImageMagick memory routines for use with GraphicsMagick.
+*/
+static void *AcquireMagickMemory(const size_t size)
+{
+ assert(size > 0);
+ return malloc(size);
+}
+
+static void *RelinquishMagickMemory(void *memory)
+{
+ if (memory)
+ {
+ free(memory);
+ }
+ return NULL;
+}
+
+static void *ResizeMagickMemory(void *memory, const size_t size)
+{
+ void *new;
+
+ if (!memory)
+ {
+ return malloc(size);
+ }
+ if (size == 0)
+ {
+ free(memory);
+ return NULL;
+ }
+ new = realloc(memory, size);
+ if (!new)
+ {
+ free(memory);
+ }
+ return new;
+}
+#endif
+
+/*
Extern: magick_malloc, magick_free, magick_realloc
Purpose: ****Magick versions of standard memory routines.
Notes: use when managing memory that ****Magick may have
allocated or may free.
If malloc fails, it raises an exception
*/
void *magick_malloc(const size_t size)
{
void *ptr;
-#if defined(HAVE_ACQUIREMAGICKMEMORY)
ptr = AcquireMagickMemory(size);
-#else
- ptr = AcquireMemory(size);
-#endif
if (!ptr)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return ptr;
}
void magick_free(void *ptr)
{
-#if defined(HAVE_ACQUIREMAGICKMEMORY)
- RelinquishMagickMemory(ptr);
-#else
- void *v = ptr;
- LiberateMemory(&v);
-#endif
+ (void) RelinquishMagickMemory(ptr);
}
void *magick_realloc(void *ptr, const size_t size)
{
void *v;
-#if defined(HAVE_ACQUIREMAGICKMEMORY)
v = ResizeMagickMemory(ptr, size);
-#else
- v = ptr;
- ReacquireMemory(&v, size);
-#endif
if (!v)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return v;
@@ -77,11 +104,11 @@
its return value.
*/
void magick_clone_string(char **new_str, const char *str)
{
- CloneString(new_str, str);
+ (void) CloneString(new_str, str);
}
/*
@@ -159,11 +186,11 @@
/*
* Extern: rm_check_ary_len(ary, len)
* Purpose: raise exception if array too short
*/
void
-rm_check_ary_len(VALUE ary, int len)
+rm_check_ary_len(VALUE ary, long len)
{
if (RARRAY(ary)->len < len)
{
rb_raise(rb_eIndexError, "not enough elements in array - expecting %d, got %d",
len, RARRAY(ary)->len);
@@ -235,11 +262,11 @@
long pct_long;
char *pct_str, *end;
int not_num;
// Try to convert the argument to a number. If failure, sets not_num to non-zero.
- rb_protect(arg_is_number, arg, ¬_num);
+ (void) rb_protect(arg_is_number, arg, ¬_num);
if (not_num)
{
arg = rb_rescue(rb_str_to_str, arg, rescue_not_str, arg);
pct_str = STRING_PTR(arg);
@@ -285,11 +312,11 @@
Purpose: return 0 if rb_num2dbl doesn't raise an exception
*/
static VALUE
check_num2dbl(VALUE obj)
{
- rb_num2dbl(obj);
+ (void) rb_num2dbl(obj);
return INT2FIX(1);
}
/*
@@ -358,11 +385,11 @@
double fuzz;
char *fuzz_str, *end;
int not_num;
// Try to convert the argument to a number. If failure, sets not_num to non-zero.
- rb_protect(arg_is_number, fuzz_arg, ¬_num);
+ (void) rb_protect(arg_is_number, fuzz_arg, ¬_num);
if (not_num)
{
// Convert to string, issue error message if failure.
fuzz_arg = rb_rescue(rb_str_to_str, fuzz_arg, rescue_not_str, fuzz_arg);
@@ -460,11 +487,11 @@
Pixel *pixel;
char buff[100];
Data_Get_Struct(self, Pixel, pixel);
sprintf(buff, "red=%d, green=%d, blue=%d, opacity=%d"
- , pixel->red, pixel->green, pixel->blue, pixel->opacity);
+ , (int)pixel->red, (int)pixel->green, (int)pixel->blue, (int)pixel->opacity);
return rb_str_new2(buff);
}
/*
Method: Magick::Pixel.from_color(string)
@@ -485,11 +512,11 @@
class = class; // defeat "never referenced" message from icc
GetExceptionInfo(&exception);
okay = QueryColorDatabase(STRING_PTR(name), &pp, &exception);
CHECK_EXCEPTION()
- DestroyExceptionInfo(&exception);
+ (void) DestroyExceptionInfo(&exception);
if (!okay)
{
rb_raise(rb_eArgError, "invalid color name: %s", STRING_PTR(name));
}
@@ -551,16 +578,16 @@
info = CloneImageInfo(NULL);
image = AllocateImage(info);
image->depth = depth;
image->matte = matte;
- DestroyImageInfo(info);
+ (void) DestroyImageInfo(info);
GetExceptionInfo(&exception);
(void) QueryColorname(image, pixel, compliance, name, &exception);
- DestroyImage(image);
+ (void) DestroyImage(image);
CHECK_EXCEPTION()
- DestroyExceptionInfo(&exception);
+ (void) DestroyExceptionInfo(&exception);
// Always return a string, even if it's ""
return rb_str_new2(name);
}
@@ -670,21 +697,21 @@
image = AllocateImage(info);
if (!image)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
- DestroyImageInfo(info);
+ (void) DestroyImageInfo(info);
image->colorspace = colorspace;
image->fuzz = fuzz;
#if defined(HAVE_ISCOLORSIMILAR)
equal = IsColorSimilar(image, this, that);
#else
equal = FuzzyColorCompare(image, this, that);
#endif
- DestroyImage(image);
+ (void) DestroyImage(image);
#else
equal = FuzzyColorMatch(this, that, fuzz);
#endif
@@ -721,18 +748,19 @@
*/
VALUE
Pixel_intensity(VALUE self)
{
Pixel *pixel;
- unsigned long intensity;
+ Quantum intensity;
Data_Get_Struct(self, Pixel, pixel);
- intensity = (unsigned long)
- (0.299*pixel->red) + (0.587*pixel->green) + (0.114*pixel->blue);
+ intensity = RoundToQuantum((0.299*pixel->red)
+ + (0.587*pixel->green)
+ + (0.114*pixel->blue));
- return ULONG2NUM(intensity);
+ return ULONG2NUM((unsigned long) intensity);
}
/*
Methods: Pixel RGBA attribute accessors
@@ -924,11 +952,11 @@
pixel = ALLOC(Pixel);
memset(pixel, '\0', sizeof(Pixel));
dup = Data_Wrap_Struct(CLASS_OF(self), NULL, destroy_Pixel, pixel);
if (rb_obj_tainted(self))
{
- rb_obj_taint(dup);
+ (void) rb_obj_taint(dup);
}
return rb_funcall(dup, ID_initialize_copy, 1, self);
}
@@ -942,11 +970,11 @@
volatile VALUE clone;
clone = Pixel_dup(self);
if (OBJ_FROZEN(self))
{
- rb_obj_freeze(clone);
+ (void) rb_obj_freeze(clone);
}
return clone;
}
@@ -1016,11 +1044,11 @@
GetExceptionInfo(&exception);
(void) QueryColorname(image, color, X11Compliance, name, &exception);
CHECK_EXCEPTION()
- DestroyExceptionInfo(&exception);
+ (void) DestroyExceptionInfo(&exception);
return rb_str_new2(name);
}
/*
@@ -1043,16 +1071,16 @@
volatile VALUE color_name;
my_info = info ? info : CloneImageInfo(NULL);
image = AllocateImage(info);
- image->matte = False;
+ image->matte = MagickFalse;
color_name = PixelPacket_to_Color_Name(image, color);
- DestroyImage(image);
+ (void) DestroyImage(image);
if (!info)
{
- DestroyImageInfo(my_info);
+ (void) DestroyImageInfo(my_info);
}
return color_name;
}
@@ -1069,11 +1097,11 @@
ExceptionInfo exception;
GetExceptionInfo(&exception);
name = STRING_PTR(name_arg);
okay = QueryColorDatabase(name, color, &exception);
- DestroyExceptionInfo(&exception);
+ (void) DestroyExceptionInfo(&exception);
if (!okay)
{
rb_raise(rb_eArgError, "invalid color name %s", name);
}
}
@@ -1289,16 +1317,14 @@
ENUM_TO_NAME(SoftLightCompositeOp)
#endif
ENUM_TO_NAME(ColorizeCompositeOp)
ENUM_TO_NAME(CopyBlueCompositeOp)
ENUM_TO_NAME(CopyCompositeOp)
-#if defined(HAVE_COPYCYANCOMPOSITEOP) // CYMK added 5.5.7
ENUM_TO_NAME(CopyCyanCompositeOp)
ENUM_TO_NAME(CopyMagentaCompositeOp)
ENUM_TO_NAME(CopyYellowCompositeOp)
ENUM_TO_NAME(CopyBlackCompositeOp)
-#endif
ENUM_TO_NAME(CopyGreenCompositeOp)
ENUM_TO_NAME(CopyOpacityCompositeOp)
ENUM_TO_NAME(CopyRedCompositeOp)
ENUM_TO_NAME(DarkenCompositeOp)
#if defined(HAVE_DSTCOMPOSITEOP)
@@ -1518,10 +1544,13 @@
ENUM_TO_NAME(TrueColorType)
ENUM_TO_NAME(TrueColorMatteType)
ENUM_TO_NAME(ColorSeparationType)
ENUM_TO_NAME(ColorSeparationMatteType)
ENUM_TO_NAME(OptimizeType)
+#if defined(HAVE_PALETTEBILEVELMATTETYPE)
+ ENUM_TO_NAME(PaletteBilevelMatteType)
+#endif
}
}
@@ -1800,11 +1829,11 @@
members = rb_funcall(st, ID_values, 0);
m = rb_ary_entry(members, 0);
if (m != Qnil)
{
- CloneString((char **)&(ci->name), STRING_PTR(m));
+ (void) CloneString((char **)&(ci->name), STRING_PTR(m));
}
m = rb_ary_entry(members, 1);
if (m != Qnil)
{
VALUE_TO_ENUM(m, ci->compliance, ComplianceType);
@@ -1968,15 +1997,15 @@
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_class2name(CLASS_OF(sp)));
}
members = rb_funcall(sp, ID_values, 0);
m = rb_ary_entry(members, 0);
- pi->x = m == Qnil ? 0 : FIX2INT(m);
+ pi->x = m == Qnil ? 0.0 : NUM2DBL(m);
m = rb_ary_entry(members, 1);
- pi->y = m == Qnil ? 0 : FIX2INT(m);
+ pi->y = m == Qnil ? 0.0 : NUM2DBL(m);
m = rb_ary_entry(members, 2);
- pi->z = m == Qnil ? 0 : FIX2INT(m);
+ pi->z = m == Qnil ? 0.0 : NUM2DBL(m);
}
/*
Extern: PointInfo_to_Point(pp)
Purpose: Create a Magick::Point object from a PointInfo structure.
@@ -2002,13 +2031,13 @@
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_class2name(CLASS_OF(sp)));
}
members = rb_funcall(sp, ID_values, 0);
m = rb_ary_entry(members, 0);
- pi->x = m == Qnil ? 0 : FIX2INT(m);
+ pi->x = m == Qnil ? 0.0 : NUM2DBL(m);
m = rb_ary_entry(members, 1);
- pi->y = m == Qnil ? 0 : FIX2INT(m);
+ pi->y = m == Qnil ? 0.0 : NUM2DBL(m);
}
/*
@@ -2219,11 +2248,11 @@
name = rb_str_new2(ti->name);
family = rb_str_new2(ti->family);
style = StyleType_new(ti->style);
stretch = StretchType_new(ti->stretch);
- weight = INT2NUM(ti->weight);
+ weight = UINT2NUM(ti->weight);
description = ti->description ? rb_str_new2(ti->description) : Qnil;
encoding = ti->encoding ? rb_str_new2(ti->encoding) : Qnil;
foundry = ti->foundry ? rb_str_new2(ti->foundry) : Qnil;
format = ti->format ? rb_str_new2(ti->format) : Qnil;
@@ -2251,35 +2280,35 @@
members = rb_funcall(st, ID_values, 0);
m = rb_ary_entry(members, 0);
if (m != Qnil)
{
- CloneString((char **)&(ti->name), STRING_PTR(m));
+ (void) CloneString((char **)&(ti->name), STRING_PTR(m));
}
m = rb_ary_entry(members, 1);
if (m != Qnil)
{
- CloneString((char **)&(ti->description), STRING_PTR(m));
+ (void) CloneString((char **)&(ti->description), STRING_PTR(m));
}
m = rb_ary_entry(members, 2);
if (m != Qnil)
{
- CloneString((char **)&(ti->family), STRING_PTR(m));
+ (void) CloneString((char **)&(ti->family), STRING_PTR(m));
}
m = rb_ary_entry(members, 3); ti->style = m == Qnil ? 0 : FIX2INT(m);
m = rb_ary_entry(members, 4); ti->stretch = m == Qnil ? 0 : FIX2INT(m);
m = rb_ary_entry(members, 5); ti->weight = m == Qnil ? 0 : FIX2INT(m);
m = rb_ary_entry(members, 6);
if (m != Qnil)
- CloneString((char **)&(ti->encoding), STRING_PTR(m));
+ (void) CloneString((char **)&(ti->encoding), STRING_PTR(m));
m = rb_ary_entry(members, 7);
if (m != Qnil)
- CloneString((char **)&(ti->foundry), STRING_PTR(m));
+ (void) CloneString((char **)&(ti->foundry), STRING_PTR(m));
m = rb_ary_entry(members, 8);
if (m != Qnil)
- CloneString((char **)&(ti->format), STRING_PTR(m));
+ (void) CloneString((char **)&(ti->format), STRING_PTR(m));
}
/*
Static: destroy_TypeInfo
@@ -2653,20 +2682,20 @@
volatile VALUE super_argv[2];
volatile VALUE enumerators;
super_argv[0] = sym;
super_argv[1] = val;
- rb_call_super(2, (VALUE *)super_argv);
+ (void) rb_call_super(2, (VALUE *)super_argv);
if (rb_cvar_defined(CLASS_OF(self), ID_enumerators) != Qtrue)
{
RUBY18(rb_cvar_set(CLASS_OF(self), ID_enumerators, rb_ary_new(), 0));
RUBY16(rb_cvar_set(CLASS_OF(self), ID_enumerators, rb_ary_new()));
}
enumerators = rb_cvar_get(CLASS_OF(self), ID_enumerators);
- rb_ary_push(enumerators, self);
+ (void) rb_ary_push(enumerators, self);
return self;
}
@@ -2701,20 +2730,20 @@
if (rb_block_given_p())
{
for (x = 0; x < RARRAY(enumerators)->len; x++)
{
- rb_yield(rb_ary_entry(enumerators, x));
+ (void) rb_yield(rb_ary_entry(enumerators, x));
}
rv = class;
}
else
{
copy = rb_ary_new2(RARRAY(enumerators)->len);
for (x = 0; x < RARRAY(enumerators)->len; x++)
{
- rb_ary_push(copy, rb_ary_entry(enumerators, x));
+ (void) rb_ary_push(copy, rb_ary_entry(enumerators, x));
}
OBJ_FREEZE(copy);
rv = copy;
}
@@ -2900,15 +2929,15 @@
rm_write_temp_image(Image *image, char *tmpnam)
{
long registry_id;
registry_id = SetMagickRegistry(ImageRegistryType, image, sizeof(Image), &image->exception);
+ rm_check_image_exception(image, RetainOnError);
if (registry_id < 0)
{
rb_raise(rb_eRuntimeError, "SetMagickRegistry failed.");
}
- rm_check_image_exception(image, RetainOnError);
sprintf(tmpnam, "mpri:%ld", registry_id);
}
/*
@@ -2961,11 +2990,11 @@
mesg = rb_str_new2(msg);
extra = loc ? rb_str_new2(loc) : Qnil;
exc = rb_funcall(Class_ImageMagickError, ID_new, 2, mesg, extra);
- rb_funcall(rb_cObject, rb_intern("raise"), 1, exc);
+ (void) rb_funcall(rb_cObject, rb_intern("raise"), 1, exc);
}
/*
Method: ImageMagickError#initialize(msg, loc)
@@ -2990,12 +3019,12 @@
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 2)", argc);
}
- rb_call_super(super_argc, (VALUE *)super_argv);
- rb_iv_set(self, "@"MAGICK_LOC, extra);
+ (void) rb_call_super(super_argc, (VALUE *)super_argv);
+ (void) rb_iv_set(self, "@"MAGICK_LOC, extra);
return self;
}
@@ -3057,17 +3086,17 @@
{
Image *clone;
ExceptionInfo exception;
GetExceptionInfo(&exception);
- clone = CloneImage(image, 0, 0, True, &exception);
+ clone = CloneImage(image, 0, 0, MagickTrue, &exception);
if (!clone)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
rm_check_exception(&exception, clone, DestroyOnError);
- DestroyExceptionInfo(&exception);
+ (void) DestroyExceptionInfo(&exception);
return clone;
}
@@ -3265,11 +3294,11 @@
if (badboy)
{
rm_check_exception(&exception, imglist, retention);
}
- DestroyExceptionInfo(&exception);
+ (void) DestroyExceptionInfo(&exception);
}
/*
* Extern: rm_check_exception
@@ -3405,14 +3434,14 @@
sprintf(extra, "%.*s at %.*s:%lu", sizeof(function), function, sizeof(module), module, line);
#endif
extra[sizeof(extra)-1] = '\0';
- DestroyExceptionInfo(exception);
+ (void) DestroyExceptionInfo(exception);
rm_magick_error(msg, extra);
#else
- DestroyExceptionInfo(exception);
+ (void) DestroyExceptionInfo(exception);
rm_magick_error(msg, NULL);
#endif
}