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