-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_img.c
79 lines (67 loc) · 2.06 KB
/
c_img.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "c_img.h"
#include <stdio.h>
#include <math.h>
void create_img(struct rgb_img **im, size_t height, size_t width){
*im = (struct rgb_img *)malloc(sizeof(struct rgb_img));
(*im)->height = height;
(*im)->width = width;
(*im)->raster = (uint8_t *)malloc(3 * height * width);
}
int read_2bytes(FILE *fp){
uint8_t bytes[2];
fread(bytes, sizeof(uint8_t), 1, fp);
fread(bytes+1, sizeof(uint8_t), 1, fp);
return ( ((int)bytes[0]) << 8) + (int)bytes[1];
}
void write_2bytes(FILE *fp, int num){
uint8_t bytes[2];
bytes[0] = (uint8_t)((num & 0XFFFF) >> 8);
bytes[1] = (uint8_t)(num & 0XFF);
fwrite(bytes, 1, 1, fp);
fwrite(bytes+1, 1, 1, fp);
}
void read_in_img(struct rgb_img **im, char *filename){
FILE *fp = fopen(filename, "rb");
size_t height = read_2bytes(fp);
size_t width = read_2bytes(fp);
create_img(im, height, width);
fread((*im)->raster, 1, 3*width*height, fp);
}
void write_img(struct rgb_img *im, char *filename){
FILE *fp = fopen(filename, "wb");
write_2bytes(fp, im->height);
write_2bytes(fp, im->width);
fwrite(im->raster, 1, im->height * im->width * 3, fp);
fclose(fp);
}
uint8_t get_pixel(struct rgb_img *im, int y, int x, int col){
return im->raster[3 * (y*(im->width) + x) + col];
}
void set_pixel(struct rgb_img *im, int y, int x, int r, int g, int b){
im->raster[3 * (y*(im->width) + x) + 0] = r;
im->raster[3 * (y*(im->width) + x) + 1] = g;
im->raster[3 * (y*(im->width) + x) + 2] = b;
}
void destroy_image(struct rgb_img *im)
{
free(im->raster);
free(im);
}
void print_grad(struct rgb_img *grad){
int height = grad->height;
int width = grad->width;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
printf("%d\t", get_pixel(grad, i, j, 0));
}
printf("\n");
}
}
void print_best(double * best, size_t height, size_t width) {
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
printf("%f\t", best[i*width + j]);
}
printf("\n");
}
}