1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2026 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-runtime-lib
* Created on: 25 янв. 2026 г.
*
* lsp-runtime-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* lsp-runtime-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_MM_BITMAP_H_
#define LSP_PLUG_IN_MM_BITMAP_H_
#include <lsp-plug.in/runtime/version.h>
#include <lsp-plug.in/common/status.h>
#include <lsp-plug.in/common/types.h>
#include <lsp-plug.in/io/Path.h>
#include <lsp-plug.in/io/IInStream.h>
#include <lsp-plug.in/mm/pixel.h>
#include <lsp-plug.in/mm/IColorMap.h>
#include <lsp-plug.in/runtime/LSPString.h>
namespace lsp
{
namespace mm
{
/**
* Bitmap class, simple raster bitmap storage
*/
class Bitmap
{
private:
uint8_t *pData;
size_t nWidth;
size_t nHeight;
size_t nStride;
pixel_format_t enFormat;
private:
status_t do_load(io::IInStream *is, pixel_format_t format, IColorMap *map);
status_t load_xpm(io::IInStream *is, pixel_format_t format, IColorMap *map);
static inline size_t calc_bytes_per_row(pixel_format_t format, size_t cols) noexcept;
static inline size_t calc_stride(pixel_format_t format, size_t cols) noexcept;
public:
Bitmap() noexcept;
Bitmap(const Bitmap &);
Bitmap(const Bitmap *);
Bitmap(Bitmap && src) noexcept;
~Bitmap();
Bitmap & operator = (const Bitmap & src);
Bitmap & operator = (const Bitmap * src);
Bitmap & operator = (Bitmap && src) noexcept;
void swap(Bitmap & src) noexcept;
void swap(Bitmap * src) noexcept;
public:
/**
* Get pixel format of the bitmap
* @return pixel format
*/
inline pixel_format_t format() const noexcept { return enFormat; }
/**
* Get number of rows
* @return number of rows
*/
inline size_t rows() const noexcept { return nHeight; }
/**
* Get bitmap width
* @return bitmap width in pixels
*/
inline size_t width() const noexcept { return nWidth; }
/**
* Get number of columns
* @return number of columns
*/
inline size_t columns() const noexcept { return nWidth; }
/**
* Get bitmap height
* @return bitmap height in pixels
*/
inline size_t height() const noexcept { return nHeight; }
/**
* Get number of bytes per each row
* @return number of bytes per each row
*/
inline size_t stride() const noexcept { return nStride; }
/**
* Get number of bytes per each row
* @return number of bytes per each row
*/
inline size_t bytes_per_row() const noexcept { return nStride; }
/**
* Get row of the bitmap
* @param index number of row
* @return pointer to row or NULL
*/
inline uint8_t *row(size_t index) noexcept { return ((pData != NULL) && (index < nHeight)) ? &pData[nStride * index] : NULL; }
/**
* Get row of the bitmap
* @param index number of row
* @return pointer to row or NULL
*/
inline const uint8_t *row(size_t index) const noexcept { return ((pData != NULL) && (index < nHeight)) ? &pData[nStride * index] : NULL; }
/**
* Get the full bitmap data
* @return full bitmap data
*/
inline uint8_t *data() noexcept { return pData; }
/**
* Get the full bitmap data
* @return full bitmap data
*/
inline const uint8_t *data() const noexcept { return pData; }
/**
* Check that bitmap is empty
* @return true if bitmap is empty
*/
inline bool is_empty() const noexcept { return pData == NULL; }
public:
/**
* Initialize bitmap
* @param format pixel format of bitmap
* @param width image width
* @param height image height
* @return status of operation
*/
status_t init(pixel_format_t format, size_t width, size_t height);
/**
* Set bitmap data
* @param src source bitmap
* @return status of operation
*/
status_t set(const Bitmap & src);
/**
* Set bitmap data
* @param src source bitmap
* @return status of operation
*/
status_t set(const Bitmap * src);
/**
* Reset bitmap content
*/
void reset();
/**
* Resize bitmap. The stored image does not scale, it becomes cropped if new size is smaller.
*
* @param width image width
* @param height image height
* @return status of operation
*/
status_t resize(size_t width, size_t height);
/**
* Convert bitmap to desired pixel format
* @param format desired pixel format
* @return status of operation
*/
status_t convert(pixel_format_t format);
/**
* Convert bitmap to desired pixel format and store to new bitmap
* @param dst destination bitmap to store result
* @param format desired pixel format
* @return status of operation
*/
status_t convert_to(Bitmap & dst, pixel_format_t format) const;
/**
* Convert bitmap to desired pixel format and store to new bitmap
* @param dst destination bitmap to store result
* @param format desired pixel format
* @return status of operation
*/
status_t convert_to(Bitmap *dst, pixel_format_t format) const;
/**
* Convert source bitmap to desired pixel format and store to this bitmap
* @param src source bitmap
* @param format desired pixel format
* @return status of operation
*/
status_t convert_from(const Bitmap & src, pixel_format_t format);
/**
* Convert source bitmap to desired pixel format and store to this bitmap
* @param src source bitmap
* @param format desired pixel format
* @return status of operation
*/
status_t convert_from(const Bitmap * src, pixel_format_t format);
/**
* Load bitmap from file
* @param path path to the file
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(const char *path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
/**
* Load bitmap from file
* @param path path to the file
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(const LSPString *path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
/**
* Load bitmap from file
* @param path path to the file
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(const LSPString & path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
/**
* Load bitmap from file
* @param path path to the file
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(const io::Path *path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
/**
* Load bitmap from memory chunk
* @param data memory chunk data
* @param size size of memory chunk
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(const void *data, size_t size, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
/**
* Load bitmap from file
* @param path path to the file
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(const io::Path & path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
/**
* Load bitmap from input stream
* @param is input stream
* @param format desired pixel format (optional)
* @param map color map (optional)
* @return status of operation
*/
status_t load(io::IInStream *is, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
};
} /* namespace mm */
} /* namespace lsp */
#endif /* LSP_PLUG_IN_MM_BITMAP_H_ */
|