Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lodepng_io_private.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72e2732aaf629b158d5d166fe05ab45421c0cbca (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
    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://stlab.adobe.com/gil for most recent version including documentation.
*/
/*************************************************************************************************/

#ifndef GIL_LODEPNG_IO_PRIVATE_H
#define GIL_LODEPNG_IO_PRIVATE_H

/// \file
/// \brief  Internal support for reading and writing PNG files
/// \author Hailin Jin and Lubomir Bourdev \n
///         Adobe Systems Incorporated
/// \date   2005-2007 \n Last updated August 14, 2007

#include <algorithm>
#include <vector>
#include <boost/static_assert.hpp>
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/io_error.hpp>
#include "lodepng.hpp"

namespace boost { namespace gil {

namespace detail {

static const std::size_t LODEPNG_BYTES_TO_CHECK = 4;

static const int LODEPNG_COLOR_MASK_PALETTE = 1;
static const int LODEPNG_COLOR_MASK_COLOR = 2;
static const int LODEPNG_COLOR_MASK_ALPHA = 4;
static const int LODEPNG_COLOR_TYPE_GRAY = 0;
static const int LODEPNG_COLOR_TYPE_PALETTE = (LODEPNG_COLOR_MASK_COLOR | LODEPNG_COLOR_MASK_PALETTE);
static const int LODEPNG_COLOR_TYPE_RGB = (LODEPNG_COLOR_MASK_COLOR);
static const int LODEPNG_COLOR_TYPE_RGB_ALPHA = (LODEPNG_COLOR_MASK_COLOR | LODEPNG_COLOR_MASK_ALPHA);
static const int LODEPNG_COLOR_TYPE_GRAY_ALPHA = (LODEPNG_COLOR_MASK_ALPHA);
static const int LODEPNG_COLOR_TYPE_RGBA = LODEPNG_COLOR_TYPE_RGB_ALPHA;
static const int LODEPNG_COLOR_TYPE_GA = LODEPNG_COLOR_TYPE_GRAY_ALPHA;


// lbourdev: These can be greatly simplified, for example:
template <typename Cs> struct lodepng_color_type {BOOST_STATIC_CONSTANT(int,color_type=0);};
template<> struct lodepng_color_type<gray_t> { BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_GRAY); };
template<> struct lodepng_color_type<rgb_t>  { BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGB); };
template<> struct lodepng_color_type<rgba_t> { BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGBA); };

template <typename Channel,typename ColorSpace> struct lodepng_is_supported {BOOST_STATIC_CONSTANT(bool,value=false);};
template <> struct lodepng_is_supported<bits8,gray_t>  {BOOST_STATIC_CONSTANT(bool,value=true);};
template <> struct lodepng_is_supported<bits8,rgb_t>   {BOOST_STATIC_CONSTANT(bool,value=true);};
template <> struct lodepng_is_supported<bits8,rgba_t>  {BOOST_STATIC_CONSTANT(bool,value=true);};
template <> struct lodepng_is_supported<bits16,gray_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
template <> struct lodepng_is_supported<bits16,rgb_t>  {BOOST_STATIC_CONSTANT(bool,value=true);};
template <> struct lodepng_is_supported<bits16,rgba_t> {BOOST_STATIC_CONSTANT(bool,value=true);};

template <typename Channel> struct lodepng_bit_depth {BOOST_STATIC_CONSTANT(int,bit_depth=sizeof(Channel)*8);};

template <typename Channel,typename ColorSpace>
struct lodepng_read_support_private {
    BOOST_STATIC_CONSTANT(bool,is_supported=false);
    BOOST_STATIC_CONSTANT(int,bit_depth=0);
    BOOST_STATIC_CONSTANT(int,color_type=0);
};
template <>
struct lodepng_read_support_private<bits8,gray_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=8);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_GRAY);
};
template <>
struct lodepng_read_support_private<bits8,rgb_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=8);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGB);
};
template <>
struct lodepng_read_support_private<bits8,rgba_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=8);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGBA);
};
template <>
struct lodepng_read_support_private<bits16,gray_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=16);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_GRAY);
};
template <>
struct lodepng_read_support_private<bits16,rgb_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=16);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGB);
};
template <>
struct lodepng_read_support_private<bits16,rgba_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=16);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGBA);
};

