Img2Num C
API Documentation
Loading...
Searching...
No Matches
cimg2num.cpp File Reference
#include "cimg2num.h"
#include "img2num.h"
#include "img2num/Error.h"
#include <cstring>
+ Include dependency graph for cimg2num.cpp:

Go to the source code of this file.

Functions

img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default (void)
 
void img2num_gaussian_blur_fft (uint8_t *image, size_t width, size_t height, double sigma)
 Apply a Gaussian blur to an image using FFT.
 
void img2num_invert_image (uint8_t *image, int width, int height)
 Invert the pixel values of an image.
 
void img2num_threshold_image (uint8_t *ptr, const int width, const int height, const int num_thresholds)
 Apply a thresholding operation to an image.
 
void img2num_black_threshold_image (uint8_t *ptr, const int width, const int height, const int num_thresholds)
 Apply black-thresholding to an image.
 
void img2num_kmeans (const uint8_t *data, uint8_t *out_data, int32_t *out_labels, const int32_t width, const int32_t height, const int32_t k, const int32_t max_iter, const uint8_t color_space)
 Perform k-means clustering on image data.
 
void img2num_bilateral_filter (uint8_t *image, size_t width, size_t height, double sigma_spatial, double sigma_range, uint8_t color_space)
 Apply bilateral filtering to an image.
 
char * img2num_labels_to_svg (const uint8_t *data, const int32_t *labels, const int width, const int height, const int min_area, const int min_thickness)
 Convert labeled regions of an image into an SVG string.
 
char * img2num_image_to_svg (const uint8_t *data, const int width, const int height, const img2num_ImageToSvgConfig *config)
 Convert labeled regions of an image into an SVG string.
 

Function Documentation

◆ img2num_bilateral_filter()

void img2num_bilateral_filter ( uint8_t *  image,
size_t  width,
size_t  height,
double  sigma_spatial,
double  sigma_range,
uint8_t  color_space 
)

Apply bilateral filtering to an image.

Parameters
imagePointer to RGBA pixel buffer.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
sigma_spatialStandard deviation for spatial Gaussian (proximity weight).
sigma_rangeStandard deviation for range Gaussian (intensity similarity weight).
color_spaceColor space flag (0 = CIE LAB, 1 = RGB).
Note
The filter modifies the image buffer in-place.
Dox File: doxygen/img2num.h.dox

Definition at line 80 of file cimg2num.cpp.

83 {
84 img2num::clear_last_error_and_catch(
85 img2num::bilateral_filter, image, width, height, sigma_spatial, sigma_range, color_space
86 );
87}

◆ img2num_black_threshold_image()

void img2num_black_threshold_image ( uint8_t *  ptr,
const int  width,
const int  height,
const int  num_thresholds 
)

Apply black-thresholding to an image.

Parameters
ptrPointer to the image buffer.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
num_thresholdsNumber of thresholds to apply.
Note
Similar to threshold_image but prioritizes darker pixels.
Dox File: doxygen/img2num.h.dox

Definition at line 63 of file cimg2num.cpp.

65 {
66 img2num::clear_last_error_and_catch(
67 img2num::black_threshold_image, ptr, width, height, num_thresholds
68 );
69}

◆ img2num_gaussian_blur_fft()

void img2num_gaussian_blur_fft ( uint8_t *  image,
size_t  width,
size_t  height,
double  sigma 
)

Apply a Gaussian blur to an image using FFT.

Parameters
imagePointer to the image buffer (RGBA).
widthWidth of the image in pixels.
heightHeight of the image in pixels.
sigmaStandard deviation for Gaussian kernel.
Note
The operation modifies the image buffer in-place.
Dox File: doxygen/img2num.h.dox

Definition at line 47 of file cimg2num.cpp.

47 {
48 img2num::clear_last_error_and_catch(img2num::gaussian_blur_fft, image, width, height, sigma);
49}

◆ img2num_image_to_svg()

char * img2num_image_to_svg ( const uint8_t *  data,
const int  width,
const int  height,
const img2num_ImageToSvgConfig config 
)

Convert labeled regions of an image into an SVG string.

Parameters
dataPointer to image data buffer.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
configimg2num_ImageToSvgConfig Configuration Struct.

‍See img2num::ImageToSvgConfig.

Returns
std::string An SVG string containing data roughly approximate to the input image.
Note
Dox File: doxygen/img2num.h.dox

Definition at line 109 of file cimg2num.cpp.

111 {
113
114 const img2num_ImageToSvgConfig& cfg {config ? *config : default_cfg};
115
116 char* result {nullptr};
117
118 img2num::clear_last_error_and_catch(
119 [&](const uint8_t* d, const int w, const int h) {
120 std::string svg {img2num::image_to_svg(d, w, h, to_cpp(cfg))};
121
122 result = static_cast<char*>(std::malloc(svg.size() + 1));
123 if (!result) {
124 return; // Allocation failed
125 }
126 std::memcpy(result, svg.c_str(), svg.size() + 1);
127 },
128 data, width, height
129 );
130
131 return result;
132}
img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default(void)
Definition cimg2num.cpp:42
Configuration options for image_to_svg.
Definition cimg2num.h:21

References img2num_ImageToSvgConfig_default().

+ Here is the call graph for this function:

◆ img2num_ImageToSvgConfig_default()

img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default ( void  )

Definition at line 42 of file cimg2num.cpp.

42 {
43 // Use the defaults from the C++ struct
44 return to_c(img2num::ImageToSvgConfig {});
45}

Referenced by img2num_image_to_svg().

+ Here is the caller graph for this function:

◆ img2num_invert_image()

void img2num_invert_image ( uint8_t *  ptr,
int  width,
int  height 
)

Invert the pixel values of an image.

Parameters
ptrPointer to the image buffer.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
Note
Each pixel value is replaced by 255 - original_value.
Dox File: doxygen/img2num.h.dox

Definition at line 51 of file cimg2num.cpp.

51 {
52 img2num::clear_last_error_and_catch(img2num::invert_image, image, width, height);
53}

◆ img2num_kmeans()

void img2num_kmeans ( const uint8_t *  data,
uint8_t *  out_data,
int32_t *  out_labels,
const int32_t  width,
const int32_t  height,
const int32_t  k,
const int32_t  max_iter,
const uint8_t  color_space 
)

Perform k-means clustering on image data.

Parameters
dataPointer to input image data buffer.
out_dataPointer to output buffer where clustered pixel values are stored.
out_labelsPointer to output buffer for cluster labels per pixel.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
kNumber of clusters to compute.
max_iterMaximum number of iterations for the algorithm.
color_spaceColor space flag (0 = CIE LAB, 1 = RGB).
Note
The function does not modify the input buffer.
Dox File: doxygen/img2num.h.dox

Definition at line 71 of file cimg2num.cpp.

74 {
75 img2num::clear_last_error_and_catch(
76 img2num::kmeans, data, out_data, out_labels, width, height, k, max_iter, color_space
77 );
78}

◆ img2num_labels_to_svg()

char * img2num_labels_to_svg ( const uint8_t *  data,
const int32_t *  labels,
const int  width,
const int  height,
const int  min_area,
const int  min_thickness 
)

Convert labeled regions of an image into an SVG string.

Parameters
dataPointer to image data buffer.
labelsPointer to label buffer, indicating region for each pixel.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
min_areaMinimum area (in pixels) for a region to be included in the SVG.
Returns
std::string A valid SVG string containing the data.
Note
Dox File: doxygen/img2num.h.dox

Definition at line 89 of file cimg2num.cpp.

92 {
93 char* result {nullptr};
94 img2num::clear_last_error_and_catch(
95 [&](const uint8_t* d, const int32_t* l, const int w, const int h, const int min_a,
96 const int min_t) {
97 std::string svg {img2num::labels_to_svg(d, l, w, h, min_a, min_t)};
98 result = static_cast<char*>(std::malloc(svg.size() + 1));
99 if (!result) {
100 return; // Allocation failed
101 }
102 std::memcpy(result, svg.c_str(), svg.size() + 1);
103 },
104 data, labels, width, height, min_area, min_thickness
105 );
106 return result;
107}

◆ img2num_threshold_image()

void img2num_threshold_image ( uint8_t *  ptr,
const int  width,
const int  height,
const int  num_thresholds 
)

Apply a thresholding operation to an image.

Parameters
ptrPointer to the image buffer.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
num_thresholdsNumber of thresholds to apply.
Note
Thresholds split pixel intensity ranges into discrete levels.
Dox File: doxygen/img2num.h.dox

Definition at line 55 of file cimg2num.cpp.

57 {
58 img2num::clear_last_error_and_catch(
59 img2num::threshold_image, ptr, width, height, num_thresholds
60 );
61}