Module: ImageRuby::ImageRubyCMixin

Defined in:
lib/imageruby-c.rb,
ext/imagecruby_base/imagecruby_base.c

Instance Method Summary collapse

Instance Method Details

#c_color_replace(rb_color1, rb_color2) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/imagecruby_base/imagecruby_base.c', line 28

VALUE c_color_replace(VALUE self, VALUE rb_color1, VALUE rb_color2) {
  VALUE rb_color1_string = rb_funcall(rb_color1, id_to_s, 0);
  const char* color1_string = RSTRING(rb_color1_string)->ptr;

  VALUE rb_color2_string = rb_funcall(rb_color2, id_to_s, 0);
  const char* color2_string = RSTRING(rb_color2_string)->ptr;

  int color2_alpha = FIX2INT( rb_funcall(rb_color2, id_a, 0) );

  int self_width = FIX2INT( rb_funcall(self, id_width, 0) );
  int self_height = FIX2INT( rb_funcall(self, id_height, 0) );

  VALUE rb_self_pixel_data = rb_funcall(self, id_pixel_data, 0);
  char* self_pixel_data = RSTRING(rb_self_pixel_data)->ptr;

  VALUE rb_self_alpha_data = rb_funcall(self, id_alpha_data, 0);
  char* self_alpha_data = RSTRING(rb_self_alpha_data)->ptr;

  int y_;
  int x_;
  for (y_ = 0; y_ < self_height; y_++) {

    for (x_ = 0; x_ < self_width; x_++) {

      if (
        self_pixel_data[x_*3] == color1_string[0] &&
        self_pixel_data[x_*3+1] == color1_string[1] &&
        self_pixel_data[x_*3+2] == color1_string[2]
        ) {

        self_pixel_data[x_*3] = color2_string[0];
        self_pixel_data[x_*3+1] = color2_string[1];
        self_pixel_data[x_*3+2] = color2_string[2];
        self_alpha_data[x_] = color2_alpha;
      }
    }

    self_alpha_data += self_width;
    self_pixel_data += (self_width * 3);

  }

  return self;
}

#c_draw(rb_x, rb_y, rb_image) ⇒ Object

VALUE c_draw_with_mask(VALUE self, VALUE rb_x, VALUE rb_y, VALUE rb_image, VALUE rb_mask_color) {

int x = FIX2INT(rb_x); int y = FIX2INT(rb_y);

int image_width = FIX2INT( rb_funcall(rb_image, id_width, 0) ); int image_height = FIX2INT( rb_funcall(rb_image, id_height, 0) );

int self_width = FIX2INT( rb_funcall(self, id_width, 0) ); int self_height = FIX2INT( rb_funcall(self, id_height, 0) );

VALUE rb_color_string = rb_funcall(rb_mask_color, id_to_s, 0); const char* color_string = RSTRING(rb_color_string)->ptr;

VALUE rb_image_pixel_data = rb_funcall(rb_image, id_pixel_data, 0); const char* image_pixel_data = RSTRING(rb_image_pixel_data)->ptr;

VALUE rb_self_pixel_data = rb_funcall(self, id_pixel_data, 0); char* self_pixel_data = RSTRING(rb_self_pixel_data)->ptr;

self_pixel_data = (x y * self_width) * 3;

int y_; int x_; for (y_ = 0; y_ < image_height; y_++) {

for (x_ = 0; x_ < image_width; x_++) {

if ( image_pixel_data != color_string || image_pixel_data != color_string || image_pixel_data != color_string ) {

self_pixel_data = image_pixel_data; self_pixel_data = image_pixel_data; self_pixel_data = image_pixel_data; } }

self_pixel_data = (self_width * 3); image_pixel_data = (image_width * 3);

}

return self;

}



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/imagecruby_base/imagecruby_base.c', line 122

VALUE c_draw(VALUE self, VALUE rb_x, VALUE rb_y, VALUE rb_image) {

  int x = FIX2INT(rb_x);
  int y = FIX2INT(rb_y);

  int image_width = FIX2INT( rb_funcall(rb_image, id_width, 0) );
  int image_height = FIX2INT( rb_funcall(rb_image, id_height, 0) );

  int self_width = FIX2INT( rb_funcall(self, id_width, 0) );
  int self_height = FIX2INT( rb_funcall(self, id_height, 0) );

  VALUE rb_image_pixel_data = rb_funcall(rb_image, id_pixel_data, 0);
  const unsigned char* image_pixel_data = RSTRING(rb_image_pixel_data)->ptr;

  VALUE rb_self_pixel_data = rb_funcall(self, id_pixel_data, 0);
  unsigned char* self_pixel_data = RSTRING(rb_self_pixel_data)->ptr;

  VALUE rb_image_alpha_data = rb_funcall(rb_image, id_alpha_data, 0);
  unsigned char* image_alpha_data = RSTRING(rb_image_alpha_data)->ptr;

  self_pixel_data += (x + y * self_width) * 3;

  int included_image_width = image_width;

  if (x + included_image_width > self_width) {
    included_image_width = self_width - x;
  }

  int y_;
  int x_;
  for (y_ = 0; y_ < image_height; y_++) {

    for (x_ = 0; x_ < included_image_width; x_++) {

      unsigned int alpha = image_alpha_data[x_];

      if (alpha < 255) {
        if (alpha > 0) {
          self_pixel_data[x_*3] = ( self_pixel_data[x_*3]*(255-alpha) + image_pixel_data[x_*3]*alpha ) / 255;
          self_pixel_data[x_*3+1] = ( self_pixel_data[x_*3+1]*(255-alpha) + image_pixel_data[x_*3+1]*alpha ) / 255;
          self_pixel_data[x_*3+2] = ( self_pixel_data[x_*3+2]*(255-alpha) + image_pixel_data[x_*3+2]*alpha ) / 255;
        }
      } else {
        self_pixel_data[x_*3] = image_pixel_data[x_*3];
        self_pixel_data[x_*3+1] = image_pixel_data[x_*3+1];
        self_pixel_data[x_*3+2] = image_pixel_data[x_*3+2];
      }
    }

    self_pixel_data += (self_width * 3);
    image_alpha_data += image_width;
    image_pixel_data += (image_width * 3);

  }

  return self;
}

#color_replace!(color1, color2) ⇒ Object



30
31
32
# File 'lib/imageruby-c.rb', line 30

def color_replace!(color1, color2)
  c_color_replace(color1, color2)
end

#draw!(x, y, image) ⇒ Object



26
27
28
# File 'lib/imageruby-c.rb', line 26

def draw!(x,y,image)
  c_draw(x,y,image)
end