template <typename Channel,typename ColorSpace>
struct lodepng_write_support_private {
    BOOST_STATIC_CONSTANT(bool,is_supported=false);
    BOOST_STATIC_CONSTANT(int,bit_depth=0);
    BOOST_STATIC_CONSTANT(int,color_type=0);
};
template <>
struct lodepng_write_support_private<bits8,gray_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=8);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_GRAY);
};
template <>
struct lodepng_write_support_private<bits8,rgb_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=8);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGB);
};
template <>
struct lodepng_write_support_private<bits8,rgba_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=8);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGBA);
};
template <>
struct lodepng_write_support_private<bits16,gray_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=16);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_GRAY);
};
template <>
struct lodepng_write_support_private<bits16,rgb_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=16);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGB);
};
template <>
struct lodepng_write_support_private<bits16,rgba_t> {
    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    BOOST_STATIC_CONSTANT(int,bit_depth=16);
    BOOST_STATIC_CONSTANT(int,color_type=LODEPNG_COLOR_TYPE_RGBA);
};

class lodepng_reader {
protected:

    LodePNG::Decoder m_decoder;
    ReaderPtr<Reader> & m_reader;

    int sig_cmp(unsigned char * sig, size_t start, size_t num_to_check)
    {
       unsigned char png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
       if (num_to_check > 8)
          num_to_check = 8;
       else if (num_to_check < 1)
          return (-1);

       if (start > 7)
          return (-1);

       if (start + num_to_check > 8)
          num_to_check = 8 - start;

       for (size_t i = 0; i < num_to_check; ++i)
       if (sig[i] != png_sig[i])
         return -1;

       return 0;
    }

    void init()
    {
      unsigned char buf[30];
      m_reader.Read(0, buf, 30);
//        io_error_if(fread(buf, 1, 30, get()) != 30,
//                    "lodepng_check_validity: fail to read file");
      m_decoder.inspect(buf, 30);
    }

public:

    lodepng_reader(ReaderPtr<Reader> & reader) : m_reader(reader) { init(); }

    point2<std::ptrdiff_t> get_dimensions() {
        return point2<std::ptrdiff_t>(m_decoder.getWidth(), m_decoder.getHeight());
    }

    template <typename View>
    void apply(const View& view)
    {
        io_error_if((view.width() != m_decoder.getWidth() || view.height() != m_decoder.getHeight()),
                    "lodepng_read_view: input view size does not match PNG file size");
        
        int bitDepth = lodepng_read_support_private<typename channel_type<View>::type,
                                                    typename color_space_type<View>::type>::bit_depth;
        if (bitDepth != m_decoder.getBpp() / m_decoder.getChannels())
           io_error("lodepng_read_view: input view type is incompatible with the image type(bitDepth mismatch)");

        int colorType = lodepng_read_support_private<typename channel_type<View>::type,
                                                     typename color_space_type<View>::type>::color_type;
        if (colorType != m_decoder.getInfoPng().color.colorType)
            io_error("lodepng_read_view: input view type is incompatible with the image type(colorType mismatch)");

        std::vector<unsigned char> inputData;
        LodePNG::loadFile(inputData, m_reader);

        std::vector<unsigned char> decodedData;
        m_decoder.decode(decodedData, inputData);

        if (m_decoder.hasError())
        {
          std::stringstream out;
          out << "lodepng_read_view: " << m_decoder.getError();
          io_error(out.str().c_str());
        }

        gil::rgba8_view_t srcView = gil::interleaved_view(
            view.width(),
            view.height(),
            gil::rgba8_ptr_t(&decodedData[0]),
            4 * view.width());

        gil::copy_pixels(srcView, view);
    }

    template <typename Image>
    void read_image(Image& im) {
        im.recreate(get_dimensions());
        apply(view(im));
    }
};

