Line data Source code
1 : // Snap Websites Servers -- snap websites image handling
2 : // Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : #include "snapwebsites/snap_exception.h"
20 :
21 : #include <QVector>
22 : #include <QSharedPointer>
23 :
24 : namespace snap
25 : {
26 :
27 : class snap_image_exception : public snap_exception
28 : {
29 : public:
30 : explicit snap_image_exception(char const * whatmsg) : snap_exception("snap_image", whatmsg) {}
31 : explicit snap_image_exception(std::string const & whatmsg) : snap_exception("snap_image", whatmsg) {}
32 : explicit snap_image_exception(QString const & whatmsg) : snap_exception("snap_image", whatmsg) {}
33 : };
34 :
35 : class snap_image_exception_no_buffer : public snap_image_exception
36 : {
37 : public:
38 : explicit snap_image_exception_no_buffer(char const * whatmsg) : snap_image_exception(whatmsg) {}
39 : explicit snap_image_exception_no_buffer(std::string const & whatmsg) : snap_image_exception(whatmsg) {}
40 : explicit snap_image_exception_no_buffer(QString const & whatmsg) : snap_image_exception(whatmsg) {}
41 : };
42 :
43 : class snap_image_exception_invalid_image : public snap_image_exception
44 : {
45 : public:
46 : explicit snap_image_exception_invalid_image(char const * whatmsg) : snap_image_exception(whatmsg) {}
47 : explicit snap_image_exception_invalid_image(std::string const & whatmsg) : snap_image_exception(whatmsg) {}
48 : explicit snap_image_exception_invalid_image(QString const & whatmsg) : snap_image_exception(whatmsg) {}
49 : };
50 :
51 :
52 :
53 :
54 :
55 : // whatever the input format, in memory we only manage RGBA images for
56 : // whatever we do with images (flip, rotate, borders...)
57 : struct snap_rgba
58 : {
59 : unsigned char f_red;
60 : unsigned char f_green;
61 : unsigned char f_blue;
62 : unsigned char f_alpha;
63 : };
64 :
65 :
66 : class snap_image;
67 :
68 : #pragma GCC diagnostic push
69 : #pragma GCC diagnostic ignored "-Weffc++"
70 0 : class snap_image_buffer_t
71 : {
72 : public:
73 : snap_image_buffer_t(snap_image *owner);
74 :
75 : QString const & get_mime_type() const;
76 : void set_mime_type(QString const & mime_type);
77 : QString const & get_format_version() const;
78 : void set_format_version(QString const & format_version);
79 : QString const & get_resolution_unit() const;
80 : void set_resolution_unit(QString const & resolution_unit);
81 : int get_xres() const;
82 : void set_xres(int xres);
83 : int get_yres() const;
84 : void set_yres(int yres);
85 : int get_width() const;
86 : void set_width(int width);
87 : int get_height() const;
88 : void set_height(int height);
89 : int get_depth() const;
90 : void set_depth(int depth);
91 : int get_bits() const;
92 : void set_bits(int bits);
93 :
94 : unsigned char * get_buffer() const;
95 :
96 : private:
97 : snap_image * f_owner = nullptr; // parent has a shared pointer to us, which is enough!
98 : QString f_mime_type = QString();
99 : QString f_format_version = QString();
100 : QString f_resolution_unit = QString();
101 : int32_t f_xres = 0;
102 : int32_t f_yres = 0;
103 : int32_t f_width = 0;
104 : int32_t f_height = 0;
105 : int32_t f_depth = 0; // 1 or 3 or 4
106 : int32_t f_bits = 0;
107 : QSharedPointer<unsigned char> f_buffer = QSharedPointer<unsigned char>();
108 : };
109 : #pragma GCC diagnostic pop
110 :
111 : typedef QSharedPointer<snap_image_buffer_t> smart_snap_image_buffer_t;
112 : typedef QVector<smart_snap_image_buffer_t> snap_image_buffer_vector_t;
113 :
114 :
115 0 : class snap_image
116 : {
117 : public:
118 : bool get_info(QByteArray const & data);
119 :
120 : size_t get_size() const;
121 : smart_snap_image_buffer_t get_buffer(int idx);
122 :
123 : private:
124 : bool info_jpeg(unsigned char const * s, size_t l, unsigned char const * e);
125 : bool info_ico(unsigned char const * s, size_t l, unsigned char const * e);
126 : bool info_bmp(unsigned char const * s, size_t l, unsigned char const * e);
127 : bool info_png(unsigned char const * s, size_t l, unsigned char const * e);
128 : bool info_gif(unsigned char const * s, size_t l, unsigned char const * e);
129 :
130 : // each buffer represents one RGBA image
131 : snap_image_buffer_vector_t f_buffers = snap_image_buffer_vector_t();
132 : };
133 :
134 :
135 :
136 : } // namespace snap
137 : // vim: ts=4 sw=4 et
|