Class: Oil::PNGReader

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

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/oil/png.c', line 92

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

	Data_Get_Struct(self, struct readerdata, reader);

	if (reader->info) {
		png_destroy_read_struct(&reader->png, &reader->info, NULL);
		allocate2(reader);
		reader->locked = 0;
	}

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

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

	reader->scale_width = png_get_image_width(reader->png, reader->info);
	reader->scale_height = png_get_image_height(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)


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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'ext/oil/png.c', line 296

static VALUE each(int argc, VALUE *argv, VALUE self)
{
	struct readerdata *reader;
	png_infop winfo;
	png_structp wpng;
	VALUE opts;
	int cmp, state;
	struct each_args args;
	uint32_t i, height;
	png_byte ctype;
	unsigned char **scanlines;
	size_t row_bytes;

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

	Data_Get_Struct(self, struct readerdata, reader);

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

	wpng = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
		(png_error_ptr)error, (png_error_ptr)warning);
	winfo = png_create_info_struct(wpng);
	png_set_write_fn(wpng, 0, write_data_fn, flush_data_fn);

	cmp = png_get_channels(reader->png, reader->info);
	ctype = png_get_color_type(reader->png, reader->info);

	png_set_IHDR(wpng, winfo, reader->scale_width, reader->scale_height, 8,
		ctype, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
		PNG_FILTER_TYPE_DEFAULT);

	height = png_get_image_height(reader->png, reader->info);
	row_bytes = png_get_rowbytes(reader->png, reader->info);

	args.reader = reader;
	args.wpng = wpng;
	args.winfo = winfo;
	args.inwidthbuf = malloc(row_bytes);
	args.outwidthbuf = malloc(reader->scale_width * cmp);

	if (png_get_interlace_type(reader->png, reader->info) == PNG_INTERLACE_NONE) {
		yscaler_init(&args.ys, height, reader->scale_height,
			reader->scale_width * cmp);
		rb_protect((VALUE(*)(VALUE))each_interlace_none, (VALUE)&args, &state);
		yscaler_free(&args.ys);
	} else {
		scanlines = malloc(height * sizeof(unsigned char *));
		for (i=0; i<height; i++) {
			scanlines[i] = malloc(row_bytes);
		}

		args.scanlines = scanlines;
		rb_protect((VALUE(*)(VALUE))each_interlace, (VALUE)&args, &state);

		for (i=0; i<height; i++) {
			free(scanlines[i]);
		}
		free(scanlines);
	}

	free(args.inwidthbuf);
	free(args.outwidthbuf);
	png_destroy_write_struct(&wpng, &winfo);

	if (state) {
		rb_jump_tag(state);
	}

	return self;
}

#heightNumeric

Retrieve the height of the image.

Returns:

  • (Numeric)


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

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)


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

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.



201
202
203
204
205
206
207
208
# File 'ext/oil/png.c', line 201

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)


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

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.



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

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)


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

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));
}