SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <cstddef>
9#include <cstdint>
10#include <vector>
11
12namespace satdump
13{
14 namespace image
15 {
16 class Image
17 {
18 protected:
19 // Image buffer
20 size_t data_size = 0;
21 size_t type_size = 0;
22 void *d_data = nullptr;
23
24 // Size and parameters
25 int d_depth = 0;
26 int d_maxv = 0;
27 size_t d_width = 0;
28 size_t d_height = 0;
29 int d_channels = 0;
30
31 public:
32 // Metadata stuff
33 void *metadata_obj = nullptr; // DO NOT USE DIRECTLY!
34 protected:
35 void copy_meta(const Image &img);
36
37 public:
41 Image();
42
50 Image(int bit_depth, size_t width, size_t height, int channels);
51
62 Image(void *buffer, int bit_depth, size_t width, size_t height, int channels);
63
64 Image(const Image &img); // Copy constructor
65 ~Image(); // Destructor
66
67 Image &operator=(const Image &img);
68
69 public:
73 void to_rgb();
74
78 void to_rgba();
79
83 Image to8bits();
84
89
95 Image to_depth(int bit_depth);
96
97 public:
106 void draw_image(int channel, Image image, int x = 0, int y = 0);
107
117 void draw_image_alpha(Image image, int x = 0, int y = 0);
118
125 void draw_pixel(size_t x, size_t y, std::vector<double> color);
126
135 void draw_line(int x0, int y0, int x1, int y1, std::vector<double> color);
136
145 void draw_circle(int x0, int y0, int radius, std::vector<double> color, bool fill = false);
146
156 void draw_rectangle(int x0, int y0, int x1, int y1, std::vector<double> color, bool fill = true);
157
158 public:
166 void init(int bit_depth, size_t width, size_t height, int channels);
167
171 void clear();
172
173 public:
179 int clamp(int input);
180
186 double clampf(double input);
187
192 void *raw_data() const { return d_data; }
193
198 int depth() const { return d_depth; }
199
204 int typesize() const { return type_size; }
205
210 size_t width() const { return d_width; }
211
216 size_t height() const { return d_height; }
217
222 int channels() const { return d_channels; }
223
228 size_t size() const { return data_size; }
229
234 int maxval() const { return d_maxv; }
235
236 public:
244 void crop(int x0, int y0, int x1, int y1);
245
251 void crop(int x0, int x1);
252
262 Image crop_to(int x0, int y0, int x1, int y1);
263
271 Image crop_to(int x0, int x1);
272
273 public:
279 void mirror(bool x, bool y);
280
281 public:
288 void resize(int width, int height);
289
296 Image resize_to(int width, int height);
297
304 void resize_bilinear(int width, int height, bool text_mode = false);
305
313 int get_pixel_bilinear(int channel, double x, double y);
314
315 public:
320 void fill(int val);
321
326 void fill_color(std::vector<double> color);
327
328 public:
336 void set(size_t p, int v)
337 {
338 if (d_depth > 8)
339 ((uint16_t *)d_data)[p] = v;
340 else
341 ((uint8_t *)d_data)[p] = v;
342 }
343
351 int get(size_t p)
352 {
353 if (d_depth > 8)
354 return ((uint16_t *)d_data)[p];
355 else
356 return ((uint8_t *)d_data)[p];
357 }
358
366 void setf(size_t p, double v)
367 {
368 if (d_depth > 8)
369 ((uint16_t *)d_data)[p] = v * (double)d_maxv;
370 else
371 ((uint8_t *)d_data)[p] = v * (double)d_maxv;
372 }
373
381 double getf(size_t p)
382 {
383 if (d_depth > 8)
384 return (double)((uint16_t *)d_data)[p] / (double)d_maxv;
385 else
386 return (double)((uint8_t *)d_data)[p] / (double)d_maxv;
387 }
388
389 // Easier int set/get
390 void set(size_t channel, size_t p, int v) { set(channel * d_width * d_height + p, v); }
391 int get(size_t channel, size_t p) { return get(channel * d_width * d_height + p); }
392 void set(size_t channel, size_t x, size_t y, int v) { set(channel * d_width * d_height + y * d_width + x, v); }
393 int get(size_t channel, size_t x, size_t y) { return get(channel * d_width * d_height + y * d_width + x); }
394
395 // Easier double setf/getf
396 void setf(size_t channel, size_t p, double v) { setf(channel * d_width * d_height + p, v); }
397 double getf(size_t channel, size_t p) { return getf(channel * d_width * d_height + p); }
398 void setf(size_t channel, size_t x, size_t y, double v) { setf(channel * d_width * d_height + y * d_width + x, v); }
399 double getf(size_t channel, size_t x, size_t y) { return getf(channel * d_width * d_height + y * d_width + x); }
400 };
401
411 void imemcpy(Image &img1, size_t pos1, Image &img2, size_t pos2, size_t px_size);
412
418 void image_to_rgba(Image &img, uint32_t *output);
419 } // namespace image
420} // namespace satdump
421
422// TODOREWORK!!!!! REMOVE. Temporary namespace hack until everything is moved over!
423namespace image
424{
425 using namespace satdump::image;
426} // namespace image
Definition image.h:17
void to_rgb()
Convert this image from B&W to RGB (if it is B&W / RGBA)
Definition image.cpp:127
void resize(int width, int height)
Resize image, using a simple pixel scaling attribution (not the best, but fast)
Definition image.cpp:319
Image()
Init null image, with no data buffer.
Definition image.cpp:13
int typesize() const
Returns image type size (in bytes)
Definition image.h:204
double clampf(double input)
Clamp input value to what this image can handle as float (0, 1)
Definition image.cpp:117
void fill_color(std::vector< double > color)
Fill image with a color.
Definition image.cpp:487
Image resize_to(int width, int height)
Resize image, to another image. Same as resize otherwise.
Definition image.cpp:342
void draw_line(int x0, int y0, int x1, int y1, std::vector< double > color)
Draw a line with Bresenham's algorithm.
Definition image_draw.cpp:96
void mirror(bool x, bool y)
Mirror the image.
Definition image.cpp:278
Image to8bits()
Convert to 8-bits. Returns the current image if it's already 8-bits.
Definition image.cpp:190
Image to_depth(int bit_depth)
Converts to a specific target bit depth.
Definition image.cpp:224
void draw_pixel(size_t x, size_t y, std::vector< double > color)
Set a pixel's color.
Definition image_draw.cpp:87
void draw_circle(int x0, int y0, int radius, std::vector< double > color, bool fill=false)
Draw a circle with Bresenham's Midpoint algorithm.
Definition image_draw.cpp:131
void crop(int x0, int y0, int x1, int y1)
Crop an image region. Must be with x0 <= x1 and y0 <= y1.
Definition image.cpp:232
void fill(int val)
Fill image with a single value.
Definition image.cpp:480
int channels() const
Returns image channel count.
Definition image.h:222
void to_rgba()
Convert this image from to RGBA (if it is B&W / RGB)
Definition image.cpp:149
void * raw_data() const
Returns the RAW void pointer. MUST ONLY BE USED IF YOU KNOW WHAT YOU ARE DOING!
Definition image.h:192
Image to16bits()
Convert to 16-bits. Returns the current image if it's already 16-bits.
Definition image.cpp:207
int maxval() const
Returns image max value.
Definition image.h:234
int clamp(int input)
Clamp input value to what this image can handle.
Definition image.cpp:107
int depth() const
Returns image bit depth.
Definition image.h:198
size_t size() const
Returns image data size.
Definition image.h:228
void resize_bilinear(int width, int height, bool text_mode=false)
Resize image, using a bilinear algorithm.
Definition image.cpp:366
void set(size_t p, int v)
Standard int set. No bound check! Variants to pass a channel index and X / Y position are also availa...
Definition image.h:336
void draw_image(int channel, Image image, int x=0, int y=0)
Draw a B&W Image onto an image channel, or if channel is kept at 0, RGB or RGBA images as well.
Definition image_draw.cpp:8
void init(int bit_depth, size_t width, size_t height, int channels)
Init (empty, filled with zeroes) image buffer.
Definition image.cpp:58
void draw_rectangle(int x0, int y0, int x1, int y1, std::vector< double > color, bool fill=true)
Draw a rectangle onto the image.
Definition image_draw.cpp:200
int get_pixel_bilinear(int channel, double x, double y)
Get a pixel from the image, but with the option to interpolate fractional pixels.
Definition image.cpp:411
int get(size_t p)
Standard int get. No bound check! Variants to pass a channel index and X / Y position are also availa...
Definition image.h:351
void clear()
Delete current buffer (and set size to 0)
Definition image.cpp:90
size_t height() const
Returns image height.
Definition image.h:216
Image crop_to(int x0, int y0, int x1, int y1)
Crop an image region. Must be with x0 <= x1 and y0 <= y1. Provides image as a return value.
Definition image.cpp:259
void draw_image_alpha(Image image, int x=0, int y=0)
Draw an alpha Image onto an image or if channel is kept at 0, RGB or RGBA images as well....
Definition image_draw.cpp:34
size_t width() const
Returns image width.
Definition image.h:210
double getf(size_t p)
Standard float get. No bound check! Variants to pass a channel index and X / Y position are also avai...
Definition image.h:381
void setf(size_t p, double v)
Standard float set. No bound check! Variants to pass a channel index and X / Y position are also avai...
Definition image.h:366