Class: Oil::PNGReader

Inherits:
Object
  • Object
show all
Defined in:
ext/oil/png.c

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/oil/png.c', line 85

static VALUE initialize(VALUE self, VALUE io)
{
  struct readerdata *reader;

  Data_Get_Struct(self, struct readerdata, reader);

  if (reader->source_io) {
    png_destroy_read_struct(&reader->png, &reader->info, NULL);
    reader->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr)error, (png_error_ptr)warning);
    reader->locked = 0;
  }

  reader->source_io = io;
  reader->info = png_create_info_struct(reader->png);
  png_set_read_fn(reader->png, (void*)io, read_data);
  png_read_info(reader->png, reader->info);
  return self;
}

Instance Method Details

#each(opts, &block) ⇒ self

Yields a series of binary strings that make up the output JPEG image.

Options is a hash which may have the following symbols:

:quality - JPEG quality setting. Betweein 0 and 100. :markers - Custom markers to include in the output JPEG. Must be a hash where

the keys are :APP[0-15] or :COM and the values are arrays of strings that
will be inserted into the markers.

Returns:

  • (self)


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'ext/oil/png.c', line 261

static VALUE each(int argc, VALUE *argv, VALUE self)
{
  struct readerdata *reader;
  int cmp, state;
  struct write_png_args args;
  unsigned char *inwidthbuf, *outwidthbuf;
  struct yscaler ys;
  VALUE opts;
  png_structp png;
  png_infop info;

  rb_scan_args(argc, argv, "01", &opts);

  Data_Get_Struct(self, struct readerdata, reader);

  raise_if_locked(reader);
  reader->locked = 1;

  png_set_packing(reader->png);
  png_set_strip_16(reader->png);
  png_set_expand(reader->png);
  png_read_update_info(reader->png, reader->info);

  if (!reader->scale_width) {
    reader->scale_width = png_get_image_width(reader->png, reader->info);
  }
  if (!reader->scale_height) {
    reader->scale_height = png_get_image_height(reader->png, reader->info);
  }

  png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
    (png_error_ptr)error, (png_error_ptr)warning);
  info = png_create_info_struct(png);

  inwidthbuf = malloc(png_get_rowbytes(reader->png, reader->info));
  cmp = png_get_channels(reader->png, reader->info);
  outwidthbuf = malloc(reader->scale_width * cmp);
  yscaler_init(&ys, png_get_image_height(reader->png, reader->info),
    reader->scale_height, reader->scale_width * cmp);

  args.reader = reader;
  args.opts = opts;
  args.png = png;
  args.info = info;
  args.inwidthbuf = inwidthbuf;
  args.outwidthbuf = outwidthbuf;
  args.ys = &ys;
  rb_protect((VALUE(*)(VALUE))each2, (VALUE)&args, &state);

  yscaler_free(&ys);
  free(inwidthbuf);
  free(outwidthbuf);
  png_destroy_write_struct(&png, &info);

  if (state) {
    rb_jump_tag(state);
  }

  return self;
}

#heightNumeric

Retrieve the height of the image.

Returns:

  • (Numeric)


125
126
127
128
129
130
# File 'ext/oil/png.c', line 125

static VALUE height(VALUE self)
{
  struct readerdata *reader;
  Data_Get_Struct(self, struct readerdata, reader);
  return INT2FIX(png_get_image_height(reader->png, reader->info));
}

#scale_heightNumeric

Retrieve the height to which the image will be resized after decompression. A height of 0 means the image will remain at original height.

Returns:

  • (Numeric)


172
173
174
175
176
177
# File 'ext/oil/png.c', line 172

static VALUE scale_height(VALUE self)
{
  struct readerdata *reader;
  Data_Get_Struct(self, struct readerdata, reader);
  return INT2FIX(reader->scale_height);
}

#scale_height=(number) ⇒ Object

Set the height to which the image will be resized after decompression. A height of 0 means the image will remain at original height.



187
188
189
190
191
192
193
194
# File 'ext/oil/png.c', line 187

static VALUE set_scale_height(VALUE self, VALUE scale_height)
{
  struct readerdata *reader;
  Data_Get_Struct(self, struct readerdata, reader);
  raise_if_locked(reader);
  reader->scale_height = NUM2INT(scale_height);
  return scale_height;
}

#scale_widthNumeric

Retrieve the width to which the image will be resized after decompression. A width of 0 means the image will remain at original width.

Returns:

  • (Numeric)


140
141
142
143
144
145
# File 'ext/oil/png.c', line 140

static VALUE scale_width(VALUE self)
{
  struct readerdata *reader;
  Data_Get_Struct(self, struct readerdata, reader);
  return INT2FIX(reader->scale_width);
}

#scale_width=(number) ⇒ Object

Set the width to which the image will be resized after decompression. A width of 0 means the image will remain at original width.



155
156
157
158
159
160
161
162
# File 'ext/oil/png.c', line 155

static VALUE set_scale_width(VALUE self, VALUE scale_width)
{
  struct readerdata *reader;
  Data_Get_Struct(self, struct readerdata, reader);
  raise_if_locked(reader);
  reader->scale_width = NUM2INT(scale_width);
  return scale_width;
}

#widthNumeric

Retrieve the width of the image.

Returns:

  • (Numeric)


111
112
113
114
115
116
# File 'ext/oil/png.c', line 111

static VALUE width(VALUE self)
{
  struct readerdata *reader;
  Data_Get_Struct(self, struct readerdata, reader);
  return INT2FIX(png_get_image_width(reader->png, reader->info));
}