Commit 70086375 authored by Florimond RAMEL's avatar Florimond RAMEL

Initial commit

parents
#ifndef IMAGE_H
#define IMAGE_H
typedef struct{
int w,h,c;
float *data;
} image;
// Basic operations
float get_pixel(image im, int x, int y, int c);
void set_pixel(image im, int x, int y, int c, float v);
image copy_image(image im);
image rgb_to_grayscale(image im);
void rgb_to_hsv(image im);
void hsv_to_rgb(image im);
void shift_image(image im, int c, float v);
void clamp_image(image im);
void crop_image_yolo(image *im, image *crop, float x, float y, float w, float h);
// Loading and saving
image make_image(int w, int h, int c);
image load_image(char *filename);
void save_image(image im, const char *name);
void free_image(image im);
#endif
// You probably don't want to edit this file
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
image make_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = calloc(h*w*c, sizeof(float));
return out;
}
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void save_image_stb(image im, const char *name)
{
char buff[256];
sprintf(buff, "%s.jpg", name);
unsigned char *data = calloc(im.w*im.h*im.c, sizeof(char));
int i,k;
for(k = 0; k < im.c; ++k){
for(i = 0; i < im.w*im.h; ++i){
data[i*im.c+k] = (unsigned char) roundf((255*im.data[i + k*im.w*im.h]));
}
}
//int success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c);
int success = stbi_write_jpg(buff, im.w, im.h, im.c, data, 100);
free(data);
if(!success) fprintf(stderr, "Failed to write image %s\n", buff);
}
void save_image(image im, const char *name)
{
save_image_stb(im, name);
}
//
// Load an image using stb
// channels = [0..4]
// channels > 0 forces the image to have that many channels
//
image load_image_stb(char *filename, int channels)
{
int w, h, c;
unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
if (!data) {
fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n",
filename, stbi_failure_reason());
exit(0);
}
if (channels) c = channels;
int i,j,k;
image im = make_image(w, h, c);
for(k = 0; k < c; ++k){
for(j = 0; j < h; ++j){
for(i = 0; i < w; ++i){
int dst_index = i + w*j + w*h*k;
int src_index = k + c*i + c*w*j;
im.data[dst_index] = (float)data[src_index]/255.;
}
}
}
//We don't like alpha channels, #YOLO
if(im.c == 4) im.c = 3;
free(data);
return im;
}
image load_image(char *filename)
{
image out = load_image_stb(filename, 0);
return out;
}
void free_image(image im)
{
free(im.data);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "image.h"
float get_pixel(image im, int x, int y, int c)
{
if (x < im.w && y < im.h && c < im.c)
{
return im.data[c * im.h * im.w + y * im.w + x];
}
return 0;
}
void set_pixel(image im, int x, int y, int c, float v)
{
if (x < 0 || y < 0 || c < 0 || x >= im.w || y >= im.h || c >= im.c)
{
return;
}
im.data[c * im.h * im.w + y * im.w + x] = v;
}
static void crop_copy(image *im, image *crop, int x, int y)
{
int i;
int j;
int k;
i = 0;
while (i < crop->c)
{
j = 0;
while (j < crop->h)
{
k = 0;
while (k < crop->w)
{
crop->data[k + j * crop->w + i * crop->w * crop->h] = im->data[(k + x) + (j + y) * im->w + i * im->w * im->h];
++k;
}
++j;
}
++i;
}
}
void crop_image_yolo(image *im, image *crop, float x, float y, float w, float h)
{
crop->w = (int)(im->w * w);
crop->h = (int)(im->h * h);
crop->c = im->c;
crop->data = (float *)malloc(sizeof(float) * crop->w * crop->h * crop->c);
crop_copy(im, crop, (int)(im->w * x - crop->w / 2), (int)(im->h * y - crop->h / 2));
//printf("x= %f -> %d\n", x, im->w);
//printf("x= %f -> %d\n", im->w * x, (int)(im->w * x));
}
image copy_image(image im)
{
int end;
image copy = make_image(im.w, im.h, im.c);
end = im.w * im.h * im.c;
for (int i=0; i < end; i++)
{
copy.data[i] = im.data[i];
}
return copy;
}
image rgb_to_grayscale(image im)
{
assert(im.c == 3);
image gray = make_image(im.w, im.h, 1);
// TODO Fill this in
return gray;
}
void shift_image(image im, int c, float v)
{
// TODO Fill this in
}
void clamp_image(image im)
{
// TODO Fill this in
}
// These might be handy
float three_way_max(float a, float b, float c)
{
return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
}
float three_way_min(float a, float b, float c)
{
return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
}
void rgb_to_hsv(image im)
{
// TODO Fill this in
}
void hsv_to_rgb(image im)
{
// TODO Fill this in
}
This diff is collapsed.
This diff is collapsed.
#include "image.h"
int main(void)
{
image im;
image cpy;
im = load_image("image.jpg");
crop_image_yolo(&im, &cpy, .5, .5, .1, .1);
// cpy = copy_image(im);
save_image(cpy, "out");
free_image(im);
free_image(cpy);
return (0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment