Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpImageIoStb.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2022 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * stb backend for JPEG and PNG image I/O operations.
33  *
34  *****************************************************************************/
35 
41 #include "vpImageIoBackend.h"
42 
43 #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2)
44 # define VISP_HAVE_SSE2 1
45 #endif
46 
47 #ifndef VISP_HAVE_SSE2
48 # define STBI_NO_SIMD
49 #endif
50 
51 #define STB_IMAGE_IMPLEMENTATION
52 #include <stb_image.h>
53 
54 #define STB_IMAGE_WRITE_IMPLEMENTATION
55 #include <stb_image_write.h>
56 
57 
58 void readStb(vpImage<unsigned char> &I, const std::string &filename)
59 {
60  int width = 0, height = 0, channels = 0;
61  unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_grey);
62  if (image == NULL) {
63  throw(vpImageException(vpImageException::ioError, "Can't read the image: %s", filename.c_str()));
64  }
65  I.init(image, static_cast<unsigned int>(height), static_cast<unsigned int>(width), true);
66  stbi_image_free(image);
67 }
68 
69 void readStb(vpImage<vpRGBa> &I, const std::string &filename)
70 {
71  int width = 0, height = 0, channels = 0;
72  unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
73  if (image == NULL) {
74  throw(vpImageException(vpImageException::ioError, "Can't read the image: %s", filename.c_str()));
75  }
76  I.init(reinterpret_cast<vpRGBa*>(image), static_cast<unsigned int>(height), static_cast<unsigned int>(width), true);
77  stbi_image_free(image);
78 }
79 
80 void writeJPEGStb(const vpImage<unsigned char> &I, const std::string &filename, int quality)
81 {
82  int res = stbi_write_jpg(filename.c_str(), static_cast<int>(I.getWidth()), static_cast<int>(I.getHeight()), STBI_grey,
83  reinterpret_cast<void*>(I.bitmap), quality);
84  if (res == 0) {
85  throw(vpImageException(vpImageException::ioError, "JEPG write error"));
86  }
87 }
88 
89 void writeJPEGStb(const vpImage<vpRGBa> &I, const std::string &filename, int quality)
90 {
91  int res = stbi_write_jpg(filename.c_str(), static_cast<int>(I.getWidth()), static_cast<int>(I.getHeight()), STBI_rgb_alpha,
92  reinterpret_cast<void*>(I.bitmap), quality);
93  if (res == 0) {
94  throw(vpImageException(vpImageException::ioError, "JEPG write error"));
95  }
96 }
97 
98 void writePNGStb(const vpImage<unsigned char> &I, const std::string &filename)
99 {
100  const int stride_in_bytes = static_cast<int>(I.getWidth());
101  int res = stbi_write_png(filename.c_str(), static_cast<int>(I.getWidth()), static_cast<int>(I.getHeight()), STBI_grey,
102  reinterpret_cast<void*>(I.bitmap), stride_in_bytes);
103  if (res == 0) {
104  throw(vpImageException(vpImageException::ioError, "PNG write error: %s", filename.c_str()));
105  }
106 }
107 
108 void writePNGStb(const vpImage<vpRGBa> &I, const std::string &filename)
109 {
110  const int stride_in_bytes = static_cast<int>(4 * I.getWidth());
111  int res = stbi_write_png(filename.c_str(), static_cast<int>(I.getWidth()), static_cast<int>(I.getHeight()), STBI_rgb_alpha,
112  reinterpret_cast<void*>(I.bitmap), stride_in_bytes);
113  if (res == 0) {
114  throw(vpImageException(vpImageException::ioError, "PNG write error: %s", filename.c_str()));
115  }
116 }
Type * bitmap
points toward the bitmap
Definition: vpImage.h:143
Error that can be emited by the vpImage class and its derivates.
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:643
unsigned int getHeight() const
Definition: vpImage.h:188
unsigned int getWidth() const
Definition: vpImage.h:246