Skip to content

Commit

Permalink
Merge pull request #1 from prismocr/images_load
Browse files Browse the repository at this point in the history
Load image and stuff
  • Loading branch information
aureleoules authored Sep 30, 2020
2 parents 9f6658b + 807adca commit fc362f9
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.out
*.swp

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CC := gcc
CFLAGS := -Wall -Werror -pedantic -Wpointer-arith
LDFLAGS := -Iinclude/

all:
$(CC) src/*.c $(CFLAGS) $(LDFLAGS)

format:
clang-format --style=file -i src/*.c

8 changes: 8 additions & 0 deletions include/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef ERROR_H
#define ERROR_H

const char *get_last_error();
void set_last_error(const char *error);

#endif // ERROR_H

13 changes: 13 additions & 0 deletions include/image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef IMAGE_H
#define IMAGE_H

#include "ndarray.h"

// Load an image in grayscale colorspace into an ndarray
int load_bmp_image(const char *path, ndarray **im);

// read an image and save it
void save_bmp_image(const char *path, ndarray *im);

#endif // IMAGE_H

17 changes: 17 additions & 0 deletions include/ndarray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef NDARRAY_H
#define NDARRAY_H

#include <stddef.h>

typedef struct ndarray ndarray;

struct ndarray {
size_t ndim;
size_t *dim;
float *val;
};

void ndarray_print(const ndarray *a);

#endif // NDARRAY_H

Binary file added lena.bmp
Binary file not shown.
14 changes: 14 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "error.h"
#include "stdlib.h"
#include "string.h"

static const char *last_error = "";

const char *get_last_error() {
return last_error;
}

void set_last_error(const char *error) {
last_error = error;
}

119 changes: 119 additions & 0 deletions src/image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "image.h"
#include "ndarray.h"
#include "error.h"
#include <stdio.h>
#include <stdlib.h>

// --- File header ---

// signature : 2
// file size: 4
// reserved field: 4
// image offset: 4

// 14 bytes

// --- Image header ---

// size of the header: 4
// image width: 4
// image height: 4
// depth: 2
// number of bits/pixels: 2
// compression size: 4
// image size: 4
// horizontal resolution: 4
// vertical resolution: 4
// number of palette colors: 4
// number of important colors: 4

// 40 bytes

// start at 54 bytes

unsigned int read_bmp_qword(FILE *f) {
unsigned char b[4];
fscanf(f, "%c%c%c%c", &b[0], &b[1], &b[2], &b[3]);
return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
}

float color_to_gray(float values[3]) {
int val = ((float) (values[0]) * 0.07f + (float) (values[1]) * 0.71f
+ (float) (values[2]) * 0.21f);
if (val > 255) {
return 255;
} else if (val < 0) {
return 0;
}
return val;
}

float color_to_gray_simple(float values[3]) {
return (values[0] + values[1] + values[2]) / 3;
}

void save_image(const char *path, const char *path_grey, ndarray *im) {
FILE *f;
FILE *fgrey;
f = fopen(path, "rt");
fgrey = fopen(path_grey, "w");

int j, i = 0;
int count = im->dim[0] * im->dim[1];

for (i = 0; i < 54; i++) {
fputc(fgetc(f), fgrey);
}
for (i = 0; i < count; i++) {
for (j = 0; j < 3; j++) {
fputc((int) im->val[i], fgrey);
}
}

fclose(f);
fclose(fgrey);
}

// TODO handle errors correctly

int load_bmp_image(const char *path, ndarray **im) {
FILE *f;
*im = (ndarray *) malloc(sizeof(ndarray));
if (*im == NULL) {
set_last_error("Failed to allocate memory for ndarray");
return 1;
}

(*im)->ndim = 2;
(*im)->dim = (size_t *) malloc(2 * sizeof(size_t));
if ((*im)->dim == NULL) {
set_last_error("Failed to allocate memory for dim");
return 1;
}

f = fopen(path, "rb");
if ((fgetc(f) << 8 | fgetc(f)) != 0x424d) {
set_last_error("Invalid file format");
return 1;
}

fseek(f, 18, SEEK_SET);
(*im)->dim[0] = read_bmp_qword(f);
(*im)->dim[1] = read_bmp_qword(f);

fseek(f, 54, SEEK_SET);

(*im)->val = (float *) malloc((*im)->dim[0] * (*im)->dim[1] * sizeof(float));

int gray_index = 0;

while (!feof(f)) {
(*im)->val[gray_index]
= color_to_gray((float[]){fgetc(f), fgetc(f), fgetc(f)});
gray_index++;
}
fclose(f);

return 0;
}

17 changes: 17 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
#include "ndarray.h"
#include "image.h"
#include "error.h"

int main(int argc, char *argv[]) {
int res;
ndarray *im;
res = load_bmp_image(argv[1], &im);
if (res) {
printf("%s\n", get_last_error());
}
printf("%ld %ld\n", im->dim[0], im->dim[1]);

return 0;
}

7 changes: 7 additions & 0 deletions src/ndarray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include "ndarray.h"

void ndarray_print(const ndarray *a) {

}

0 comments on commit fc362f9

Please sign in to comment.