CODE:
#include
#include
#include
#include "image_io.h"
struct image_t read_png(const char* file_name) {
struct image_t result = {0, 0, NULL};
png_structp png_ptr;
png_infop info_ptr;
png_bytepp row_pointers;
png_uint_32 width, height;
int bit_depth, color_type;
uint32_t i, j;
FILE* fp;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fputs("png_create_read_struct error\n", stderr);
return result;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
fputs("png_create_info_struct error\n", stderr);
return result;
}
if ((fp = fopen(file_name, "rb")) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fputs("file open error\n", stderr);
return result;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
fputs("error in libpng\n", stderr);
return result;
}
png_init_io(png_ptr, fp);
png_read_png(png_ptr, info_ptr,
PNG_TRANSFORM_SCALE_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING |
PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_GRAY_TO_RGB, NULL);
row_pointers = png_get_rows(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
if (width > SIZE_MAX / sizeof(struct rgb_t) || height > SIZE_MAX / (sizeof(struct rgb_t) * width)) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
fputs("image too large\n", stderr);
return result;
}
result.data = malloc(sizeof(*result.data) * width * height);
if (result.data == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
fputs("error in libpng\n", stderr);
return result;
}
result.width = width;
result.height = height;
for (i = 0; i r = row_pointers[i][j * 3];
current->g = row_pointers[i][j * 3 + 1];
current->b = row_pointers[i][j * 3 + 2];
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return result;
}
int write_png(struct image_t image, const char* file_name) {
png_structp png_ptr;
png_infop info_ptr;
png_bytep image_data;
png_bytepp row_pointers;
FILE* fp;
uint32_t i, j;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fputs("png_create_write_struct error\n", stderr);
return 0;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fputs("png_create_info_struct error\n", stderr);
return 0;
}
if ((fp = fopen(file_name, "wb")) == NULL) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fputs("file open error\n", stderr);
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
fputs("error in libpng\n", stderr);
return 0;
}
row_pointers = malloc(sizeof(png_bytep) * image.height);
if (row_pointers == NULL) {
fputs("malloc row failed\n", stderr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 0;
}
image_data = malloc(sizeof(png_byte) * 3 * image.width * image.height);
if (image_data == NULL) {
fputs("malloc row failed\n", stderr);
png_destroy_write_struct(&png_ptr, &info_ptr);
free(row_pointers);
fclose(fp);
return 0;
}
for (i = 0; i
#include
#include
#include "image_io.h"
/*
白っぽい → 通れる → 1
黒っぽい → 通れない → 0
*/
char* create_bfs_data_from_image(struct image_t image) {
char* result = malloc(sizeof(char) * image.width * image.height);
uint32_t i;
if (result == NULL) return NULL;
for (i = 0; i = 128 * 3);
}
return result;
}
char* bfs(const char* data, uint32_t width, uint32_t height,
uint32_t sx, uint32_t sy, uint32_t dx, uint32_t dy) {
static const int d[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
char* from = calloc(width * height, sizeof(char));
char* result = calloc(width * height, sizeof(char));
struct queue_t {
uint32_t x, y;
int from;
}* queue = NULL;
size_t queue_start, queue_end;
uint32_t x, y;
int cur_idx;
if (width 0 ? argv[0] : "meiro-solver");
return 1;
}
if (sscanf(argv[3], "%"SCNu32, &sx) != 1 || sscanf(argv[4], "%"SCNu32, &sy) != 1 ||
sscanf(argv[5], "%"SCNu32, &dx) != 1 || sscanf(argv[6], "%"SCNu32, &dy) != 1) {
fputs("invalid coord\n", stderr);
return 1;
}
image = read_png(argv[1]);
if (image.data == NULL) {
fputs("PNG read failed\n", stderr);
return 1;
}
if (image.width
#include
#include
#include "image_io.h"
struct hindo_t {
struct rgb_t color;
uint32_t count;
};
int main(int argc, char* argv[]) {
uint32_t block_size;
uint32_t x, y, i, j;
struct image_t image;
struct hindo_t* hindo;
if (argc != 4) {
fprintf(stderr, "Usage: %s input_file output_file block_size\n",
argc > 0 ? argv[0] : "block-painter");
return 1;
}
if (sscanf(argv[3], "%"SCNu32, &block_size) != 1) {
fputs("invalid block size\n", stderr);
return 1;
}
if (block_size == 0) {
fputs("invalid block size\n", stderr);
return 1;
}
if (block_size > SIZE_MAX / sizeof(struct hindo_t) ||
block_size > SIZE_MAX / (sizeof(struct hindo_t) * block_size)) {
fputs("block size too large\n", stderr);
return 1;
}
hindo = malloc(sizeof(struct hindo_t) * block_size * block_size);
if (hindo == NULL) {
fputs("hindo malloc failed\n", stderr);
return 1;
}
image = read_png(argv[1]);
if (image.data == NULL) {
fputs("PNG read failed\n", stderr);
free(hindo);
return 1;
}
for (y = 0; y = hindo_max) {
hindo[k].color = c;
hindo[k].count = 0;
hindo_max++;
}
hindo[k].count++;
}
}
min_count = hindo[0].count;
min_index = 0;
for (k = 1; k UINT32_MAX - block_size) break;
}
if (y > UINT32_MAX - block_size) break;
}
free(hindo);
if (!write_png(image, argv[2])) {
fputs("PNG write failed\n", stderr);
free(image.data);
return 1;
}
free(image.data);
return 0;
}