Class: Axon::JPEGReader
- Inherits:
-
Object
- Object
- Axon::JPEGReader
- Includes:
- Image, Enumerable
- Defined in:
- ext/axon/jpeg_reader.c
Instance Method Summary collapse
-
#[](marker) ⇒ Array
Read raw data from the given JPEG header marker.
-
#color_model ⇒ Object
Returns a symbol representing the color space into which the JPEG will be transformed as it is read.
-
#color_model=(symbol) ⇒ Object
Explicitly sets the color space to which the JPEG will be transformed as it is read.
-
#num_components ⇒ Numeric
Retrieve the number of components as stored in the JPEG image.
-
#dct_method ⇒ Object
Returns a symbol representing the algorithm used for the DCT (discrete cosine transform) step in JPEG encoding.
-
#dct_method=(symbol) ⇒ Object
Sets the algorithm used for the DCT step in JPEG encoding.
-
#each_scanline(&block) ⇒ Object
Iterate over each decoded scanline in the JPEG image.
-
#exif ⇒ String
Read the Exif data from the JPEG.
-
#height ⇒ Numeric
Retrieve the height of the image as it will be written out.
-
#icc_profile ⇒ String
Read the icc_profile from the JPEG.
-
#in_color_model ⇒ Object
Returns a symbol representing the color space in which the JPEG is stored.
-
#color_space=(symbol) ⇒ Object
Explicitly sets the color space the JPEG will be read in.
-
#new(io[, markers, rewind_after_scanlines]) ⇒ Object
constructor
Create a new JPEG Reader.
-
#saw_adobe_marker ⇒ Boolean
Indicate that an Adobe marker was found in the header.
-
#saw_jfif_marker ⇒ Boolean
Indicate that a JFIF marker was found in the header.
-
#scale_denom ⇒ Numeric
Retrieve the denominator of the fraction by which the JPEG will be scaled as it is read.
-
#scale_denom=(number) ⇒ Object
Set the denominator of the fraction by which the JPEG will be scaled as it is read.
-
#scale_num ⇒ Numeric
Retrieve the numerator of the fraction by which the JPEG will be scaled as it is read.
-
#scale_num=(number) ⇒ Object
Set the numerator of the fraction by which the JPEG will be scaled as it is read.
-
#width ⇒ Numeric
Retrieve the width of the image as it will be written out.
Methods included from Image
#crop, #fit, #scale_bilinear, #scale_nearest_neighbor, #to_jpeg, #to_png, #write_jpeg, #write_png
Constructor Details
#new(io[, markers, rewind_after_scanlines]) ⇒ Object
Create a new JPEG Reader. string_or_io may be an object that responds to read or a string.
markers should be an array of valid JPEG header marker symbols. Valid symbols are :APP0 through :APP15 and :COM.
If performance is important, you can avoid reading all header markers by supplying an empty array, [].
When markers are not specified, we read all known JPEG markers.
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/axon/jpeg_reader.c', line 185
static VALUE
initialize(int argc, VALUE *argv, VALUE self)
{
struct readerdata *reader;
j_decompress_ptr cinfo;
VALUE io, markers, rewind_after_scanlines;
int i;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
cinfo = &reader->cinfo;
rb_scan_args(argc, argv, "12", &io, &markers, &rewind_after_scanlines);
reader->source_io = io;
reader->mgr.bytes_in_buffer = 0;
if(NIL_P(markers)) {
jpeg_save_markers(cinfo, JPEG_COM, 0xFFFF);
for (i = 0; i < 16; i++)
jpeg_save_markers(cinfo, JPEG_APP0 + i, 0xFFFF);
}
reader->rewind_after_scanlines = RTEST(rewind_after_scanlines);
read_header(reader, markers);
return self;
}
|
Instance Method Details
#[](marker) ⇒ Array
Read raw data from the given JPEG header marker. Note that the marker must have been specified by Reader#save_markers prior to the call to Reader#read_header.
The return from this method is an array, since there may be multiple instances of a single marker in a JPEG header.
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'ext/axon/jpeg_reader.c', line 680
static VALUE
aref(VALUE self, VALUE marker_sym)
{
struct jpeg_decompress_struct * cinfo;
jpeg_saved_marker_ptr marker;
VALUE ary = rb_ary_new();
int marker_i = sym_to_marker_code(marker_sym);
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next)
if (marker->marker == marker_i)
rb_ary_push(ary, rb_str_new(marker->data, marker->data_length));
return ary;
}
|
#color_model ⇒ Object
Returns a symbol representing the color space into which the JPEG will be transformed as it is read.
By default this color space is based on Reader#color_space.
Possible color spaces are: GRAYSCALE, RGB, YCbCr, CMYK, and YCCK. This method will return nil if the color space is not recognized.
286 287 288 289 290 291 292 293 294 295 296 |
# File 'ext/axon/jpeg_reader.c', line 286
static VALUE
color_model(VALUE self)
{
ID id;
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
id = j_color_space_to_id(cinfo->out_color_space);
return ID2SYM(id);
}
|
#color_model=(symbol) ⇒ Object
Explicitly sets the color space to which the JPEG will be transformed as it is read.
Legal transformations are: YCbCr to GRAYSCALE, YCbCr to RGB, GRAYSCALE to RGB, and YCCK to CMYK
308 309 310 311 312 313 314 315 316 317 318 |
# File 'ext/axon/jpeg_reader.c', line 308
static VALUE
set_color_model(VALUE self, VALUE cs)
{
struct readerdata *reader;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
reader->cinfo.out_color_space = id_to_j_color_space(SYM2ID(cs));
return cs;
}
|
#num_components ⇒ Numeric
Retrieve the number of components as stored in the JPEG image.
221 222 223 224 225 226 227 228 229 |
# File 'ext/axon/jpeg_reader.c', line 221
static VALUE
components(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->num_components);
}
|
#dct_method ⇒ Object
Returns a symbol representing the algorithm used for the DCT (discrete cosine transform) step in JPEG encoding.
Possible DCT algorithms are: ISLOW, IFAST, and IFLOAT.
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'ext/axon/jpeg_reader.c', line 428
static VALUE
dct_method(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
ID id;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
id = j_dct_method_to_id(cinfo->dct_method);
if (NIL_P(id))
return Qnil;
else
return ID2SYM(id);
}
|
#dct_method=(symbol) ⇒ Object
Sets the algorithm used for the DCT step in JPEG encoding.
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'ext/axon/jpeg_reader.c', line 450
static VALUE
set_dct_method(VALUE self, VALUE dct_method)
{
struct readerdata *reader;
J_DCT_METHOD val;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
val = id_to_j_dct_method(SYM2ID(dct_method));
if (val == (J_DCT_METHOD)NULL) {
return Qnil;
} else {
reader->cinfo.dct_method = val;
return dct_method;
}
}
|
#each_scanline(&block) ⇒ Object
Iterate over each decoded scanline in the JPEG image. During this operation the reader is locked, and you can’t change any decoding parameters or re initialize the reader.
Should a major exception occur (anything other than a StandardError), then the reader will be left in a locked state.
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'ext/axon/jpeg_reader.c', line 542
static VALUE
each(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
struct readerdata *reader;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
if (!reader->header_read)
read_header(reader, Qnil);
reader->locked = 1;
jpeg_start_decompress(&reader->cinfo);
rb_ensure(each2, (VALUE)reader, each2_ensure, (VALUE)reader);
return self;
}
|
#exif ⇒ String
Read the Exif data from the JPEG. This requires that the APP1 segment has been selected by save_markers (this is the default behaviour).
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'ext/axon/jpeg_reader.c', line 650
static VALUE
exif(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
jpeg_saved_marker_ptr marker;
int len;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (marker_is_exif(marker)) {
len = marker->data_length - EXIF_OVERHEAD_LEN;
return rb_str_new(marker->data + EXIF_OVERHEAD_LEN, len);
}
}
return Qnil;
}
|
#height ⇒ Numeric
Retrieve the height of the image as it will be written out. This is primarily affected by scale_num and scale_denom if they are set.
Note that this value is not automatically calculated unless you call Reader#calc_output_dimensions or after Reader#each_scanline has been called.
592 593 594 595 596 597 598 599 600 |
# File 'ext/axon/jpeg_reader.c', line 592
static VALUE
height(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->output_height);
}
|
#icc_profile ⇒ String
Read the icc_profile from the JPEG. This requires that the APP2 segment has been selected by save_markers (this is the default behaviour).
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'ext/axon/jpeg_reader.c', line 609
static VALUE
icc_profile(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
JOCTET *icc_embed_buffer;
unsigned int icc_embed_len;
VALUE str;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
read_icc_profile(cinfo, &icc_embed_buffer, &icc_embed_len);
if (icc_embed_len <= 0) {
return Qnil;
} else {
str = rb_str_new(icc_embed_buffer, icc_embed_len);
free(icc_embed_buffer);
return str;
}
}
|
#in_color_model ⇒ Object
Returns a symbol representing the color space in which the JPEG is stored.
This does not have to be set explicitly and can be relied upon when the file conforms to JFIF or Adobe conventions. Otherwise it is guessed.
Possible color spaces are: GRAYSCALE, RGB, YCbCr, CMYK, and YCCK. This method will return nil if the color space is not recognized.
243 244 245 246 247 248 249 250 251 252 253 |
# File 'ext/axon/jpeg_reader.c', line 243
static VALUE
in_color_model(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
ID id;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
id = j_color_space_to_id(cinfo->jpeg_color_space);
return ID2SYM(id);
}
|
#color_space=(symbol) ⇒ Object
Explicitly sets the color space the JPEG will be read in. This will override the guessed color space.
262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/axon/jpeg_reader.c', line 262
static VALUE
set_in_color_model(VALUE self, VALUE cs)
{
struct readerdata *reader;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
reader->cinfo.jpeg_color_space = id_to_j_color_space(SYM2ID(cs));
return cs;
}
|
#saw_adobe_marker ⇒ Boolean
Indicate that an Adobe marker was found in the header.
719 720 721 722 723 724 725 726 727 |
# File 'ext/axon/jpeg_reader.c', line 719
static VALUE
saw_adobe_marker(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return cinfo->saw_Adobe_marker ? Qtrue : Qfalse;
}
|
#saw_jfif_marker ⇒ Boolean
Indicate that a JFIF marker was found in the header.
703 704 705 706 707 708 709 710 711 |
# File 'ext/axon/jpeg_reader.c', line 703
static VALUE
saw_jfif_marker(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return cinfo->saw_JFIF_marker ? Qtrue : Qfalse;
}
|
#scale_denom ⇒ Numeric
Retrieve the denominator of the fraction by which the JPEG will be scaled as it is read. This is 1, 2, 4, or 8 for libjpeg version 6b. In version 8b this is always the source DCT size, which is 8 for baseline JPEG.
366 367 368 369 370 371 372 373 374 |
# File 'ext/axon/jpeg_reader.c', line 366
static VALUE
scale_denom(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->scale_denom);
}
|
#scale_denom=(number) ⇒ Object
Set the denominator of the fraction by which the JPEG will be scaled as it is read. This can be set to 1, 2, 4, or 8 for libjpeg version 6b. In version 8b this must always be the source DCT size, which is 8 for baseline JPEG.
384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'ext/axon/jpeg_reader.c', line 384
static VALUE
set_scale_denom(VALUE self, VALUE scale_denom)
{
struct readerdata *reader;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
reader->cinfo.scale_denom = FIX2INT(scale_denom);
jpeg_calc_output_dimensions(&reader->cinfo);
return scale_denom;
}
|
#scale_num ⇒ Numeric
Retrieve the numerator of the fraction by which the JPEG will be scaled as it is read. This is always 1 for libjpeg version 6b. In version 8b this can be 1 to 16.
328 329 330 331 332 333 334 335 |
# File 'ext/axon/jpeg_reader.c', line 328
static VALUE
scale_num(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->scale_num);
}
|
#scale_num=(number) ⇒ Object
Set the numerator of the fraction by which the JPEG will be scaled as it is read. This must always be 1 for libjpeg version 6b. In version 8b this can be set to 1 through 16.
345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'ext/axon/jpeg_reader.c', line 345
static VALUE
set_scale_num(VALUE self, VALUE scale_num)
{
struct readerdata *reader;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
reader->cinfo.scale_num = FIX2INT(scale_num);
jpeg_calc_output_dimensions(&reader->cinfo);
return scale_num;
}
|
#width ⇒ Numeric
Retrieve the width of the image as it will be written out. This is primarily affected by scale_num and scale_denom if they are set.
Note that this value is not automatically calculated unless you call Reader#calc_output_dimensions or after Reader#each_scanline has been called.
572 573 574 575 576 577 578 579 580 |
# File 'ext/axon/jpeg_reader.c', line 572
static VALUE
width(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->output_width);
}
|