removed warnings

This commit is contained in:
antifallobst 2023-03-21 17:09:59 +01:00
parent 72785e2ac7
commit aa66c04c17
6 changed files with 49 additions and 41 deletions

View File

@ -3,7 +3,7 @@ project(Juniorcamp C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
add_executable(Juniorcamp src/main.c src/gfx.c) add_executable(Juniorcamp src/main.c src/distance.c src/gfx.c)
target_link_libraries(Juniorcamp imago m X11) target_link_libraries(Juniorcamp imago m X11)
target_include_directories(Juniorcamp PRIVATE inc) target_include_directories(Juniorcamp PRIVATE inc)

BIN
images/ratatouille.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 888 KiB

BIN
images/ratatouille_mask.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

15
inc/distance.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef JUNIORCAMP_DISTANCE_H
#define JUNIORCAMP_DISTANCE_H
#include <stdint.h>
#define MASK_MULTIPLIER 500
typedef struct {
int64_t distance;
int8_t direction;
} distance_T;
int64_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m);
#endif //JUNIORCAMP_DISTANCE_H

23
src/distance.c Normal file
View File

@ -0,0 +1,23 @@
#include "distance.h"
#include <math.h>
int64_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) {
uint8_t r_a = (pixel_a) & 0xFF;
uint8_t g_a = (pixel_a >> 8) & 0xFF;
uint8_t b_a = (pixel_a >> 16) & 0xFF;
uint8_t r_b = (pixel_b) & 0xFF;
uint8_t g_b = (pixel_b >> 8) & 0xFF;
uint8_t b_b = (pixel_b >> 16) & 0xFF;
uint8_t r_m = (pixel_m) & 0xFF;
uint8_t g_m = (pixel_m >> 8) & 0xFF;
int64_t dist = (int64_t)sqrt(pow(r_a - r_b, 2) +
pow(g_a - g_b, 2) +
pow(b_a - b_b, 2)) +
(r_m * MASK_MULTIPLIER) -
(g_m * MASK_MULTIPLIER);
return dist;
}

View File

@ -2,18 +2,11 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
#include <memory.h> #include <memory.h>
#include <imago2.h> #include <imago2.h>
#include <time.h> #include <time.h>
#include "gfx.h" #include "gfx.h"
#include "distance.h"
#define MASK_MULTIPLIER 50
typedef struct {
int32_t distance;
int8_t direction;
} distance_T;
typedef struct { typedef struct {
uint32_t width; uint32_t width;
@ -25,38 +18,12 @@ typedef struct {
uint32_t* mask_buffer; uint32_t* mask_buffer;
} image_T; } image_T;
int32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) {
uint8_t r_a = (pixel_a) & 0xFF;
uint8_t g_a = (pixel_a >> 8) & 0xFF;
uint8_t b_a = (pixel_a >> 16) & 0xFF;
uint8_t r_b = (pixel_b) & 0xFF;
uint8_t g_b = (pixel_b >> 8) & 0xFF;
uint8_t b_b = (pixel_b >> 16) & 0xFF;
uint8_t r_m = (pixel_m) & 0xFF;
uint8_t g_m = (pixel_m >> 8) & 0xFF;
int32_t dist = (int32_t)sqrt(pow(r_a - r_b, 2) +
pow(g_a - g_b, 2) +
pow(b_a - b_b, 2)) +
(r_m * MASK_MULTIPLIER) -
(g_m * MASK_MULTIPLIER);
return dist;
}
image_T* image_load(const char* filename, const char* filename_mask) { image_T* image_load(const char* filename, const char* filename_mask) {
printf("loading '%s'...\n", filename); printf("loading '%s'...\n", filename);
image_T* image = malloc(sizeof(image_T)); image_T* image = malloc(sizeof(image_T));
if (filename_mask != NULL) {
image->mask_buffer = img_load_pixels(filename_mask, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32); image->mask_buffer = img_load_pixels(filename_mask, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32);
} else {
image->mask_buffer = NULL;
}
image->buffer = img_load_pixels(filename, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32); image->buffer = img_load_pixels(filename, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32);
image->dist_buffer = calloc(image->width * image->height, sizeof(distance_T)); image->dist_buffer = calloc(image->width * image->height, sizeof(distance_T));
image->new_width = image->width; image->new_width = image->width;
@ -87,7 +54,7 @@ void image_calculate_dist_buffer(image_T* image) {
for (int y = 1; y < image->new_height; y++) { for (int y = 1; y < image->new_height; y++) {
for (int x = 0; x < image->new_width; x++) { for (int x = 0; x < image->new_width; x++) {
int32_t dist; int64_t dist;
distance_T shortest_dist; distance_T shortest_dist;
if (x > 0) { if (x > 0) {
@ -157,7 +124,7 @@ void image_cut_line(image_T* image, uint32_t line_id) {
memcpy(&image->buffer[(image->width * y) + x], &image->buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t)); memcpy(&image->buffer[(image->width * y) + x], &image->buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t));
if (image->mask_buffer != NULL) memcpy(&image->mask_buffer[(image->width * y) + x], &image->mask_buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t)); if (image->mask_buffer != NULL) memcpy(&image->mask_buffer[(image->width * y) + x], &image->mask_buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t));
gfx_color(0, 0xFF, 0); gfx_color(0, 0xFF, 0);
gfx_point(x, y); gfx_point((int)x, (int)y);
} }
image->new_width -= 1; image->new_width -= 1;
@ -178,7 +145,7 @@ void image_dump(image_T* image) {
gfx_color(0, 0, 0); gfx_color(0, 0, 0);
for (int y = 0; y < image->new_height; y++) { for (int y = 0; y < image->new_height; y++) {
gfx_point(image->new_width, y); gfx_point((int)image->new_width, y);
} }
gfx_flush(); gfx_flush();
@ -188,7 +155,7 @@ void image_export(image_T* image, const char* filename) {
printf("exporting image to '%s'\n", filename); printf("exporting image to '%s'\n", filename);
struct img_pixmap img; struct img_pixmap img;
img_init(&img); img_init(&img);
img_set_pixels(&img, image->new_width, image->new_height, IMG_FMT_RGBA32, NULL); img_set_pixels(&img, (int)image->new_width, (int)image->new_height, IMG_FMT_RGBA32, NULL);
uint32_t* out_buffer = (uint32_t*)img.pixels; uint32_t* out_buffer = (uint32_t*)img.pixels;
@ -200,7 +167,7 @@ void image_export(image_T* image, const char* filename) {
} }
int main(int argc, char* argv[]) { int main() {
printf("Line Cutter - Juniorcamp Informatik\n"); printf("Line Cutter - Juniorcamp Informatik\n");
image_T* image = image_load("../images/paris.png", "../images/paris_mask.png"); image_T* image = image_load("../images/paris.png", "../images/paris_mask.png");
@ -235,6 +202,9 @@ int main(int argc, char* argv[]) {
running = false; running = false;
break; break;
} }
default: {
break;
}
} }
} }