libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GdkCRNPixbuf.cpp
Go to the documentation of this file.
1 /* Copyright 2010-2016 CoReNum, INSA-Lyon
2  *
3  * This file is part of libgtkcrnmm.
4  *
5  * libgtkcrnmm is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libgtkcrnmm is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libgtkcrnmm. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: GdkCRNPixbuf.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <libgtkcrnmm_config.h>
23 #include <GdkCRNPixbuf.h>
24 #include <CRNStringUTF8.h>
25 #include <CRNIO/CRNPath.h>
26 #include <CRNi18n.h>
27 
28 using namespace crn;
29 
30 static Glib::RefPtr<Gdk::Pixbuf> pixbufFromCRNImageRGB(const ImageRGB &img)
31 {
32  Glib::RefPtr<Gdk::Pixbuf> pb(Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, int(img.GetWidth()), int(img.GetHeight())));
33  size_t offset = 0;
34  int offsetpb = 0;
35  guint8 *pix = pb->get_pixels();
36  for (size_t y = 0; y < img.GetHeight(); y++)
37  {
38  int pto = offsetpb;
39  for (size_t x = 0; x < img.GetWidth(); x++)
40  {
41  pix[pto++] = img.At(offset).r;
42  pix[pto++] = img.At(offset).g;
43  pix[pto++] = img.At(offset++).b;
44  }
45  offsetpb += pb->get_rowstride();
46  }
47  return pb;
48 }
49 
50 static Glib::RefPtr<Gdk::Pixbuf> pixbufFromCRNImageGray(const ImageGray &img)
51 {
52  Glib::RefPtr<Gdk::Pixbuf> pb(Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, int(img.GetWidth()), int(img.GetHeight())));
53  size_t offset = 0;
54  int offsetpb = 0;
55  guint8 *pix = pb->get_pixels();
56  for (size_t y = 0; y < img.GetHeight(); y++)
57  {
58  int pto = offsetpb;
59  for (size_t x = 0; x < img.GetWidth(); x++)
60  {
61  uint8_t v = img.At(offset++);
62  pix[pto++] = v;
63  pix[pto++] = v;
64  pix[pto++] = v;
65  }
66  offsetpb += pb->get_rowstride();
67  }
68  return pb;
69 }
70 
71 static Glib::RefPtr<Gdk::Pixbuf> pixbufFromCRNImageBW(const ImageBW &img)
72 {
73  Glib::RefPtr<Gdk::Pixbuf> pb(Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, int(img.GetWidth()), int(img.GetHeight())));
74  size_t offset = 0;
75  int offsetpb = 0;
76  guint8 *pix = pb->get_pixels();
77  for (size_t y = 0; y < img.GetHeight(); y++)
78  {
79  int pto = offsetpb;
80  for (size_t x = 0; x < img.GetWidth(); x++)
81  {
82  uint8_t v = img.At(offset++) ? 255 : 0;
83  pix[pto++] = v;
84  pix[pto++] = v;
85  pix[pto++] = v;
86  }
87  offsetpb += pb->get_rowstride();
88  }
89  return pb;
90 }
91 
100 Glib::RefPtr<Gdk::Pixbuf> GdkCRN::PixbufFromCRNImage(const ImageBase &img)
101 {
102  try
103  {
104  return pixbufFromCRNImageRGB(dynamic_cast<const ImageRGB&>(img));
105  }
106  catch (...) {}
107  try
108  {
109  return pixbufFromCRNImageGray(dynamic_cast<const ImageGray&>(img));
110  }
111  catch (...) {}
112  try
113  {
114  return pixbufFromCRNImageBW(dynamic_cast<const ImageBW&>(img));
115  }
116  catch (...) {}
117  throw ExceptionInvalidArgument(crn::StringUTF8("GdkCRN::PixbufFromCRNImage(const ImageBase &img): ") + _("unsupported pixel format."));
118 }
119 
125 Glib::RefPtr<Gdk::Pixbuf> GdkCRN::PixbufFromFile(const Path &p)
126 {
127 #ifdef _MSC_VER
128  auto ext = p.GetExtension();
129  ext.ToLower();
130  if (ext == "png" || ext == "jpeg" || ext == "jpg")
132  else
133  return Gdk::Pixbuf::create_from_file(p.CStr());
134 #else
135  return Gdk::Pixbuf::create_from_file(p.CStr());
136 #endif
137 }
138 
147 ImageRGB GdkCRN::CRNImageFromPixbuf(const Glib::RefPtr<Gdk::Pixbuf> &pb)
148 {
149  if (!pb)
150  {
151  throw ExceptionInvalidArgument(StringUTF8("SImage SImageFromPixbuf(const Glib::RefPtr<Gdk::Pixbuf> &pb): ") + _("Null pixbuf given."));
152  }
153  guint8 *pix = pb->get_pixels();
154  auto imgrgb = ImageRGB(pb->get_width(), pb->get_height());
155  int offset = 0;
156  int offsetpb = 0;
157  int alpha = pb->get_has_alpha() ? 1 : 0;
158  for (size_t y = 0; y < imgrgb.GetHeight(); y++)
159  {
160  int tpo = offsetpb;
161  for (size_t x = 0; x < imgrgb.GetWidth(); x++)
162  {
163  tpo += alpha;
164  imgrgb.At(offset).r = pix[tpo++];
165  imgrgb.At(offset).g = pix[tpo++];
166  imgrgb.At(offset++).b = pix[tpo++];
167  }
168  offsetpb += pb->get_rowstride();
169  }
170  return imgrgb;
171 }
172 
173 #ifdef CRN_USING_GTKMM3
174 
181 {
182  Gdk::RGBA col;
183  col.set_rgba(p.r / 255.0, p.g / 255.0, p.b / 255.0);
184  return col;
185 }
186 
194 {
195  return crn::pixel::RGB8(uint8_t(color.get_red() * 255), uint8_t(color.get_green() * 255), uint8_t(color.get_blue() * 255));
196 }
197 
198 #else
199 
206 {
207  Gdk::Color col;
208  col.set_rgb_p(p.r / 255.0, p.g / 255.0, p.b / 255.0);
209  return col;
210 }
211 
219 {
220  return crn::pixel::RGB8(uint8_t(color.get_red_p() * 255), uint8_t(color.get_green_p() * 255), uint8_t(color.get_blue_p() * 255));
221 }
222 #endif
223 
Glib::RefPtr< Gdk::Pixbuf > PixbufFromCRNImage(const crn::ImageBase &img)
Creates a Gdk::Pixbuf from a crn::Image.
Abstract class for images.
Definition: CRNImage.h:141
StringUTF8 & ToLower()
Converts the string to lowercase.
Glib::RefPtr< Gdk::Pixbuf > PixbufFromFile(const crn::Path &p)
Creates a Gdk::Pixbuf from a file.
crn::pixel::RGB8 CRNPixelRGBFromGdkColor(const Gdk::Color &color)
Creates a crn::PixelRGB from a Gdk::Color.
std::vector< pixel_type >::reference At(size_t x, size_t y) noexcept
Returns a reference to a pixel.
Definition: CRNImage.h:224
size_t GetHeight() const noexcept
Definition: CRNImage.h:74
#define _(String)
Definition: CRNi18n.h:51
Image< pixel::RGB< uint8_t >> ImageRGB
Color image class.
const char * CStr() const noexcept
Conversion to UTF8 cstring.
A convenience class for file paths.
Definition: CRNPath.h:39
crn::ImageRGB CRNImageFromPixbuf(const Glib::RefPtr< Gdk::Pixbuf > &pb)
Creates a crn::Image from a Gdk::Pixbuf.
UImage NewImageFromFile(const Path &fname)
Loads an image from a file.
Definition: CRNImage.cpp:609
size_t GetWidth() const noexcept
Definition: CRNImage.h:72
A character string class.
Definition: CRNStringUTF8.h:49
RGB< uint8_t > RGB8
Definition: CRNPixel.h:87
Gdk::Color ColorFromCRNPixel(const crn::pixel::RGB8 &p)
Creates a Gdk::Color from a crn::Pixel.
Path GetExtension() const
Returns the extension.
Definition: CRNPath.cpp:252
Base class for images.
Definition: CRNImage.h:46
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107