// This code will be simplified...
template <typename CC>
class lodepng_reader_color_convert : public lodepng_reader {
private:
    CC _cc;
public:
    lodepng_reader_color_convert(ReaderPtr<Reader> & reader, CC cc_in) : lodepng_reader(reader),_cc(cc_in) {}
    lodepng_reader_color_convert(ReaderPtr<Reader> & reader) : lodepng_reader(reader) {}
    template <typename View>
    void apply(const View& view)
    {
      io_error_if((static_cast<unsigned>(view.width()) != m_decoder.getWidth()
                   || static_cast<unsigned>(view.height()) != m_decoder.getHeight()),
                  "lodepng_read_view: input view size does not match PNG file size");

      std::vector<unsigned char> inputData;
      LodePNG::loadFile(inputData, m_reader);

      std::vector<unsigned char> decodedData;
      m_decoder.decode(decodedData, inputData);

      if (m_decoder.hasError())
      {
        std::stringstream out;
        out << "lodepng_read_view: " << m_decoder.getError();
        io_error(out.str().c_str());
      }

      int bitDepth = m_decoder.getBpp() / m_decoder.getChannels();
      int colorType = m_decoder.getInfoPng().color.colorType;

      switch (colorType)
      {
      case LODEPNG_COLOR_TYPE_GRAY:
        switch (bitDepth)
        {
        case 8:
          {
            gil::gray8_view_t srcView = gil::interleaved_view(
              m_decoder.getWidth(),
              m_decoder.getHeight(),
              gil::gray8_ptr_t(&decodedData[0]),
              sizeof(gil::gray8_pixel_t) * view.width()
            );

            gil::copy_and_convert_pixels(srcView, view, _cc);
            break;
          }
        case 16:
          {
            gil::gray8_view_t srcView = gil::interleaved_view(
              m_decoder.getWidth(),
              m_decoder.getHeight(),
              gil::gray8_ptr_t(&decodedData[0]),
              sizeof(gil::gray8_pixel_t) * view.width()
            );

            gil::copy_and_convert_pixels(srcView, view, _cc);
            break;
          }
        default:
            io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
        }
        break;
      case LODEPNG_COLOR_TYPE_RGB:
        switch (bitDepth)
        {
        case 8:
          {
            gil::rgb8_view_t srcView = gil::interleaved_view(
              m_decoder.getWidth(),
              m_decoder.getHeight(),
              gil::rgb8_ptr_t(&decodedData[0]),
              sizeof(gil::rgb8_pixel_t) * view.width()
            );
            gil::copy_and_convert_pixels(srcView, view, _cc);
            break;
          }
        case 16:
          {
            gil::rgb16_view_t srcView = gil::interleaved_view(
                m_decoder.getWidth(),
                m_decoder.getHeight(),
                gil::rgb16_ptr_t(&decodedData[0]),
                sizeof(gil::rgba16_pixel_t) * view.width()
                );
            gil::copy_and_convert_pixels(srcView, view, _cc);
            break;
          }
        default:
          io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
        }
        break;
      case LODEPNG_COLOR_TYPE_RGBA:
        switch (bitDepth)
        {
        case 8:
          {
            gil::rgba8_view_t srcView = gil::interleaved_view(
                m_decoder.getWidth(),
                m_decoder.getHeight(),
                gil::rgba8_ptr_t(&decodedData[0]),
                sizeof(gil::rgba8_pixel_t) * m_decoder.getWidth()
                );
            gil::copy_and_convert_pixels(srcView, view, _cc);
            break;
          }
        case 16:
          {
            gil::rgba16_view_t srcView = gil::interleaved_view(
                m_decoder.getWidth(),
                m_decoder.getHeight(),
                gil::rgba16_ptr_t(&decodedData[0]),
                sizeof(gil::rgba16_pixel_t) * m_decoder.getWidth()
                );
            gil::copy_and_convert_pixels(srcView, view, _cc);
            break;
          }
        default:
          io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
        }
        break;
      default:
        io_error("png_reader_color_convert::apply(): unknown color type");
      }
    }

    template <typename Image>
    void read_image(Image& im) {
        im.recreate(get_dimensions());
        apply(view(im));
    }
};


class lodepng_writer /*: public file_mgr */{
protected:

    LodePNG::Encoder m_encoder;
    WriterPtr<Writer> & m_writer;

    void init() {}

public:
    lodepng_writer(WriterPtr<Writer> & writer) : /*file_mgr(filename, "wb"), m_fileName(filename)*/ m_writer(writer) { init(); }

    template <typename View>
    void apply(const View& view)
    {
      typedef lodepng_write_support_private<
          typename channel_type<View>::type,
          typename color_space_type<View>::type
      > write_support_t;

      /// Create temporary image with base color_space_type.

      LodePNG_InfoRaw infoRaw = m_encoder.getInfoRaw();
      LodePNG_InfoPng infoPng = m_encoder.getInfoPng();

      infoRaw.color.bitDepth = write_support_t::bit_depth;
      infoRaw.color.colorType = write_support_t::color_type;

      m_encoder.setInfoRaw(infoRaw);

      infoPng.compressionMethod = 0;
      infoPng.filterMethod = 0;
      infoPng.interlaceMethod = 0;
      infoPng.height = view.height();
      infoPng.width = view.width();
      infoPng.color.bitDepth = write_support_t::bit_depth;
      infoPng.color.colorType = write_support_t::color_type;

      m_encoder.setInfoPng(infoPng);

      std::vector<unsigned char> buffer;

      /// in original code there was some tricks with byte indiannes.
      m_encoder.encode(
          buffer,
          (unsigned char *)&view(0, 0),
          view.width(),
          view.height()
      );

      LodePNG::saveFile(buffer, m_writer);
   }
};

} // namespace detail
} }  // namespace boost::gil

#endif