Module: OilyPNG::Resampling

Included in:
Canvas
Defined in:
ext/oily_png/oily_png_ext.c

Instance Method Summary collapse

Instance Method Details

#resample_bilinear!(v_new_width, v_new_height) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/oily_png/resampling.c', line 155

VALUE oily_png_canvas_resample_bilinear_bang(VALUE self, VALUE v_new_width, VALUE v_new_height) {
  long new_width = NUM2LONG(v_new_width);
  long new_height = NUM2LONG(v_new_height);

  long self_width = NUM2LONG(rb_funcall(self, rb_intern("width"), 0));
  long self_height = NUM2LONG(rb_funcall(self, rb_intern("height"), 0));

  VALUE pixels = rb_ary_new2(new_width*new_height);
  VALUE source = rb_iv_get(self, "@pixels");

  long *index_x = ALLOC_N(long, new_width);
  long *index_y = ALLOC_N(long, new_height);
  long *interp_x = ALLOC_N(long, new_width);
  long *interp_y = ALLOC_N(long, new_height);

  oily_png_generate_steps_residues(self_width, new_width, index_x, interp_x);
  oily_png_generate_steps_residues(self_height, new_height, index_y, interp_y);

  long index = 0;
  long x, y;
  long y1, y2, x1, x2;
  PIXEL y_residue, x_residue;
  PIXEL pixel_11, pixel_21, pixel_12, pixel_22;
  PIXEL pixel_top, pixel_bot;
  for (y = 0; y < new_height; y++) {
    y1 = index_y[y] < 0 ? 0 : index_y[y];
    y2 = index_y[y]+1 >= self_height ? self_height-1 : index_y[y]+1;
    y_residue = interp_y[y];

    for (x = 0; x < new_width; x++) {
      x1 = index_x[x] < 0 ? 0 : index_x[x];
      x2 = index_x[x]+1 >= self_width ? self_width-1 : index_x[x]+1;
      x_residue = interp_x[x];

      pixel_11 = NUM2UINT(rb_ary_entry(source, y1*self_width + x1));
      pixel_21 = NUM2UINT(rb_ary_entry(source, y1*self_width + x2));
      pixel_12 = NUM2UINT(rb_ary_entry(source, y2*self_width + x1));
      pixel_22 = NUM2UINT(rb_ary_entry(source, y2*self_width + x2));

      pixel_top = oily_png_color_interpolate_quick(pixel_21, pixel_11, x_residue);
      pixel_bot = oily_png_color_interpolate_quick(pixel_22, pixel_12, x_residue);

      rb_ary_store(pixels, index++, UINT2NUM(oily_png_color_interpolate_quick(pixel_bot, pixel_top, y_residue)));
    }
  }

  xfree(index_x);
  xfree(index_y);
  xfree(interp_x);
  xfree(interp_y);
  interp_x = NULL;
  interp_y = NULL;

  rb_iv_set(self, "@pixels", pixels);
  rb_iv_set(self, "@width", LONG2NUM(new_width));
  rb_iv_set(self, "@height", LONG2NUM(new_height));

  return self;
}

#resample_nearest_neighbor!(v_new_width, v_new_height) ⇒ Object



114
115
116
117
118
119
120
121
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
# File 'ext/oily_png/resampling.c', line 114

VALUE oily_png_canvas_resample_nearest_neighbor_bang(VALUE self, VALUE v_new_width, VALUE v_new_height) {
  long new_width = NUM2LONG(v_new_width);
  long new_height = NUM2LONG(v_new_height);

  long self_width = NUM2LONG(rb_funcall(self, rb_intern("width"), 0));
  long self_height = NUM2LONG(rb_funcall(self, rb_intern("height"), 0));

  VALUE pixels = rb_ary_new2(new_width*new_height);
  VALUE source = rb_iv_get(self, "@pixels");

  long *steps_x = ALLOC_N(long, new_width);
  long *steps_y = ALLOC_N(long, new_height);

  oily_png_generate_steps_residues(self_width, new_width, steps_x, NULL);
  oily_png_generate_steps_residues(self_height, new_height, steps_y, NULL);

  long index = 0;
  long x, y;
  long src_index;
  for (y=0; y < new_height; y++) {
    for (x = 0; x < new_width; x++) {
      src_index = steps_y[y] * self_width + steps_x[x];
      VALUE pixel = rb_ary_entry(source, src_index);
      rb_ary_store(pixels, index, pixel);
      index++;
    }
  }

  xfree(steps_x);
  steps_x = NULL;

  xfree(steps_y);
  steps_y = NULL;

  rb_iv_set(self, "@pixels", pixels);
  rb_iv_set(self, "@width", LONG2NUM(new_width));
  rb_iv_set(self, "@height", LONG2NUM(new_height));

  return self;
}