Berserk
Image.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_IMAGE_HPP
29 #define BERSERK_IMAGE_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Data.hpp>
33 #include <core/Typedefs.hpp>
34 #include <core/string/String.hpp>
35 #include <rhi/RHIDefs.hpp>
36 
38 
52 class Image {
53 public:
59 
61  enum class FileFormat {
62  Png,
63  Jpg,
64  Bmp
65  };
66 
67  BRK_API Image() = default;
68  BRK_API Image(uint32 width, uint32 height, Format format);
69  BRK_API Image(uint32 width, uint32 height, uint32 stride, uint32 pixelSize, Format format, Ref<Data> pixelData);
70  BRK_API Image(const Image &) = default;
71  BRK_API Image(Image &&) noexcept = default;
72  BRK_API ~Image() = default;
73 
74  BRK_API Image &operator=(const Image &) = default;
75  BRK_API Image &operator=(Image &&) noexcept = default;
76 
77  BRK_API Image Resize(uint32 newWidth, uint32 newHeight) const;
78 
79  BRK_API bool Empty() const { return mWidth * mHeight == 0; }
80 
81  BRK_API uint32 GetWidth() const { return mWidth; }
82  BRK_API uint32 GetHeight() const { return mHeight; }
83  BRK_API uint32 GetStride() const { return mStride; }
84  BRK_API uint32 GetPixelSize() const { return mPixelSize; }
85  BRK_API uint32 GetSizeBytes() const { return mStride * mHeight; }
86  BRK_API Format GetFormat() const { return mFormat; }
87  BRK_API const Ref<Data> &GetPixelData() const { return mPixelData; }
88 
100  BRK_API bool SaveRgba(const String &filepath, FileFormat fileFormat, int quality = 100) const;
101 
112  BRK_API static Image LoadRgba(const String &path, uint32 channels = 4);
113 
114 private:
115  uint32 mWidth = 0;
116  uint32 mHeight = 0;
117  uint32 mStride = 0;
118  uint32 mPixelSize = 0;
119  Format mFormat = Format::Unknown;
120  Ref<Data> mPixelData;
121 };
122 
128 
129 #endif//BERSERK_IMAGE_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
std::uint32_t uint32
Definition: Typedefs.hpp:44
Format of the image stored data.
Hardware-independent image representation.
Definition: Image.hpp:52
BRK_API const Ref< Data > & GetPixelData() const
Definition: Image.hpp:87
static BRK_API Image LoadRgba(const String &path, uint32 channels=4)
Load rgba image from the disc.
Definition: Image.cpp:128
FileFormat
Formats to save image.
Definition: Image.hpp:61
BRK_API uint32 GetWidth() const
Definition: Image.hpp:81
BRK_API bool SaveRgba(const String &filepath, FileFormat fileFormat, int quality=100) const
Save rgba image to the file.
Definition: Image.cpp:98
BRK_API Image(const Image &)=default
BRK_API uint32 GetSizeBytes() const
Definition: Image.hpp:85
BRK_API Image Resize(uint32 newWidth, uint32 newHeight) const
Definition: Image.cpp:61
BRK_API Image()=default
BRK_API Image(Image &&) noexcept=default
BRK_API uint32 GetHeight() const
Definition: Image.hpp:82
BRK_API Format GetFormat() const
Definition: Image.hpp:86
BRK_API uint32 GetPixelSize() const
Definition: Image.hpp:84
BRK_API uint32 GetStride() const
Definition: Image.hpp:83
BRK_API bool Empty() const
Definition: Image.hpp:79
Utf-8 encoded std based default string class.
RHITextureFormat
Definition: RHIDefs.hpp:135
Definition: GLDevice.cpp:46