oil 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/Rakefile +7 -3
- data/ext/oil/jpeg.c +25 -49
- data/ext/oil/oil.c +4 -3
- data/ext/oil/oil_libjpeg.c +104 -0
- data/ext/oil/oil_libjpeg.h +57 -0
- data/ext/oil/oil_libpng.c +161 -0
- data/ext/oil/oil_libpng.h +58 -0
- data/ext/oil/oil_resample.c +1012 -0
- data/ext/oil/oil_resample.h +149 -0
- data/ext/oil/png.c +22 -100
- data/lib/oil.rb +1 -1
- metadata +9 -6
- data/ext/oil/resample.c +0 -938
- data/ext/oil/resample.h +0 -119
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43f7b2521cd984aa636d4d3f55d72b25b3e58f99ddfbaf1fa52dff9dc3e9369d
|
4
|
+
data.tar.gz: 753964c879c07214b84cba0ea49e629e4bff83e490999bfe19fef0fc82c2ce91
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb401d8f26646728db46c67c887e040fe04faf06dacb49f19738102be2defc0b872745dd7166d55a9bf64fb174e7bf9976032e690ed713b5c66fde3ea47f5bb2
|
7
|
+
data.tar.gz: a0620f7f9e1e6c8e1688c2e4c5cbb1c8934613f48c83b5a41b463eacfa24afafb721ae62d5dc1fb8b273c2b6c77de4b293d197bc40da92ffe8f8e73d6875829a
|
data/MIT-LICENSE
CHANGED
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ Rake::ExtensionTask.new('oil') do |ext|
|
|
6
6
|
ext.lib_dir = 'lib/oil'
|
7
7
|
end
|
8
8
|
|
9
|
-
s = Gem::Specification.new('oil', '0.2.
|
9
|
+
s = Gem::Specification.new('oil', '0.2.1') do |s|
|
10
10
|
s.license = 'MIT'
|
11
11
|
s.summary = 'Resize JPEG and PNG images.'
|
12
12
|
s.description = 'Resize JPEG and PNG images, aiming for fast performance and low memory use.'
|
@@ -17,8 +17,12 @@ s = Gem::Specification.new('oil', '0.2.0') do |s|
|
|
17
17
|
README.rdoc
|
18
18
|
MIT-LICENSE
|
19
19
|
lib/oil.rb
|
20
|
-
ext/oil/
|
21
|
-
ext/oil/
|
20
|
+
ext/oil/oil_resample.c
|
21
|
+
ext/oil/oil_resample.h
|
22
|
+
ext/oil/oil_libjpeg.c
|
23
|
+
ext/oil/oil_libjpeg.h
|
24
|
+
ext/oil/oil_libpng.c
|
25
|
+
ext/oil/oil_libpng.h
|
22
26
|
ext/oil/jpeg.c
|
23
27
|
ext/oil/png.c
|
24
28
|
ext/oil/oil.c
|
data/ext/oil/jpeg.c
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
#include <ruby/st.h>
|
3
3
|
#include <jpeglib.h>
|
4
|
-
#include "
|
4
|
+
#include "oil_libjpeg.h"
|
5
5
|
|
6
6
|
#define READ_SIZE 1024
|
7
7
|
#define WRITE_SIZE 1024
|
@@ -170,8 +170,8 @@ struct readerdata {
|
|
170
170
|
int locked;
|
171
171
|
VALUE source_io;
|
172
172
|
VALUE buffer;
|
173
|
-
|
174
|
-
|
173
|
+
int scale_width;
|
174
|
+
int scale_height;
|
175
175
|
};
|
176
176
|
|
177
177
|
static void null_jdecompress(j_decompress_ptr dinfo) {}
|
@@ -754,8 +754,7 @@ struct write_jpeg_args {
|
|
754
754
|
struct writerdata *writer;
|
755
755
|
unsigned char *inwidthbuf;
|
756
756
|
unsigned char *outwidthbuf;
|
757
|
-
struct
|
758
|
-
struct preprocess_xscaler xs;
|
757
|
+
struct oil_libjpeg ol;
|
759
758
|
};
|
760
759
|
|
761
760
|
static VALUE each2(struct write_jpeg_args *args)
|
@@ -763,12 +762,13 @@ static VALUE each2(struct write_jpeg_args *args)
|
|
763
762
|
struct writerdata *writer;
|
764
763
|
struct jpeg_decompress_struct *dinfo;
|
765
764
|
struct jpeg_compress_struct *cinfo;
|
766
|
-
unsigned char *outwidthbuf
|
767
|
-
|
768
|
-
uint32_t i, scalex, scaley;
|
765
|
+
unsigned char *outwidthbuf;
|
766
|
+
int i, scalex, scaley;
|
769
767
|
VALUE quality, markers;
|
768
|
+
struct oil_libjpeg *ol;
|
770
769
|
|
771
770
|
writer = args->writer;
|
771
|
+
ol = &args->ol;
|
772
772
|
outwidthbuf = args->outwidthbuf;
|
773
773
|
dinfo = &args->reader->dinfo;
|
774
774
|
cinfo = &writer->cinfo;
|
@@ -784,8 +784,6 @@ static VALUE each2(struct write_jpeg_args *args)
|
|
784
784
|
writer->cinfo.in_color_space = dinfo->out_color_space;
|
785
785
|
writer->cinfo.input_components = dinfo->output_components;
|
786
786
|
|
787
|
-
xinbuf = args->inwidthbuf;
|
788
|
-
|
789
787
|
jpeg_set_defaults(cinfo);
|
790
788
|
|
791
789
|
if (!NIL_P(args->opts)) {
|
@@ -806,12 +804,8 @@ static VALUE each2(struct write_jpeg_args *args)
|
|
806
804
|
}
|
807
805
|
}
|
808
806
|
|
809
|
-
for(i=
|
810
|
-
|
811
|
-
jpeg_read_scanlines(dinfo, (JSAMPARRAY)&xinbuf, 1);
|
812
|
-
preprocess_xscaler_scale(&args->xs, xinbuf, yinbuf);
|
813
|
-
}
|
814
|
-
yscaler_scale(&args->ys, outwidthbuf, i);
|
807
|
+
for(i=scaley; i>0; i--) {
|
808
|
+
oil_libjpeg_read_scanline(ol, outwidthbuf);
|
815
809
|
jpeg_write_scanlines(cinfo, (JSAMPARRAY)&outwidthbuf, 1);
|
816
810
|
}
|
817
811
|
|
@@ -820,24 +814,6 @@ static VALUE each2(struct write_jpeg_args *args)
|
|
820
814
|
return Qnil;
|
821
815
|
}
|
822
816
|
|
823
|
-
static enum oil_colorspace jpeg_cs_to_oil(J_COLOR_SPACE cs)
|
824
|
-
{
|
825
|
-
switch(cs) {
|
826
|
-
case JCS_GRAYSCALE:
|
827
|
-
return OIL_CS_G;
|
828
|
-
case JCS_RGB:
|
829
|
-
return OIL_CS_RGB;
|
830
|
-
case JCS_CMYK:
|
831
|
-
return OIL_CS_CMYK;
|
832
|
-
#ifdef JCS_EXTENSIONS
|
833
|
-
case JCS_EXT_RGBX:
|
834
|
-
return OIL_CS_RGBX;
|
835
|
-
#endif
|
836
|
-
default:
|
837
|
-
rb_raise(rb_eRuntimeError, "Color space not recognized.");
|
838
|
-
}
|
839
|
-
}
|
840
|
-
|
841
817
|
/*
|
842
818
|
* call-seq:
|
843
819
|
* reader.each(opts, &block) -> self
|
@@ -856,12 +832,10 @@ static VALUE each(int argc, VALUE *argv, VALUE self)
|
|
856
832
|
{
|
857
833
|
struct readerdata *reader;
|
858
834
|
struct writerdata writer;
|
859
|
-
int state;
|
835
|
+
int state, width_out, ret;
|
860
836
|
struct write_jpeg_args args;
|
861
|
-
unsigned char *
|
862
|
-
uint32_t width_in, width_out;
|
837
|
+
unsigned char *outwidthbuf;
|
863
838
|
VALUE opts;
|
864
|
-
enum oil_colorspace cs;
|
865
839
|
|
866
840
|
rb_scan_args(argc, argv, "01", &opts);
|
867
841
|
|
@@ -877,27 +851,29 @@ static VALUE each(int argc, VALUE *argv, VALUE self)
|
|
877
851
|
writer.cinfo.err = &reader->jerr;
|
878
852
|
jpeg_create_compress(&writer.cinfo);
|
879
853
|
|
880
|
-
cs = jpeg_cs_to_oil(reader->dinfo.out_color_space);
|
881
|
-
width_in = reader->dinfo.output_width;
|
882
854
|
width_out = reader->scale_width;
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
855
|
+
ret = oil_libjpeg_init(&args.ol, &reader->dinfo, width_out,
|
856
|
+
reader->scale_height);
|
857
|
+
if (ret!=0) {
|
858
|
+
jpeg_destroy_compress(&writer.cinfo);
|
859
|
+
rb_raise(rb_eRuntimeError, "Unable to allocate memory.");
|
860
|
+
}
|
861
|
+
outwidthbuf = malloc(width_out * OIL_CMP(args.ol.os.cs));
|
862
|
+
if (!outwidthbuf) {
|
863
|
+
oil_libjpeg_free(&args.ol);
|
864
|
+
jpeg_destroy_compress(&writer.cinfo);
|
865
|
+
rb_raise(rb_eRuntimeError, "Unable to allocate memory.");
|
866
|
+
}
|
888
867
|
|
889
868
|
args.reader = reader;
|
890
869
|
args.opts = opts;
|
891
870
|
args.writer = &writer;
|
892
|
-
args.inwidthbuf = inwidthbuf;
|
893
871
|
args.outwidthbuf = outwidthbuf;
|
894
872
|
reader->locked = 1;
|
895
873
|
rb_protect((VALUE(*)(VALUE))each2, (VALUE)&args, &state);
|
896
874
|
|
897
|
-
|
898
|
-
preprocess_xscaler_free(&args.xs);
|
875
|
+
oil_libjpeg_free(&args.ol);
|
899
876
|
free(outwidthbuf);
|
900
|
-
free(inwidthbuf);
|
901
877
|
jpeg_destroy_compress(&writer.cinfo);
|
902
878
|
|
903
879
|
if (state) {
|
data/ext/oil/oil.c
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
#include <ruby.h>
|
2
|
-
#include "
|
2
|
+
#include "oil_resample.h"
|
3
3
|
|
4
4
|
static VALUE rb_fix_ratio(VALUE self, VALUE src_w, VALUE src_h, VALUE out_w, VALUE out_h)
|
5
5
|
{
|
6
|
-
|
6
|
+
int out_width, out_height;
|
7
7
|
VALUE ret;
|
8
|
+
|
8
9
|
out_width = NUM2INT(out_w);
|
9
10
|
out_height = NUM2INT(out_h);
|
10
|
-
|
11
|
+
oil_fix_ratio(NUM2INT(src_w), NUM2INT(src_h), &out_width, &out_height);
|
11
12
|
ret = rb_ary_new2(2);
|
12
13
|
rb_ary_push(ret, INT2FIX(out_width));
|
13
14
|
rb_ary_push(ret, INT2FIX(out_height));
|
@@ -0,0 +1,104 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2014-2019 Timothy Elliott
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to deal
|
5
|
+
* in the Software without restriction, including without limitation the rights
|
6
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
* copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
* THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include "oil_libjpeg.h"
|
23
|
+
#include <stdlib.h>
|
24
|
+
|
25
|
+
int oil_libjpeg_init(struct oil_libjpeg *ol,
|
26
|
+
struct jpeg_decompress_struct *dinfo, int out_width, int out_height)
|
27
|
+
{
|
28
|
+
int ret;
|
29
|
+
enum oil_colorspace cs;
|
30
|
+
|
31
|
+
ol->dinfo = dinfo;
|
32
|
+
|
33
|
+
cs = jpeg_cs_to_oil(dinfo->out_color_space);
|
34
|
+
if (cs == OIL_CS_UNKNOWN) {
|
35
|
+
return -1;
|
36
|
+
}
|
37
|
+
|
38
|
+
ol->inbuf = malloc(dinfo->output_width * dinfo->output_components);
|
39
|
+
if (!ol->inbuf) {
|
40
|
+
return -2;
|
41
|
+
}
|
42
|
+
|
43
|
+
ret = oil_scale_init(&ol->os, dinfo->output_height, out_height,
|
44
|
+
dinfo->output_width, out_width, cs);
|
45
|
+
if (ret!=0) {
|
46
|
+
free(ol->inbuf);
|
47
|
+
return ret;
|
48
|
+
}
|
49
|
+
|
50
|
+
return 0;
|
51
|
+
}
|
52
|
+
|
53
|
+
void oil_libjpeg_free(struct oil_libjpeg *ol)
|
54
|
+
{
|
55
|
+
if (ol->inbuf) {
|
56
|
+
free(ol->inbuf);
|
57
|
+
}
|
58
|
+
oil_scale_free(&ol->os);
|
59
|
+
}
|
60
|
+
|
61
|
+
void oil_libjpeg_read_scanline(struct oil_libjpeg *ol, unsigned char *outbuf)
|
62
|
+
{
|
63
|
+
int i;
|
64
|
+
|
65
|
+
for (i=oil_scale_slots(&ol->os); i>0; i--) {
|
66
|
+
jpeg_read_scanlines(ol->dinfo, &ol->inbuf, 1);
|
67
|
+
oil_scale_in(&ol->os, ol->inbuf);
|
68
|
+
}
|
69
|
+
oil_scale_out(&ol->os, outbuf);
|
70
|
+
}
|
71
|
+
|
72
|
+
enum oil_colorspace jpeg_cs_to_oil(J_COLOR_SPACE cs)
|
73
|
+
{
|
74
|
+
switch(cs) {
|
75
|
+
case JCS_GRAYSCALE:
|
76
|
+
return OIL_CS_G;
|
77
|
+
case JCS_RGB:
|
78
|
+
return OIL_CS_RGB;
|
79
|
+
case JCS_CMYK:
|
80
|
+
return OIL_CS_CMYK;
|
81
|
+
#ifdef JCS_EXTENSIONS
|
82
|
+
case JCS_EXT_RGBX:
|
83
|
+
case JCS_EXT_BGRX:
|
84
|
+
return OIL_CS_RGBX;
|
85
|
+
#endif
|
86
|
+
default:
|
87
|
+
return OIL_CS_UNKNOWN;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
J_COLOR_SPACE oil_cs_to_jpeg(enum oil_colorspace cs)
|
92
|
+
{
|
93
|
+
switch(cs) {
|
94
|
+
case OIL_CS_G:
|
95
|
+
return JCS_GRAYSCALE;
|
96
|
+
case OIL_CS_RGB:
|
97
|
+
return JCS_RGB;
|
98
|
+
case OIL_CS_CMYK:
|
99
|
+
return JCS_CMYK;
|
100
|
+
default:
|
101
|
+
return JCS_UNKNOWN;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2014-2019 Timothy Elliott
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to deal
|
5
|
+
* in the Software without restriction, including without limitation the rights
|
6
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
* copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
* THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#ifndef OIL_LIBJPEG_H
|
23
|
+
#define OIL_LIBJPEG_H
|
24
|
+
|
25
|
+
#include <stdio.h>
|
26
|
+
#include <jpeglib.h>
|
27
|
+
#include "oil_resample.h"
|
28
|
+
|
29
|
+
struct oil_libjpeg {
|
30
|
+
struct oil_scale os;
|
31
|
+
struct jpeg_decompress_struct *dinfo;
|
32
|
+
unsigned char *inbuf;
|
33
|
+
};
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Initialize an oil_libjpeg struct.
|
37
|
+
* @ol: Pointer to the struct to be initialized.
|
38
|
+
* @dinfo: Pointer to a libjpeg decompress struct, with header already read.
|
39
|
+
* @out_height: Desired height, in pixels, of the output image.
|
40
|
+
* @out_width: Desired width, in pixels, of the output image.
|
41
|
+
*
|
42
|
+
* Returns 0 on success.
|
43
|
+
* Returns -1 if an argument is bad.
|
44
|
+
* Returns -2 if unable to allocate memory.
|
45
|
+
*/
|
46
|
+
int oil_libjpeg_init(struct oil_libjpeg *ol,
|
47
|
+
struct jpeg_decompress_struct *dinfo, int out_width, int out_height);
|
48
|
+
|
49
|
+
void oil_libjpeg_free(struct oil_libjpeg *ol);
|
50
|
+
|
51
|
+
void oil_libjpeg_read_scanline(struct oil_libjpeg *ol, unsigned char *outbuf);
|
52
|
+
|
53
|
+
enum oil_colorspace jpeg_cs_to_oil(J_COLOR_SPACE cs);
|
54
|
+
|
55
|
+
J_COLOR_SPACE oil_cs_to_jpeg(enum oil_colorspace cs);
|
56
|
+
|
57
|
+
#endif
|
@@ -0,0 +1,161 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2014-2019 Timothy Elliott
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to deal
|
5
|
+
* in the Software without restriction, including without limitation the rights
|
6
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
* copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
* THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include "oil_libpng.h"
|
23
|
+
#include <stdlib.h>
|
24
|
+
|
25
|
+
static unsigned char **alloc_full_image_buf(int height, int rowbytes)
|
26
|
+
{
|
27
|
+
int i, j;
|
28
|
+
unsigned char **imgbuf;
|
29
|
+
|
30
|
+
imgbuf = malloc(height * sizeof(unsigned char *));
|
31
|
+
if (!imgbuf) {
|
32
|
+
return NULL;
|
33
|
+
}
|
34
|
+
for (i=0; i<height; i++) {
|
35
|
+
imgbuf[i] = malloc(rowbytes);
|
36
|
+
if (!imgbuf[i]) {
|
37
|
+
for (j=0; j<i-1; j++) {
|
38
|
+
free(imgbuf[j]);
|
39
|
+
}
|
40
|
+
free(imgbuf);
|
41
|
+
return NULL;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
return imgbuf;
|
45
|
+
}
|
46
|
+
|
47
|
+
static void free_full_image_buf(unsigned char **imgbuf, int height)
|
48
|
+
{
|
49
|
+
int i;
|
50
|
+
for (i=0; i<height; i++) {
|
51
|
+
free(imgbuf[i]);
|
52
|
+
}
|
53
|
+
free(imgbuf);
|
54
|
+
}
|
55
|
+
|
56
|
+
int oil_libpng_init(struct oil_libpng *ol, png_structp rpng, png_infop rinfo,
|
57
|
+
int out_width, int out_height)
|
58
|
+
{
|
59
|
+
int ret, in_width, in_height, buf_len;
|
60
|
+
enum oil_colorspace cs;
|
61
|
+
|
62
|
+
ol->rpng = rpng;
|
63
|
+
ol->rinfo = rinfo;
|
64
|
+
ol->in_vpos = 0;
|
65
|
+
ol->inbuf = NULL;
|
66
|
+
ol->inimage = NULL;
|
67
|
+
|
68
|
+
cs = png_cs_to_oil(png_get_color_type(rpng, rinfo));
|
69
|
+
if (cs == OIL_CS_UNKNOWN) {
|
70
|
+
return -1;
|
71
|
+
}
|
72
|
+
|
73
|
+
in_width = png_get_image_width(rpng, rinfo);
|
74
|
+
in_height = png_get_image_height(rpng, rinfo);
|
75
|
+
ret = oil_scale_init(&ol->os, in_height, out_height, in_width,
|
76
|
+
out_width, cs);
|
77
|
+
if (ret!=0) {
|
78
|
+
free(ol->inbuf);
|
79
|
+
return ret;
|
80
|
+
}
|
81
|
+
|
82
|
+
buf_len = png_get_rowbytes(rpng, rinfo);
|
83
|
+
switch (png_get_interlace_type(rpng, rinfo)) {
|
84
|
+
case PNG_INTERLACE_NONE:
|
85
|
+
ol->inbuf = malloc(buf_len);
|
86
|
+
if (!ol->inbuf) {
|
87
|
+
oil_scale_free(&ol->os);
|
88
|
+
return -2;
|
89
|
+
}
|
90
|
+
break;
|
91
|
+
case PNG_INTERLACE_ADAM7:
|
92
|
+
ol->inimage = alloc_full_image_buf(in_height, buf_len);
|
93
|
+
if (!ol->inimage) {
|
94
|
+
oil_scale_free(&ol->os);
|
95
|
+
return -2;
|
96
|
+
}
|
97
|
+
png_read_image(rpng, ol->inimage);
|
98
|
+
break;
|
99
|
+
}
|
100
|
+
|
101
|
+
return 0;
|
102
|
+
}
|
103
|
+
|
104
|
+
void oil_libpng_free(struct oil_libpng *ol)
|
105
|
+
{
|
106
|
+
if (ol->inbuf) {
|
107
|
+
free(ol->inbuf);
|
108
|
+
}
|
109
|
+
if (ol->inimage) {
|
110
|
+
free_full_image_buf(ol->inimage, ol->os.in_height);
|
111
|
+
}
|
112
|
+
oil_scale_free(&ol->os);
|
113
|
+
}
|
114
|
+
|
115
|
+
static void read_scanline_interlaced(struct oil_libpng *ol, unsigned char *outbuf)
|
116
|
+
{
|
117
|
+
int i;
|
118
|
+
|
119
|
+
for (i=oil_scale_slots(&ol->os); i>0; i--) {
|
120
|
+
oil_scale_in(&ol->os, ol->inimage[ol->in_vpos++]);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
static void read_scanline(struct oil_libpng *ol, unsigned char *outbuf)
|
125
|
+
{
|
126
|
+
int i;
|
127
|
+
|
128
|
+
for (i=oil_scale_slots(&ol->os); i>0; i--) {
|
129
|
+
png_read_row(ol->rpng, ol->inbuf, NULL);
|
130
|
+
oil_scale_in(&ol->os, ol->inbuf);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
void oil_libpng_read_scanline(struct oil_libpng *ol, unsigned char *outbuf)
|
135
|
+
{
|
136
|
+
switch (png_get_interlace_type(ol->rpng, ol->rinfo)) {
|
137
|
+
case PNG_INTERLACE_NONE:
|
138
|
+
read_scanline(ol, outbuf);
|
139
|
+
break;
|
140
|
+
case PNG_INTERLACE_ADAM7:
|
141
|
+
read_scanline_interlaced(ol, outbuf);
|
142
|
+
break;
|
143
|
+
}
|
144
|
+
oil_scale_out(&ol->os, outbuf);
|
145
|
+
}
|
146
|
+
|
147
|
+
enum oil_colorspace png_cs_to_oil(png_byte cs)
|
148
|
+
{
|
149
|
+
switch(cs) {
|
150
|
+
case PNG_COLOR_TYPE_GRAY:
|
151
|
+
return OIL_CS_G;
|
152
|
+
case PNG_COLOR_TYPE_GA:
|
153
|
+
return OIL_CS_GA;
|
154
|
+
case PNG_COLOR_TYPE_RGB:
|
155
|
+
return OIL_CS_RGB;
|
156
|
+
case PNG_COLOR_TYPE_RGBA:
|
157
|
+
return OIL_CS_RGBA;
|
158
|
+
default:
|
159
|
+
return OIL_CS_UNKNOWN;
|
160
|
+
}
|
161
|
+
}
|