Class: Axon::JPEG::Reader

Inherits:
Object
  • Object
show all
Defined in:
ext/axon/jpeg.c,
ext/axon/jpeg.c

Overview

Read compressed JPEG images from an IO.

Instance Method Summary collapse

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])


601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'ext/axon/jpeg.c', line 601

static VALUE
initialize(int argc, VALUE *argv, VALUE self)
{
    struct readerdata *reader;
    j_decompress_ptr cinfo;
    VALUE io, markers;

    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.

Returns:

  • (Array)


1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'ext/axon/jpeg.c', line 1015

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_modelObject

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.



693
694
695
696
697
698
699
700
701
702
703
# File 'ext/axon/jpeg.c', line 693

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



716
717
718
719
720
721
722
723
724
725
726
# File 'ext/axon/jpeg.c', line 716

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

#componentsNumeric

Retrieve the number of components as stored in the JPEG image.

Returns:

  • (Numeric)


629
630
631
632
633
634
635
# File 'ext/axon/jpeg.c', line 629

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_methodObject

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.



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'ext/axon/jpeg.c', line 822

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.



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'ext/axon/jpeg.c', line 847

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

#exifString

Get the raw Exif data from the JPEG. This requires that the APP1 segment has been selected by initialize (this is the default behavior).

Returns:

  • (String)


985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'ext/axon/jpeg.c', line 985

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

#getsString?

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.

Returns:

  • (String, nil)


875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'ext/axon/jpeg.c', line 875

static VALUE
j_gets(VALUE self)
{
    struct readerdata *reader;
    struct jpeg_decompress_struct *cinfo;
    VALUE sl;
    int sl_width, ret;
    JSAMPROW ijg_buffer;

    Data_Get_Struct(self, struct readerdata, reader);
    cinfo = &reader->cinfo;

    if (!reader->header_read)
      read_header(reader, Qnil);

    if (!reader->decompress_started) {
	reader->decompress_started = 1;
	jpeg_start_decompress(cinfo);
    }

    sl_width = cinfo->output_width * cinfo->output_components;
    sl = rb_str_new(0, sl_width);
    ijg_buffer = (JSAMPROW)RSTRING_PTR(sl);

    ret = jpeg_read_scanlines(cinfo, &ijg_buffer, 1);
    return ret == 0 ? Qnil : sl;
}

#heightNumeric

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.

Returns:

  • (Numeric)


927
928
929
930
931
932
933
# File 'ext/axon/jpeg.c', line 927

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_profileString

Read the raw icc_profile from the JPEG. This requires that the APP2 segment has been selected by initialize (this is the default behaviour).

Returns:

  • (String)


943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'ext/axon/jpeg.c', line 943

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_modelObject

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.



650
651
652
653
654
655
656
657
658
659
660
# File 'ext/axon/jpeg.c', line 650

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.



670
671
672
673
674
675
676
677
678
679
680
# File 'ext/axon/jpeg.c', line 670

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

#linenoNumeric

Returns the number of the next line to be read from the image, starting at 0.

Returns:

  • (Numeric)


1070
1071
1072
1073
1074
1075
1076
# File 'ext/axon/jpeg.c', line 1070

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_markerBoolean

Indicates whether an Adobe marker was found in the header.

Returns:

  • (Boolean)


1054
1055
1056
1057
1058
1059
1060
# File 'ext/axon/jpeg.c', line 1054

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_markerBoolean

Indicates whether a JFIF marker was found in the header.

Returns:

  • (Boolean)


1039
1040
1041
1042
1043
1044
1045
# File 'ext/axon/jpeg.c', line 1039

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_denomNumeric

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.

Returns:

  • (Numeric)


779
780
781
782
783
784
785
# File 'ext/axon/jpeg.c', line 779

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.



799
800
801
802
803
804
805
806
807
808
809
810
# File 'ext/axon/jpeg.c', line 799

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_numNumeric

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.

Returns:

  • (Numeric)


737
738
739
740
741
742
743
# File 'ext/axon/jpeg.c', line 737

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.



757
758
759
760
761
762
763
764
765
766
767
768
# File 'ext/axon/jpeg.c', line 757

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

#widthNumeric

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.

Returns:

  • (Numeric)


911
912
913
914
915
916
917
# File 'ext/axon/jpeg.c', line 911

static VALUE
width(VALUE self)
{
    struct jpeg_decompress_struct * cinfo;
    Data_Get_Struct(self, struct jpeg_decompress_struct, cinfo);
    return INT2FIX(cinfo->output_width);
}