OCRudoku  v1.0
Resolve word grid with ease
Loading...
Searching...
No Matches
ImageUtils.h File Reference
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_render.h>
#include <SDL_error.h>
#include <SDL_rect.h>
#include <SDL_surface.h>
Include dependency graph for ImageUtils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define M_PI   acos(-1.0)
 

Functions

void render_image_file (const char *file, SDL_Renderer *renderer, const SDL_Rect *srcrect, const SDL_Rect *dstrect)
 Renders an image file onto a given SDL renderer.
 
void render_full_image_file (const char *file, SDL_Renderer *renderer, const SDL_Rect *dstrect)
 Renders an entire image file onto a given SDL renderer.
 
void render_surface (SDL_Surface *surface, SDL_Renderer *renderer, const SDL_Rect *srcrect, const SDL_Rect *dstrect)
 Renders a given SDL surface onto a given SDL renderer.
 
void * get_pixel_pt (SDL_Surface *surface, size_t x, size_t y)
 Retrieves a pointer to the pixel at the specified coordinates in an SDL surface.
 
Uint32 get_pixel_val (void *pixel, SDL_PixelFormat *format)
 Retrieves the pixel value from a given pixel pointer and SDL pixel format.
 
void set_pixel_val (void *pixel, SDL_PixelFormat *format, Uint32 val)
 Sets the pixel value at a given memory location based on the SDL pixel format.
 
void pixel_to_grayscale (void *pixel, SDL_PixelFormat *format)
 Converts a pixel to grayscale.
 
void pixel_to_black_white (void *pixel, SDL_PixelFormat *format, Uint8 threshold)
 Converts a pixel to black or white based on a threshold.
 
void image_to_black_white (SDL_Surface *surface)
 Converts an entire SDL surface to black and white.
 
SDL_Surface * create_sub_surface (SDL_Surface *surface, SDL_Rect rect)
 Creates a sub-surface from a given SDL surface and rectangle.
 
SDL_Surface * rotate_pixels (SDL_Surface *surface, double angle)
 
double * image_to_bool_array (const char *file, size_t *image_len)
 
SDL_Surface * resize_image (SDL_Surface *source, int new_width, int new_height)
 

Macro Definition Documentation

◆ M_PI

#define M_PI   acos(-1.0)

Function Documentation

◆ create_sub_surface()

SDL_Surface * create_sub_surface ( SDL_Surface * surface,
SDL_Rect rect )

Creates a sub-surface from a given SDL surface and rectangle.

This function creates a new SDL surface that represents a sub-region of the provided surface, defined by the specified rectangle. The pixel data from the original surface within the rectangle is copied to the new sub-surface.

Parameters
surfaceThe original SDL_Surface from which to create the sub-surface.
rectThe SDL_Rect that defines the region of the original surface to be copied.
Returns
A new SDL_Surface representing the sub-region of the original surface.
Note
The function will terminate the program with an error message if the sub-surface cannot be created.

◆ get_pixel_pt()

void * get_pixel_pt ( SDL_Surface * surface,
size_t x,
size_t y )

Retrieves a pointer to the pixel at the specified coordinates in an SDL surface.

This function calculates the memory address of the pixel located at the given (x, y) coordinates within the provided SDL surface and returns a pointer to that pixel.

Parameters
surfaceThe SDL_Surface from which to retrieve the pixel.
xThe x-coordinate of the pixel.
yThe y-coordinate of the pixel.
Returns
A pointer to the pixel at the specified coordinates.
Note
The function assumes that the coordinates are within the bounds of the surface.

◆ get_pixel_val()

Uint32 get_pixel_val ( void * pixel,
SDL_PixelFormat * format )

Retrieves the pixel value from a given pixel pointer and SDL pixel format.

This function extracts the pixel value from the provided pixel pointer based on the specified SDL pixel format. It handles different bytes per pixel (bpp) cases and returns the pixel value as a Uint32.

Parameters
pixelA pointer to the pixel data.
formatThe SDL_PixelFormat structure that describes the format of the pixel.
Returns
The pixel value as a Uint32.
Note
The function assumes that the pixel pointer and format are valid and properly initialized.

◆ image_to_black_white()

void image_to_black_white ( SDL_Surface * surface)

Converts an entire SDL surface to black and white.

This function iterates over each pixel in the provided SDL surface, converts each pixel to black or white based on a fixed threshold, and updates the surface with the new pixel values.

Parameters
surfaceThe SDL_Surface to be converted to black and white.
Note
The function will terminate the program with an error message if the surface cannot be locked.

◆ image_to_bool_array()

double * image_to_bool_array ( const char * file,
size_t * image_len )

◆ pixel_to_black_white()

void pixel_to_black_white ( void * pixel,
SDL_PixelFormat * format,
Uint8 threshold )

Converts a pixel to black or white based on a threshold.

This function takes a pointer to a pixel and its format, converts the pixel's color to grayscale by averaging its red, green, and blue components, and then sets the pixel to either black or white based on the provided threshold.

Parameters
pixelA pointer to the pixel data.
formatThe SDL_PixelFormat structure that describes the format of the pixel.
thresholdThe threshold value used to determine whether the pixel should be black or white.
Note
The function assumes that the pixel pointer and format are valid and properly initialized.

◆ pixel_to_grayscale()

void pixel_to_grayscale ( void * pixel,
SDL_PixelFormat * format )

Converts a pixel to grayscale.

This function takes a pointer to a pixel and its format, converts the pixel's color to grayscale by averaging its red, green, and blue components, and then sets the pixel to the resulting grayscale value.

Parameters
pixelA pointer to the pixel data.
formatThe SDL_PixelFormat structure that describes the format of the pixel.
Note
The function assumes that the pixel pointer and format are valid and properly initialized.

◆ render_full_image_file()

void render_full_image_file ( const char * file,
SDL_Renderer * renderer,
const SDL_Rect * dstrect )

Renders an entire image file onto a given SDL renderer.

This function loads an image from the specified file, creates a texture from the loaded surface, and renders it onto the provided SDL renderer within the specified destination rectangle.

Parameters
fileThe path to the image file to be rendered.
rendererThe SDL_Renderer to render the image onto.
dstrectThe destination rectangle on the renderer where the image will be rendered.
Note
The function will terminate the program with an error message if the image file cannot be loaded, if the texture cannot be created from the surface, or if the rendering fails.

◆ render_image_file()

void render_image_file ( const char * file,
SDL_Renderer * renderer,
const SDL_Rect * srcrect,
const SDL_Rect * dstrect )

Renders an image file onto a given SDL renderer.

This function loads an image from the specified file, creates a texture from the loaded surface, and renders it onto the provided SDL renderer within the specified source and destination rectangles.

Parameters
fileThe path to the image file to be rendered.
rendererThe SDL_Renderer to render the image onto.
srcrectThe source rectangle to be used from the image. If NULL, the entire image is used.
dstrectThe destination rectangle on the renderer where the image will be rendered.
Note
The function will terminate the program with an error message if the image file cannot be loaded, if the texture cannot be created from the surface, or if the rendering fails.

◆ render_surface()

void render_surface ( SDL_Surface * surface,
SDL_Renderer * renderer,
const SDL_Rect * srcrect,
const SDL_Rect * dstrect )

Renders a given SDL surface onto a given SDL renderer.

This function creates a texture from the provided SDL surface and renders it onto the provided SDL renderer within the specified source and destination rectangles.

Parameters
surfaceThe SDL_Surface to be rendered.
rendererThe SDL_Renderer to render the surface onto.
srcrectThe source rectangle to be used from the surface. If NULL, the entire surface is used.
dstrectThe destination rectangle on the renderer where the surface will be rendered.
Note
The function will terminate the program with an error message if the texture cannot be created from the surface, or if the rendering fails.

◆ resize_image()

SDL_Surface * resize_image ( SDL_Surface * source,
int new_width,
int new_height )

Function to resize an image to a new width and height

Parameters
sourceThe source image to resize
new_widthThe new width of the image
new_heightThe new height of the image
Returns
SDL_Surface*: The resized image

◆ rotate_pixels()

SDL_Surface * rotate_pixels ( SDL_Surface * surface,
double angle )

Rotates the pixels of an SDL surface by a given angle.

This function creates a new SDL surface with the dimensions adjusted to fit the rotated image. It then rotates the pixels of the original surface by the specified angle and copies them to the new surface. The background color of the new surface is set to white.

Parameters
surfaceThe original SDL_Surface to be rotated.
angleThe angle in degrees by which to rotate the surface.
Returns
A new SDL_Surface containing the rotated image.
Note
The function will terminate the program with an error message if the rotated surface cannot be created, or if the surfaces cannot be locked.

◆ set_pixel_val()

void set_pixel_val ( void * pixel,
SDL_PixelFormat * format,
Uint32 val )

Sets the pixel value at a given memory location based on the SDL pixel format.

This function assigns a new pixel value to the specified memory location, interpreting the memory layout according to the provided SDL pixel format. It handles different bytes per pixel (bpp) cases and sets the pixel value accordingly.

Parameters
pixelA pointer to the memory location where the pixel value should be set.
formatThe SDL_PixelFormat structure that describes the format of the pixel.
valThe new pixel value to be set.
Note
The function assumes that the pixel pointer and format are valid and properly initialized.