Skip to content

Instantly share code, notes, and snippets.

@MarekKnapek
Created January 18, 2025 23:36
Show Gist options
  • Select an option

  • Save MarekKnapek/46a439a0377d901c2c0a68209ffe50fe to your computer and use it in GitHub Desktop.

Select an option

Save MarekKnapek/46a439a0377d901c2c0a68209ffe50fe to your computer and use it in GitHub Desktop.
CVUT FEL
#include <stdbool.h> /* false */
#include <stdio.h> /* printf */
#include <stdlib.h> /* malloc free exit srand rand NULL */
#include <time.h> /* time_t time */
#define width 12
#define height 8
#define elem_size 3
#define my_check(x) do{ if(!(x)){ print_error_and_exit(#x, __FILE__, __LINE__); } }while(false)
static void print_error_and_exit(char const* const expression, char const* const file, int const line)
{
printf("Failed with expression `%s', in file `%s' at line %d! Exiting the program!\n", expression, file, line);
exit(1);
}
static void canvas_swap(unsigned char* const canvas, int const src_x, int const src_y, int const w, int const h, int const dst_x, int const dst_y)
{
my_check(canvas);
my_check(src_x >= 0 && src_x < width);
my_check(src_y >= 0 && src_y < height);
my_check(w >= 1);
my_check(h >= 1);
my_check(dst_x >= 0 && dst_x < width);
my_check(dst_y >= 0 && dst_y < height);
my_check(src_x + w <= width);
my_check(src_y + h <= height);
my_check(dst_x + w <= width);
my_check(dst_y + h <= height);
my_check((src_x < dst_x && src_x + w <= dst_x) || (dst_x < src_x && dst_x + w <= src_x));
my_check((src_y < dst_y && src_y + h <= dst_y) || (dst_y < src_y && dst_y + h <= src_y));
for(int delta_y = 0; delta_y != h; ++delta_y)
{
for(int delta_x = 0; delta_x != w; ++delta_x)
{
for(int delta_color = 0; delta_color != elem_size; ++delta_color)
{
int const my_src_x = src_x + delta_x;
int const my_src_y = src_y + delta_y;
int const my_dst_x = dst_x + delta_x;
int const my_dst_y = dst_y + delta_y;
unsigned char* const src_color_ptr = &canvas[(((my_src_y * width) + my_src_x) * elem_size) + delta_color];
unsigned char* const dst_color_ptr = &canvas[(((my_dst_y * width) + my_dst_x) * elem_size) + delta_color];
unsigned char color_a;
unsigned char color_b;
unsigned char color_temp;
color_a = *src_color_ptr;
color_b = *dst_color_ptr;
color_temp = color_a;
color_a = color_b;
color_b = color_temp;
*src_color_ptr = color_a;
*dst_color_ptr = color_b;
}
}
}
}
static void canvas_fill_with_random_data(unsigned char* const canvas)
{
my_check(canvas);
for(int y = 0; y != height; ++y)
{
for(int x = 0; x != width; ++x)
{
for(int c = 0; c != elem_size; ++c)
{
canvas[(((y * width) + x) * elem_size) + c] = ((unsigned char)(rand() & 0xff));
}
}
}
}
static void canvas_print(unsigned char const* const canvas)
{
my_check(canvas);
for(int y = 0; y != height; ++y)
{
for(int x = 0; x != width; ++x)
{
for(int c = 0; c != elem_size; ++c)
{
unsigned char const color = canvas[(((y * width) + x) * elem_size) + c];
printf("%02x", color);
}
printf(" ");
}
printf("\n");
}
printf("\n");
}
int main(void)
{
time_t tm = time(NULL); my_check(tm != ((time_t)(-1)));
srand(((unsigned int)(tm)));
unsigned char* const canvas = ((unsigned char*)(malloc(width * height * elem_size))); my_check(canvas);
canvas_fill_with_random_data(canvas);
printf("Before:\n\n");
canvas_print(canvas);
int src_x = 1; /* todo: parse from txt file */
int src_y = 1; /* todo: parse from txt file */
int w = 3; /* todo: parse from txt file */
int h = 2; /* todo: parse from txt file */
int dst_x = 7; /* todo: parse from txt file */
int dst_y = 4; /* todo: parse from txt file */
canvas_swap(canvas, src_x, src_y, w, h, dst_x, dst_y);
printf("After:\n\n");
canvas_print(canvas);
free(canvas);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment