Symphony Of Empires
binary_image.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
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 3 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, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // binary_image.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <sstream>
26 #include <stdexcept>
27 #include <cstring>
28 
29 extern "C" {
30 #define STB_IMAGE_WRITE_STATIC 1
31 #define STB_IMAGE_IMPLEMENTATION 1
32 #include "eng3d/stb_image.h"
33 #define STB_IMAGE_WRITE_IMPLEMENTATION 1
34 #include "eng3d/stb_image_write.h"
35 }
36 
37 #include "eng3d/binary_image.hpp"
38 #include "eng3d/utils.hpp"
39 #include "eng3d/io.hpp"
40 #include "eng3d/state.hpp"
41 #include "eng3d/log.hpp"
42 
44  from_file(path.str);
45 }
46 
47 Eng3D::BinaryImage::BinaryImage(size_t _width, size_t _height, size_t _bpp)
48  : width{ _width },
49  height{ _height },
50  bpp{ _bpp }
51 {
52  if(bpp == 32)
53  buffer = std::make_unique<uint32_t[]>(width * height);
54  /*else if(bpp == 16)
55  buffer = std::make_unique<uint16_t[]>(width * height);
56  else if(bpp == 8)
57  buffer = std::make_unique<uint8_t[]>(width * height);*/
58 }
59 
61  : Eng3D::BinaryImage(tex.width, tex.height, tex.bpp)
62 {
63  if(tex.bpp != this->bpp && this->bpp != 32)
64  CXX_THROW(BinaryImageException, "internal", "Can't do 16-bit/8-bit copies");
65  std::memcpy(buffer.get(), tex.buffer.get(), sizeof(uint32_t) * (width * height));
66 }
67 
69  int i_width, i_height, i_channels;
70 
71  // stbi can do the conversion to RGBA for us ;)
72  auto* c_buffer = stbi_load(path.str.c_str(), &i_width, &i_height, &i_channels, 4);
73  if(c_buffer == nullptr)
75  width = static_cast<size_t>(i_width);
76  height = static_cast<size_t>(i_height);
77 
78  // Requires re-allocation so it's handled by new[] (we don't know what stbi uses)
79  buffer = std::make_unique<uint32_t[]>(width * height);
80  std::memcpy(buffer.get(), c_buffer, sizeof(uint32_t) * width * height);
81  std::free(c_buffer);
82 }
83 
84 void Eng3D::BinaryImage::to_file(const std::string& filename) {
85  int channel_count = bpp == 32 ? 4 : bpp == 16 ? 2 : bpp == 8 ? 1 : 0;
86  int stride = channel_count * width;
87  stbi_write_png(filename.c_str(), width, height, channel_count, buffer.get(), stride);
88 }
STBIDEF const char * stbi_failure_reason(void)
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels)
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
This binary image class helps load images and visual resources from the disk; the binary image IS NOT...
virtual void from_file(const Eng3D::IO::Path &path)
BinaryImage()=default
virtual void to_file(const std::string &filename)
std::unique_ptr< uint32_t[]> buffer
The path class abstracts away most of the burden from handling system-dependant filesystem paths.
Definition: io.hpp:44
std::string str
Definition: io.hpp:59
#define CXX_THROW(class,...)
Definition: utils.hpp:98