Commit 39778159 authored by Scott Lembcke's avatar Scott Lembcke Committed by Scott Lembcke

Removed all references to array stubs. Arrays are now converted in place.

2006-06-30  Scott Lembcke <lemb0029@gnome.org>
	* ext/: Removed all references to array stubs. Arrays are now converted in
	  place.

	* ext/conversion.c: Array conversion changed to be done in place, not
	  usings stubs.

	* ext/conversion.c: int8arrays are now converted to Ruby strings. That way
	  they are more usable as data.

	* ext/rbgimpmain.c: Changes is ext/conversion.c no longer requires the
	  GimpParam arrays to be handled by the GC. They are now immediately
	  destroyed.

	* ext/: Removed Gimp::ParamRegion structs.
parent 3a1520cd
2006-06-30 Scott Lembcke <lemb0029@gnome.org>
* ext/: Removed all references to array stubs. Arrays are now converted in
place.
* ext/conversion.c: Array conversion changed to be done in place, not
usings stubs.
* ext/conversion.c: int8arrays are now converted to Ruby strings. That way
they are more usable as data.
* ext/rbgimpmain.c: Changes is ext/conversion.c no longer requires the
GimpParam arrays to be handled by the GC. They are now immediately
destroyed.
* ext/: Removed Gimp::ParamRegion structs.
2006-06-27 Scott Lembcke <lemb0029@gnome.org>
* gimp-lib/gimp.rb: Added #to_proc to PDB::Procedure.
......
......@@ -14,7 +14,6 @@ AM_CFLAGS = $(RUBY_CFLAGS) $(GIMP_CFLAGS)
lib_LTLIBRARIES = gimpext.la
gimpext_la_SOURCES = \
arraytypes.c \
conversion.c \
ids.c \
rbgimp.c \
......
#include <ruby.h>
#include <libgimp/gimp.h>
#include "rbgimp.h"
VALUE cInt32ArrayStub;
VALUE cInt16ArrayStub;
VALUE cInt8ArrayStub;
VALUE cFloatArrayStub;
VALUE cStringArrayStub;
static VALUE
rb_int32_array_stub_to_a (VALUE self,
VALUE rbnum)
{
gint32 *arr;
Data_Get_Struct(self, gint32, arr);
int num = NUM2INT(rbnum);
volatile VALUE result = rb_ary_new();
int i;
for(i=0; i<num; i++)
rb_ary_push(result, INT2NUM(arr[i]));
return result;
}
static VALUE
rb_int16_array_stub_to_a (VALUE self,
VALUE rbnum)
{
gint16 *arr;
Data_Get_Struct(self, gint16, arr);
int num = NUM2INT(rbnum);
volatile VALUE result = rb_ary_new();
int i;
for(i=0; i<num; i++)
rb_ary_push(result, INT2NUM(arr[i]));
return result;
}
static VALUE
rb_int8_array_stub_to_a (VALUE self,
VALUE rbnum)
{
gint8 *arr;
Data_Get_Struct(self, gint8, arr);
int num = NUM2INT(rbnum);
volatile VALUE result = rb_ary_new();
int i;
for(i=0; i<num; i++)
rb_ary_push(result, INT2NUM(arr[i]));
return result;
}
static VALUE
rb_float_array_stub_to_a (VALUE self,
VALUE rbnum)
{
gfloat *arr;
Data_Get_Struct(self, gfloat, arr);
int num = NUM2INT(rbnum);
volatile VALUE result = rb_ary_new();
int i;
for(i=0; i<num; i++)
rb_ary_push(result, rb_float_new(arr[i]));
return result;
}
static VALUE
rb_string_array_stub_to_a (VALUE self,
VALUE rbnum)
{
gchar **arr;
Data_Get_Struct(self, gchar*, arr);
int num = NUM2INT(rbnum);
volatile VALUE result = rb_ary_new();
int i;
for(i=0; i<num; i++)
rb_ary_push(result, rb_str_new2(arr[i]));
return result;
}
void Init_arraytypes(void)
{
cInt32ArrayStub = rb_define_class_under(mGimp, "Int32ArrayStub", rb_cObject);
rb_define_method(cInt32ArrayStub, "to_a", rb_int32_array_stub_to_a, 1);
cInt16ArrayStub = rb_define_class_under(mGimp, "Int16ArrayStub", rb_cObject);
rb_define_method(cInt16ArrayStub, "to_a", rb_int16_array_stub_to_a, 1);
cInt8ArrayStub = rb_define_class_under(mGimp, "Int8ArrayStub", rb_cObject);
rb_define_method(cInt8ArrayStub, "to_a", rb_int8_array_stub_to_a, 1);
cFloatArrayStub = rb_define_class_under(mGimp, "FloatArrayStub", rb_cObject);
rb_define_method(cFloatArrayStub, "to_a", rb_float_array_stub_to_a, 1);
cStringArrayStub = rb_define_class_under(mGimp, "StringArrayStub", rb_cObject);
rb_define_method(cStringArrayStub, "to_a", rb_string_array_stub_to_a, 1);
}
......@@ -7,72 +7,134 @@
#include "rbgimp.h"
static gint32
get_arr_size (const GimpParam *params,
int index)
{
const GimpParam *param = params + (index - 1);
if (index < 0 || param->type != GIMP_PDB_INT32)
rb_raise(rb_eTypeError, "Size parameter not given for array %d.", index);
return param->data.d_int32;
}
static VALUE
gimp_array2rb(const GimpParam *params,
int index)
{
const GimpParam *param = &params[index];
gint32 size = get_arr_size(params, index);
volatile VALUE result = rb_ary_new();
gint32 *int32arr;
gint16 *int16arr;
gdouble *floatarr;
gchar **stringarr;
int i;
switch(param->type)
{
case GIMP_PDB_INT32ARRAY:
int32arr = param->data.d_int32array;
for(i=0; i<size; i++)
rb_ary_push(result, INT2NUM(int32arr[i]));
break;
case GIMP_PDB_INT16ARRAY:
int16arr = param->data.d_int16array;
for(i=0; i<size; i++)
rb_ary_push(result, INT2NUM(int16arr[i]));
break;
case GIMP_PDB_FLOATARRAY:
floatarr = param->data.d_floatarray;
for(i=0; i<size; i++)
rb_ary_push(result, rb_float_new(floatarr[i]));
break;
case GIMP_PDB_STRINGARRAY:
stringarr = param->data.d_stringarray;
for(i=0; i<size; i++)
rb_ary_push(result, rb_str_new2(stringarr[i]));
break;
default:
rb_bug("Bad type value in gimp_array2rb()");
}
return result;
}
static VALUE
int8array2str(const GimpParam *params,
int index)
{
gint32 size = get_arr_size(params, index);
char *ptr = (char *)params[index].data.d_int8array;
return rb_str_new(ptr, size);
}
VALUE
GimpParam2rb (GimpParam param)
GimpParam2rb (const GimpParam *params,
int index)
{
GimpPDBArgType type = param.type;
const GimpParam *param = params + index;
GimpPDBArgType type = param->type;
volatile VALUE data;
switch(type)
switch (type)
{
case GIMP_PDB_INT32:
data = INT2NUM(param.data.d_int32);
data = INT2NUM(param->data.d_int32);
break;
case GIMP_PDB_INT16:
data = INT2NUM(param.data.d_int16);
data = INT2NUM(param->data.d_int16);
break;
case GIMP_PDB_INT8:
data = INT2NUM(param.data.d_int8);
data = INT2NUM(param->data.d_int8);
break;
case GIMP_PDB_FLOAT:
data = rb_float_new(param.data.d_float);
data = rb_float_new(param->data.d_float);
break;
case GIMP_PDB_STRING:
data = rb_str_new2(param.data.d_string);
data = rb_str_new2("the");//param->data.d_string);
break;
case GIMP_PDB_INT32ARRAY:
data = Data_Wrap_Struct(cInt32ArrayStub, NULL, NULL, param.data.d_int32array);
break;
case GIMP_PDB_INT16ARRAY:
data = Data_Wrap_Struct(cInt16ArrayStub, NULL, NULL, param.data.d_int16array);
break;
case GIMP_PDB_INT8ARRAY:
data = Data_Wrap_Struct(cInt8ArrayStub, NULL, NULL, param.data.d_int8array);
break;
case GIMP_PDB_FLOATARRAY:
data = Data_Wrap_Struct(cFloatArrayStub, NULL, NULL, param.data.d_floatarray);
case GIMP_PDB_STRINGARRAY:
data = gimp_array2rb(params, index);
break;
case GIMP_PDB_STRINGARRAY:
data = Data_Wrap_Struct(cStringArrayStub, NULL, NULL, param.data.d_stringarray);
case GIMP_PDB_INT8ARRAY:
data = int8array2str(params, index);
break;
case GIMP_PDB_COLOR:
data = GimpRGB2rb(&param.data.d_color);
data = GimpRGB2rb(&param->data.d_color);
break;
case GIMP_PDB_REGION:
data = GimpParamRegion2rb(&param.data.d_region);
break;
// case GIMP_PDB_REGION:
// data = GimpParamRegion2rb(&param->data.d_region);
// break;
case GIMP_PDB_DISPLAY:
data = INT2NUM(param.data.d_display);
data = INT2NUM(param->data.d_display);
break;
case GIMP_PDB_IMAGE:
data = INT2NUM(param.data.d_image);
data = INT2NUM(param->data.d_image);
break;
case GIMP_PDB_LAYER:
data = INT2NUM(param.data.d_layer);
data = INT2NUM(param->data.d_layer);
break;
// case GIMP_PDB_LAYER_MASK:
......@@ -80,23 +142,23 @@ GimpParam2rb (GimpParam param)
// break;
case GIMP_PDB_CHANNEL:
data = INT2NUM(param.data.d_channel);
data = INT2NUM(param->data.d_channel);
break;
case GIMP_PDB_DRAWABLE:
data = INT2NUM(param.data.d_drawable);
data = INT2NUM(param->data.d_drawable);
break;
case GIMP_PDB_SELECTION:
data = INT2NUM(param.data.d_selection);
data = INT2NUM(param->data.d_selection);
break;
case GIMP_PDB_BOUNDARY:
data = INT2NUM(param.data.d_boundary);
data = INT2NUM(param->data.d_boundary);
break;
case GIMP_PDB_VECTORS:
data = INT2NUM(param.data.d_vectors);
data = INT2NUM(param->data.d_vectors);
break;
// case GIMP_PDB_UNIT:
......@@ -113,14 +175,14 @@ GimpParam2rb (GimpParam param)
// break;
case GIMP_PDB_STATUS:
data = INT2NUM(param.data.d_status);
data = INT2NUM(param->data.d_status);
break;
default:
rb_bug("Parameter type %d does not exist.\n", param.type);
rb_bug("Parameter type %d does not exist.\n", param->type);
}
return rb_struct_new(sGimpParam, INT2NUM(type), data);
return rb_struct_new(sGimpParam, INT2NUM(type), data);
}
VALUE
......@@ -128,26 +190,17 @@ GimpParams2rb (const GimpParam *params,
int count)
{
volatile VALUE ary = rb_ary_new();
int i;
for(i=0; i<count; i++)
rb_ary_push(ary, GimpParam2rb(params[i]));
rb_ary_push(ary, GimpParam2rb(params, i));
return ary;
}
static gchar*
rbcopystr(VALUE rbstr)
{
char *str = StringValuePtr(rbstr);
gchar *result = g_malloc(strlen(str) + 1);
strcpy(result, str);
return str;
}
static gpointer
convert_array(GimpPDBArgType type,
rb2gimp_array(GimpPDBArgType type,
VALUE rbarr)
{
rbarr = rb_check_array_type(rbarr);
......@@ -157,7 +210,6 @@ convert_array(GimpPDBArgType type,
int i;
gint32 *int32arr;
gint16 *int16arr;
gint8 *int8arr;
gdouble *floatarr;
gchar **stringarr;
......@@ -172,12 +224,7 @@ convert_array(GimpPDBArgType type,
int16arr = g_new(gint16, count);
for(i=0; i<count; i++) int16arr[i] = (gint16)NUM2INT(arr[i]);
return int16arr;
case GIMP_PDB_INT8ARRAY:
int8arr = g_new(gint8, count);
for(i=0; i<count; i++) int8arr[i] = (gint8)NUM2INT(arr[i]);
return int8arr;
case GIMP_PDB_FLOATARRAY:
floatarr = g_new(gdouble, count);
for(i=0; i<count; i++) floatarr[i] = (gdouble)NUM2DBL(arr[i]);
......@@ -185,13 +232,22 @@ convert_array(GimpPDBArgType type,
case GIMP_PDB_STRINGARRAY:
stringarr = g_new(gchar *, count);
for(i=0; i<count; i++) stringarr[i] = rbcopystr(arr[i]);
for(i=0; i<count; i++) stringarr[i] = g_strdup(StringValuePtr(arr[i]));
return stringarr;
default:
rb_bug("Bad type value in convert_array()");
}
}
static gint8 *
str2gint8(VALUE rb_str)
{
Check_Type(rb_str, T_STRING);
struct RString *str = RSTRING(rb_str);
return g_memdup(str->ptr, str->len);
}
GimpParam
rb2GimpParam (VALUE rbparam)
{
......@@ -223,36 +279,36 @@ rb2GimpParam (VALUE rbparam)
break;
case GIMP_PDB_STRING:
result.data.d_string = rbcopystr(rbdata);
result.data.d_string = g_strdup(StringValuePtr(rbdata));
break;
case GIMP_PDB_INT32ARRAY:
result.data.d_int32array = convert_array(type, rbdata);
result.data.d_int32array = rb2gimp_array(type, rbdata);
break;
case GIMP_PDB_INT16ARRAY:
result.data.d_int16array = convert_array(type, rbdata);
result.data.d_int16array = rb2gimp_array(type, rbdata);
break;
case GIMP_PDB_INT8ARRAY:
result.data.d_int8array = convert_array(type, rbdata);
result.data.d_int8array = str2gint8(rbdata);
break;
case GIMP_PDB_FLOATARRAY:
result.data.d_floatarray = convert_array(type, rbdata);
result.data.d_floatarray = rb2gimp_array(type, rbdata);
break;
case GIMP_PDB_STRINGARRAY:
result.data.d_stringarray = convert_array(type, rbdata);
result.data.d_stringarray = rb2gimp_array(type, rbdata);
break;
case GIMP_PDB_COLOR:
result.data.d_color = rb2GimpRGB(rbdata);
break;
case GIMP_PDB_REGION:
/* case GIMP_PDB_REGION:
result.data.d_region = rb2GimpParamRegion(rbdata);
break;
break;*/
case GIMP_PDB_DISPLAY:
result.data.d_display = (gint32)NUM2INT(rbdata);
......@@ -367,7 +423,7 @@ rb2GimpParamDef (VALUE param)
result.name = StringValuePtr(name);
result.description = StringValuePtr(dscr);
// printf("%d %s %s\n", result.type, result.name, result.description);
// printf("%d %s %s\n", result.type, result.name, result.description); //PRINTF
return result;
}
......@@ -392,6 +448,8 @@ rb2GimpParamDefs (VALUE rbparamdefs,
int i;
for(i=0; i<num; i++)
gimpparamdefs[i] = rb2GimpParamDef(arr[i]);
*count = (gint)num;
return gimpparamdefs;
......@@ -399,38 +457,7 @@ rb2GimpParamDefs (VALUE rbparamdefs,
}
VALUE
GimpParamRegion2rb (GimpParamRegion *region)
{
volatile VALUE x = INT2NUM(region->x);
volatile VALUE y = INT2NUM(region->y);
volatile VALUE w = INT2NUM(region->width);
volatile VALUE h = INT2NUM(region->height);
return rb_struct_new(sGimpParamRegion, x, y, w, h, NULL);
}
GimpParamRegion
rb2GimpParamRegion(VALUE region)
{
if(!rb_obj_is_kind_of(region, sGimpParamRegion))
rb_raise(rb_eArgError, "Parameters must be of type Gimp::ParamRegion");
VALUE x = rb_struct_aref(region, ID2SYM(id_x));
VALUE y = rb_struct_aref(region, ID2SYM(id_y));
VALUE width = rb_struct_aref(region, ID2SYM(id_width));
VALUE height = rb_struct_aref(region, ID2SYM(id_height));
GimpParamRegion result;
result.x = NUM2INT(x);
result.y = NUM2INT(y);
result.width = NUM2INT(width);
result.height = NUM2INT(height);
return result;
}
VALUE
GimpRGB2rb (GimpRGB *color)
GimpRGB2rb (const GimpRGB *color)
{
GimpRGB *tmp = malloc(sizeof(GimpRGB));
*tmp = *color;
......@@ -455,7 +482,7 @@ rb2GimpRGB (VALUE color)
}
VALUE
GimpHSV2rb (GimpHSV *color)
GimpHSV2rb (const GimpHSV *color)
{
GimpHSV *tmp = ALLOC(GimpHSV);
*tmp = *color;
......@@ -480,7 +507,7 @@ rb2GimpHSV (VALUE color)
}
VALUE
GimpCMYK2rb (GimpCMYK *color)
GimpCMYK2rb (const GimpCMYK *color)
{
GimpCMYK *tmp = ALLOC(GimpCMYK);
*tmp = *color;
......
......@@ -29,8 +29,4 @@ Init_ids (void)
id_query_proc = rb_intern("query_proc");
id_run_proc = rb_intern("run_proc");
id_call = rb_intern("call");
id_x = rb_intern("x");
id_y = rb_intern("y");
id_width = rb_intern("width");
id_height = rb_intern("height");
}
......@@ -131,8 +131,6 @@ void Init_gimpext(void)
Init_structures();
Init_ids();
Init_arraytypes();
Init_gimpconstants();
Init_gimpmain();
......
......@@ -6,22 +6,17 @@ extern VALUE mGimp;
extern VALUE cGimpRGB;
extern VALUE cGimpHSV;
extern VALUE cGimpCMYK;
extern VALUE cInt32ArrayStub;
extern VALUE cInt16ArrayStub;
extern VALUE cInt8ArrayStub;
extern VALUE cFloatArrayStub;
extern VALUE cStringArrayStub;
//structures (structures.c)
extern VALUE sGimpParamDef;
extern VALUE sGimpParam;
extern VALUE sGimpPlugInInfo;
extern VALUE sGimpParamRegion;
//Type conversion functions (conversion.c)
VALUE GimpParam2rb (GimpParam param);
VALUE GimpParam2rb (const GimpParam *param,
int index);
VALUE GimpParams2rb (const GimpParam *params,
int count);
GimpParam rb2GimpParam (VALUE rbparam);
......@@ -35,20 +30,17 @@ GimpParamDef rb2GimpParamDef (VALUE param);
GimpParamDef *rb2GimpParamDefs (VALUE rbparamdefs,
int *count);
VALUE GimpParamRegion2rb (GimpParamRegion *region);
GimpParamRegion rb2GimpParamRegion (VALUE region);
VALUE GimpRGB2rb (const GimpRGB *color);
GimpRGB *rb2GimpRGBPtr (VALUE color);
GimpRGB rb2GimpRGB (VALUE color);
VALUE GimpRGB2rb (GimpRGB *color);
GimpRGB *rb2GimpRGBPtr (VALUE color);
GimpRGB rb2GimpRGB (VALUE color);
VALUE GimpHSV2rb (const GimpHSV *color);
GimpHSV *rb2GimpHSVPtr (VALUE color);
GimpHSV rb2GimpHSV (VALUE color);
VALUE GimpHSV2rb (GimpHSV *color);
GimpHSV *rb2GimpHSVPtr (VALUE color);
GimpHSV rb2GimpHSV (VALUE color);
VALUE GimpCMYK2rb (GimpCMYK *color);
GimpCMYK *rb2GimpCMYKPtr (VALUE color);
GimpCMYK rb2GimpCMYK (VALUE color);
VALUE GimpCMYK2rb (const GimpCMYK *color);
GimpCMYK *rb2GimpCMYKPtr (VALUE color);
GimpCMYK rb2GimpCMYK (VALUE color);
//ids (ids.c)
......@@ -61,16 +53,11 @@ extern ID id_quit_proc;
extern ID id_query_proc;
extern ID id_run_proc;
extern ID id_call;
extern ID id_x;
extern ID id_y;
extern ID id_width;
extern ID id_height;
//Init functions
void Init_structures(void);
void Init_ids(void);
void Init_arraytypes(void);
void Init_gimpconstants(void);
void Init_gimpmain(void);
......
......@@ -241,6 +241,7 @@ rb_gimp_install_procedure (VALUE self,
}
//TODO temp proc functions
// Are they needed?
static VALUE
rb_gimp_run_procedure (VALUE self,
......@@ -261,8 +262,8 @@ rb_gimp_run_procedure (VALUE self,
rbreturn_vals = GimpParams2rb(return_vals, n_return_vals);
gimp_destroy_params(params, n_params);
gc_register_params(return_vals, n_return_vals);
// gimp_destroy_params(return_vals, n_return_vals);
// gc_register_params(return_vals, n_return_vals);
gimp_destroy_params(return_vals, n_return_vals);
return rbreturn_vals;
}
......
......@@ -3,6 +3,8 @@
#include "rbgimp.h"
//TODO a lot of these function might as well be used from the pdb.
// figure out which ones.
static VALUE
rb_gimp_procedural_db_temp_name (VALUE self)
{
......
......@@ -22,11 +22,4 @@ void Init_structures(void)
"query_proc",
"run_proc", NULL);
rb_define_const(mGimp, "PlugInInfo", sGimpPlugInInfo);
sGimpParamRegion = rb_struct_define("GimpParamRegion",
"x",
"y",
"width",
"height");
rb_define_const(mGimp, "ParamRegion", sGimpParamRegion);
}
......@@ -20,11 +20,10 @@ module Gimp
:STRING => :to_str,
:INT32ARRAY => :to_ary,
:INT16ARRAY => :to_ary,
:INT8ARRAY => :to_ary,
:INT8ARRAY => :to_str,
:FLOATARRAY => :to_ary,
:STRINGARRAY => :to_ary,
:COLOR => Gimp::Rgb,
:REGION => Gimp::ParamRegion,
:DISPLAY => :to_int,
:IMAGE => :to_int,
:LAYER => :to_int,
......@@ -160,9 +159,14 @@ module PDB
raise(ArgumentError, message)
end
result = args.zip(paramdefs).collect do|arg, paramdef|
#TODO No type checking!
Gimp::Param.new(paramdef.type, arg)
begin
result = args.zip(paramdefs).collect do|arg, paramdef|
paramdef.check(arg)
Gimp::Param.new(paramdef.type, arg)
end
rescue TypeError
message = "Bad Argument: #{$!.message}"
raise(TypeError, message)
end
end
......
......@@ -158,9 +158,9 @@ module RubyFu
result.check(value)
Gimp::Param.new(result.type, value)
end
rescue
#FIXME more informative handling would be good.
raise "return value type check failed?"
rescue TypeError
message = "Procedure return value type check failed: #{$!.message}"
raise(TypeError, message)
end
return values
......
......@@ -4,13 +4,17 @@ Track down the corrupted string issue that occurs in coolmetal-logo.rb.
Parasite argument types.
Handle parameter definitions/defaults better in RubyFu.
Make sure GINT8 params are handled correctly as uints.
Finish OO Gimp types.
Make sure all the useful parts of the C API were wrapped.
The color code has turned out to be sub-par, lots of room for improvment.
The color code has turned out to be sub-par, lots of room for improvment.
Region parameters seem to have issues in GIMP. Should they just be removed?
Remove // style comments.
Signal handlers might need to be backed up. (see how the old one did this. Is it still necessary?)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment