summaryrefslogtreecommitdiff
path: root/boost/gil/extension/io/jpeg_io_private.hpp
blob: 4b3e7bdd5bab50c3e7a7ff89b6f9546f3fda5e11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
    Copyright 2005-2007 Adobe Systems Incorporated
   
    Use, modification and distribution are subject to the Boost Software License,
    Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt).

    See http://opensource.adobe.com/gil for most recent version including documentation.
*/

/*************************************************************************************************/

#ifndef GIL_JPEG_IO_PRIVATE_H
#define GIL_JPEG_IO_PRIVATE_H

/// \file
/// \brief  Internal support for reading and writing JPEG files
/// \author Hailin Jin and Lubomir Bourdev \n
///         Adobe Systems Incorporated
/// \date   2005-2007 \n Last updated September 24, 2006

#include <stdio.h>
#include <boost/static_assert.hpp>
#include <vector>
#include "../../gil_all.hpp"
#include "io_error.hpp"
#include <jpeglib.h>

namespace boost { namespace gil {

namespace detail {

// lbourdev: What is the advantage of having channel and colorspace together? Are there cases where they are interrelated?

template <typename Channel,typename ColorSpace>
struct jpeg_read_support_private {
    BOOST_STATIC_CONSTANT(bool,is_supported=false);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_UNKNOWN);
};
template <>
struct jpeg_read_support_private<bits8,gray_t> {
    BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_GRAYSCALE);
};
template <>
struct jpeg_read_support_private<bits8,rgb_t> {
    BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_RGB);
};
template <>
struct jpeg_read_support_private<bits8,cmyk_t> {
    BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_CMYK);
};
template <typename Channel,typename ColorSpace>
struct jpeg_write_support_private {
    BOOST_STATIC_CONSTANT(bool,is_supported=false);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_UNKNOWN);
};
template <>
struct jpeg_write_support_private<bits8,gray_t> {
    BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_GRAYSCALE);
};
template <>
struct jpeg_write_support_private<bits8,rgb_t> {
    BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_RGB);
};
template <>
struct jpeg_write_support_private<bits8,cmyk_t> {
    BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_CMYK);
};


class jpeg_reader : public file_mgr {
protected:
    jpeg_decompress_struct  _cinfo;
    jpeg_error_mgr          _jerr;

    void init() {
        _cinfo.err=jpeg_std_error(&_jerr);
        jpeg_create_decompress(&_cinfo);
        jpeg_stdio_src(&_cinfo,_fp.get());
        jpeg_read_header(&_cinfo,TRUE);
    }
public:
    jpeg_reader(FILE* file)           : file_mgr(file)           { init(); }
    jpeg_reader(const char* filename) : file_mgr(filename, "rb") { init(); }

    ~jpeg_reader() { jpeg_destroy_decompress(&_cinfo); }

    template <typename View>
    void apply(const View& view) {
        jpeg_start_decompress(&_cinfo);    // lbourdev: Can this return an error? You need to check and throw. Check all other library methods that can return an error state...
        io_error_if(_cinfo.data_precision!=8,"jpeg_reader::apply(): this image file is not supported");
        io_error_if(_cinfo.out_color_space!=jpeg_read_support_private<typename channel_type<View>::type,
                                                                      typename color_space_type<View>::type>::color_type,
                    "jpeg_reader::apply(): input view type does not match the image file");
        io_error_if(view.dimensions() != get_dimensions(), "jpeg_reader::apply(): input view dimensions do not match the image file");
        std::vector<pixel<bits8,layout<typename color_space_type<View>::type> > > row(view.width());
        JSAMPLE* row_address=(JSAMPLE*)&row.front();
        for(int y=0;y<view.height();++y) {
            io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
                        "jpeg_reader::apply(): fail to read JPEG file");
            std::copy(row.begin(),row.end(),view.row_begin(y));
        }
        jpeg_finish_decompress(&_cinfo);
    }
    
    template <typename Image>
    void read_image(Image& im) {
        im.recreate(get_dimensions());
        apply(view(im));
    }

    point2<std::ptrdiff_t> get_dimensions() const {
        return point2<std::ptrdiff_t>(_cinfo.image_width,_cinfo.image_height);
    }
};

// This code will be simplified...
template <typename CC>
class jpeg_reader_color_convert : public jpeg_reader {
private:
    CC _cc;
public:
    jpeg_reader_color_convert(FILE* file,CC cc_in)           : jpeg_reader(file),_cc(cc_in) {}
    jpeg_reader_color_convert(FILE* file)           : jpeg_reader(file) {}
    jpeg_reader_color_convert(const char* filename,CC cc_in) : jpeg_reader(filename),_cc(cc_in) {}
    jpeg_reader_color_convert(const char* filename) : jpeg_reader(filename) {}
    template <typename View>
    void apply(const View& view) {
        jpeg_start_decompress(&_cinfo);    // lbourdev: Can this return an error? You need to check and throw. Check all other library methods that can return an error state...
        io_error_if(_cinfo.data_precision!=8,"jpeg_reader_color_covert::apply(): this image file is not supported");
        io_error_if(view.dimensions() != get_dimensions(), "jpeg_reader_color_covert::apply(): input view dimensions don't match the image file");
        switch (_cinfo.out_color_space) {
        case JCS_GRAYSCALE: {
            std::vector<gray8_pixel_t> row(view.width());
            JSAMPLE* row_address=(JSAMPLE*)&row.front();
            for(int y=0;y<view.height();++y) {
                io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
                            "jpeg_reader_color_covert::apply(): fail to read JPEG file");
                std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray8_ref_t, typename View::value_type,CC>(_cc));
            }
            break;
        }
        case JCS_RGB: {
            std::vector<rgb8_pixel_t> row(view.width());
            JSAMPLE* row_address=(JSAMPLE*)&row.front();
            for(int y=0;y<view.height();++y) {
                io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
                            "jpeg_reader_color_covert::apply(): fail to read JPEG file");
                std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb8_ref_t, typename View::value_type,CC>(_cc));
            }
            break;
        }
        case JCS_CMYK: {
            std::vector<cmyk8_pixel_t> row(view.width());
            JSAMPLE* row_address=(JSAMPLE*)&row.front();
            for(int y=0;y<view.height();++y) {
                io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
                            "jpeg_reader_color_covert::apply(): fail to read JPEG file");
                std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<cmyk8_ref_t, typename View::value_type,CC>(_cc));
            }
            break;
        }
        default:
            io_error("jpeg_reader_color_covert::apply(): unknown color type");
        }
        jpeg_finish_decompress(&_cinfo);
    }    
    template <typename Image>
    void read_image(Image& im) {
        im.recreate(get_dimensions());
        apply(view(im));
    }
};

class jpeg_writer : public file_mgr {
    jpeg_compress_struct _cinfo;
    jpeg_error_mgr _jerr;

    void init() {
        _cinfo.err=jpeg_std_error(&_jerr);
        jpeg_create_compress(&_cinfo);
        jpeg_stdio_dest(&_cinfo,_fp.get());
    }
public:
    jpeg_writer(FILE* file)           : file_mgr(file)           { init(); }
    jpeg_writer(const char* filename) : file_mgr(filename, "wb") { init(); }
    ~jpeg_writer() { jpeg_destroy_compress(&_cinfo); }
    
    template <typename View>
    void apply(const View& view,int quality=100) {
        _cinfo.image_width  = (JDIMENSION)view.width();
        _cinfo.image_height = (JDIMENSION)view.height();
        _cinfo.input_components=num_channels<View>::value;
        _cinfo.in_color_space = jpeg_write_support_private<typename channel_type<View>::type,
                                                           typename color_space_type<View>::type>::color_type;
        jpeg_set_defaults(&_cinfo);
        jpeg_set_quality(&_cinfo, quality, TRUE);
        jpeg_start_compress(&_cinfo, TRUE);
        std::vector<pixel<bits8,layout<typename color_space_type<View>::type> > > row(view.width());
        JSAMPLE* row_address=(JSAMPLE*)&row.front();
        for (int y=0;y<view.height(); ++y) {
            std::copy(view.row_begin(y),view.row_end(y),row.begin());
            io_error_if(jpeg_write_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1) != 1,
                        "jpeg_writer::apply(): fail to write file");
        }
        jpeg_finish_compress(&_cinfo);
    }
};

} // namespace detail

} }  // namespace boost::gil

#endif