Class: Axon::JPEG::Reader
- Inherits:
-
Object
- Object
- Axon::JPEG::Reader
- Defined in:
- ext/axon/jpeg.c,
ext/axon/jpeg.c
Overview
Read compressed JPEG images from an IO.
Instance Method Summary collapse
-
#[](marker) ⇒ Array
Read raw data from the given JPEG header marker.
-
#color_model ⇒ Object
Returns a symbol representing the color model into which the JPEG will be transformed as it is read.
-
#color_model=(symbol) ⇒ Object
Explicitly sets the color model to which the JPEG will be transformed as it is read.
-
#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.
-
#exif ⇒ String
Get the raw Exif data from the JPEG.
-
#gets ⇒ String?
Reads the next scanline of data from the image.
-
#height ⇒ Numeric
Retrieve the height of the image as it will be written out.
-
#icc_profile ⇒ String
Read the raw icc_profile from the JPEG.
-
#in_color_model ⇒ Object
Returns a symbol representing the color model in which the JPEG is stored.
-
#in_color_model=(symbol) ⇒ Object
Explicitly sets the color model the JPEG will be read in.
-
#new(io_in[, markers]) ⇒ Object
constructor
Creates a new JPEG Reader.
-
#lineno ⇒ Numeric
Returns the number of the next line to be read from the image, starting at 0.
-
#saw_adobe_marker ⇒ Boolean
Indicates whether an Adobe marker was found in the header.
-
#saw_jfif_marker ⇒ Boolean
Indicates whether 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.
Constructor Details
#new(io_in[, markers]) ⇒ Object
Creates a new JPEG Reader. io_in
must be an IO-like object that responds to read(size).
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 any header markers by supplying an empty array, [].
When markers are not specified, we read all known JPEG markers.
io = File.open("image.jpg", "r")
reader = Axon::JPEG::Reader.new(io)
io = File.open("image.jpg", "r")
reader = Axon::JPEG::Reader.new(io, [:APP4, :APP5])
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'ext/axon/jpeg.c', line 621
static VALUE
initialize(int argc, VALUE *argv, VALUE self)
{
struct readerdata *reader;
j_decompress_ptr cinfo;
VALUE io, markers;
int i;
Data_Get_Struct(self, struct readerdata, reader);
raise_if_locked(reader);
cinfo = &reader->cinfo;
rb_scan_args(argc, argv, "11", &io, &markers);
reader->source_io = io;
reader->mgr.bytes_in_buffer = 0;
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 initialize.
The return from this method is an array, since there may be multiple instances of a single marker in a JPEG header.
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 |
# File 'ext/axon/jpeg.c', line 1049
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 model into which the JPEG will be transformed as it is read.
Possible color models are: :GRAYSCALE, :RGB, :YCbCr, :CMYK, and :YCCK. This method will return nil if the color model is not recognized.
714 715 716 717 718 719 720 721 722 723 724 |
# File 'ext/axon/jpeg.c', line 714
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 model 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
737 738 739 740 741 742 743 744 745 746 747 |
# File 'ext/axon/jpeg.c', line 737
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;
}
|
#components ⇒ Numeric
Retrieve the number of components as stored in the JPEG image.
650 651 652 653 654 655 656 |
# File 'ext/axon/jpeg.c', line 650
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.
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 |
# File 'ext/axon/jpeg.c', line 843
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.
Possible DCT algorithms are: :ISLOW, :IFAST, and :IFLOAT.
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 |
# File 'ext/axon/jpeg.c', line 868
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;
}
}
|
#exif ⇒ String
Get the raw Exif data from the JPEG. This requires that the APP1 segment has been selected by initialize (this is the default behavior).
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
# File 'ext/axon/jpeg.c', line 1019
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;
}
|
#gets ⇒ String?
Reads the next scanline of data from the image. Once the first scanline has been read you can no longer change read options for this reader.
If the end of the image has been reached, this will return nil.
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 |
# File 'ext/axon/jpeg.c', line 918
static VALUE
j_gets(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
struct readerdata *reader;
Data_Get_Struct(self, struct readerdata, reader);
if (!reader->header_read)
read_header(reader, Qnil);
if (reader->locked == 0) {
reader->locked = 1;
jpeg_start_decompress(&reader->cinfo);
}
return j_gets2(reader);
}
|
#height ⇒ Numeric
Retrieve the height of the image as it will be written out. This can be affected by scale_num and scale_denom if they are set.
961 962 963 964 965 966 967 |
# File 'ext/axon/jpeg.c', line 961
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 raw icc_profile from the JPEG. This requires that the APP2 segment has been selected by initialize (this is the default behaviour).
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 |
# File 'ext/axon/jpeg.c', line 977
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 model 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 models are: :GRAYSCALE, :RGB, :YCbCr, :CMYK, and :YCCK. This method will return nil if the color model is not recognized.
671 672 673 674 675 676 677 678 679 680 681 |
# File 'ext/axon/jpeg.c', line 671
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);
}
|
#in_color_model=(symbol) ⇒ Object
Explicitly sets the color model the JPEG will be read in. This will override the guessed color model.
691 692 693 694 695 696 697 698 699 700 701 |
# File 'ext/axon/jpeg.c', line 691
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;
}
|
#lineno ⇒ Numeric
Returns the number of the next line to be read from the image, starting at 0.
1104 1105 1106 1107 1108 1109 1110 |
# File 'ext/axon/jpeg.c', line 1104
static VALUE
lineno(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->output_scanline);
}
|
#saw_adobe_marker ⇒ Boolean
Indicates whether an Adobe marker was found in the header.
1088 1089 1090 1091 1092 1093 1094 |
# File 'ext/axon/jpeg.c', line 1088
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
Indicates whether a JFIF marker was found in the header.
1073 1074 1075 1076 1077 1078 1079 |
# File 'ext/axon/jpeg.c', line 1073
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.
800 801 802 803 804 805 806 |
# File 'ext/axon/jpeg.c', line 800
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.
Prior to version 1.2, libjpeg-turbo will not scale down images on decompression, and this option will do nothing.
820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'ext/axon/jpeg.c', line 820
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 = NUM2INT(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.
758 759 760 761 762 763 764 |
# File 'ext/axon/jpeg.c', line 758
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.
Prior to version 1.2, libjpeg-turbo will not scale down images on decompression, and this option will do nothing.
778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'ext/axon/jpeg.c', line 778
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 = NUM2INT(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 can be affected by scale_num and scale_denom if they are set.
945 946 947 948 949 950 951 |
# File 'ext/axon/jpeg.c', line 945
static VALUE
width(VALUE self)
{
struct jpeg_decompress_struct * cinfo;
Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
return INT2FIX(cinfo->output_width);
}
|