Skip to content
Snippets Groups Projects
Commit c37b9880 authored by damian.boquetec's avatar damian.boquetec
Browse files

TP avancé à 15%

parent be83e800
Branches
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ LINKS = -lm
SANITIZERS = -g -fsanitize=address -fsanitize=leak
pgm_exe: pgm_main.o pgm.o
pgm_exe: pgm_main.o pgm.o matrix.o
$(CC) $^ -o $@ $(LINKS) $(SANITIZERS)
......@@ -15,8 +15,12 @@ pgm.o: pgm.c pgm.h
$(CC) $^ -c $<
matrix.o: matrix.c matrix.h
$(CC) $^ -c $<
clean:
rm -f *.o
rm -f *.gch
rm -f pgm_exe
......
File added
matrix.c 0 → 100644
#include "matrix.h"
error_code matrix_alloc(matrix *mat, int32_t l, int32_t c){
mat->l = l;
mat->c = c;
if (l > 0 && c > 0){
//On utilise la taille d'un pointer d'int32_t car on veut avoir assez de place pour stocker une ADRESSE, et non pas un int32_t !
mat->data = malloc(l * sizeof(int32_t *));
if (mat->data == NULL){
return memory_error;
}
for (int i = 0; i < l; i++){
mat->data[i] = malloc(c * sizeof(int32_t));
if (mat->data[i] == NULL){
return memory_error;
}
}
}else{
return out_of_bounds;
}
return ok;
}
error_code matrix_init(matrix *mat, int32_t l, int32_t c, int32_t val){
mat->l = l;
mat->c = c;
if (l > 0 && c > 0){
mat->data = malloc(l * sizeof(int32_t*));
for (int i = 0; i < l; i++){
mat->data[i] = malloc(c * sizeof(int32_t));
for (int j = 0; j < c; j++){
mat->data[i][j] = val;
}
}
return ok;
}else{
return out_of_bounds;
}
}
error_code matrix_destroy(matrix *mat){
for (int i = 0; i < mat->l; i++){
free(mat->data[i]);
mat->data[i] = NULL;
}
free(mat->data);
mat->data = NULL;
mat->l = -1;
mat->c = -1;
return ok;
}
error_code matrix_init_from_array(matrix *mat, int32_t l, int32_t c, int32_t data[], int32_t s){
if (s != l * c){
return out_of_bounds;
}
error_code err = matrix_alloc(mat, l, c);
if (err != ok){
return err;
}
for (int i = 0, a = 0; i < l; i++){
for (int j = 0; j < c; j++, a++){
mat->data[i][j] = data[a];
}
}
return ok;
}
error_code matrix_clone(matrix *cloned, const matrix mat){
if(mat.data == NULL){
return uninitialized;
}
error_code err = matrix_alloc(cloned, mat.l, mat.c);
if (err != ok){
return err;
}
for (int i = 0; i < cloned->l; i++){
for (int j = 0; j < cloned->c; j++){
cloned->data[i][j] = mat.data[i][j];
}
}
return ok;
}
error_code matrix_transpose(matrix *transposed,const matrix mat){
if(mat.data == NULL){
return uninitialized;
}
error_code err = matrix_alloc(transposed, mat.c, mat.l);
if (err != ok){
return err;
}
for(int i = 0; i < transposed->l; i++){
for (int j = 0; j < transposed->c; j++){
transposed->data[i][j] = mat.data[j][i];
}
}
return ok;
}
error_code matrix_print(const matrix mat){
// TODO: handle errors !
if(mat.data != NULL){
if (mat.c > 0 && mat.l > 0){
for (int i = 0; i < mat.l; i++){
for (int y = 0; y < mat.c; y++){
printf("%d ", mat.data[i][y]);
}
printf("\n");
}
return ok;
}else{
return out_of_bounds;/*en réalité c'est plutôt une erreur de taille de matrice illégale*/
}
}else{
return uninitialized;
}
}
/*error_code matrix_extract_submatrix(matrix *sub,const matrix mat, int32_t l0, int32_t l1, int32_t c0, int32_t c1)
l'énoncé n'étant pas clair, je vais faire cette fonction selon ce que j'ai compris*/
error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t l, int32_t c){
if (mat.data == NULL){
return uninitialized;
}
if (mat.c < c || mat.l < l){
return out_of_bounds;
}
error_code err = matrix_alloc(sub, l, c);
if (err != ok){
return err;
}
for(int i = 0; i < sub->l; i++){
for(int j = 0; j < sub->c; j++){
sub->data[i][j] = mat.data[i][j];
}
}
return ok;
}
//Les params ne devraient pas être const ?
bool matrix_is_equal(matrix mat1, matrix mat2){
if (mat1.c == mat2.c && mat1.l == mat2.l){
for (int i = 0; i < mat1.l; i++){
for (int j = 0; j < mat1.c; j++){
if (mat1.data[i][j] != mat2.data[i][j]){
return false;
}
}
}
return true;
}else{
return false;
}
}
error_code matrix_get(int32_t *elem, const matrix mat, int32_t il, int32_t ic){
if (mat.data == NULL){
return uninitialized;
}
if (il >= mat.l || ic >= mat.c){
return out_of_bounds;
}
for (int i = 0; i < mat.l; i++){
for (int j = 0; j < mat.c; j++){
if (i == il && j == ic){
*elem = mat.data[i][j];
}
}
}
return ok;
}
error_code matrix_set(matrix mat, int32_t il, int32_t ic, int32_t elem){
if (mat.data == NULL){
return uninitialized;
}
if (il >= mat.l || ic >= mat.c){
return out_of_bounds;
}
for (int i = 0; i < mat.l; i++){
for (int j = 0; j < mat.c; j++){
if (i == il && j == ic){
mat.data[i][j] = elem;
}
}
}
return ok;
}
error_code matrix_map_ip(matrix mat, void (*foo)(int *)){
if (mat.data == NULL){
return uninitialized;
}
for(int i = 0; i < mat.l; i++){
for(int j = 0; j < mat.c; j++){
foo(&mat.data[i][j]);
}
}
return ok;
}
error_code matrix_map(matrix *mapped, const matrix mat, void (*foo)(int *)){
if (mat.data == NULL){
return uninitialized;
}
error_code err = matrix_alloc(mapped, mat.l, mat.c);
if (err != ok){
return err;
}
for(int i = 0; i < mat.l; i++){
for(int j = 0; j < mat.c; j++){
int value = mat.data[i][j];
foo(&value);
mapped->data[i][j] = value;
}
}
return ok;
}
void print_error_name(uint8_t error){
switch (error)
{
case 0:
printf("'ok' \n");
break;
case 1:
printf("'out of bounds' \n");
break;
case 2:
printf("'memory error' \n");
break;
case 3:
printf("'uninitialised'\n");
break;
}
}
void plus_one(uint32_t *value){
*value += 1;
}
\ No newline at end of file
matrix.h 0 → 100644
#ifndef MATRIX_H
#define MATRIX_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct matrix {
int32_t l, c;
int32_t ** data;
} matrix;
typedef enum error_code {
ok, out_of_bounds, memory_error, uninitialized
} error_code;
error_code matrix_alloc(matrix *mat, int32_t l, int32_t c);
error_code matrix_init(matrix *mat, int32_t l, int32_t c, int32_t val);
error_code matrix_destroy(matrix *mat);
error_code matrix_init_from_array(matrix *mat, int32_t l, int32_t c, int32_t data[], int32_t s);
error_code matrix_clone(matrix *cloned, const matrix mat);
error_code matrix_transpose(matrix* transposed,const matrix mat);
error_code matrix_print(const matrix mat);
//error_code matrix_extract_submatrix(matrix *sub,const matrix mat, int32_t l0, int32_t l1, int32_t c0, int32_t c1);
error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t l, int32_t c);
bool matrix_is_equal(matrix mat1, matrix mat2);
error_code matrix_get(int32_t *elem, const matrix mat, int32_t il, int32_t ic);
error_code matrix_set(matrix mat, int32_t il, int32_t ic, int32_t elem);
error_code matrix_map_ip(matrix mat, void (*foo)(int *));
error_code matrix_map(matrix *mapped, const matrix mat, void (*foo)(int *));
void print_error_name(uint8_t error);
void plus_one(uint32_t *value);
#endif
\ No newline at end of file
......@@ -3,7 +3,27 @@
pgm_error pgm_read_from_file(pgm *p, char *filename){
if (filename == NULL || p == NULL){
return failure;
}
char header[100];
int px_lin, px_col, px_max;
//something is buggy over here...
FILE *fp = fopen(filename, "rb");
matrix_alloc(&(p->pixels), px_lin, px_col);
fscanf(fp, "%s %d %d %d", header, &px_lin, &px_col, &px_max);
for (int i = 0; i < px_lin; i++){
for (int j = 0; i < px_col; j++){
uint8_t tmp;
fread(&tmp, sizeof(uint8_t), 1, fp);
printf("%d", tmp);
p->pixels.data[i][j] = (int)tmp;
}
}
fclose(fp);
return success;
}
/*
......
......@@ -3,10 +3,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdint.h>
#include "matrix.h"
typedef struct _pgm{
int32_t max;
matrix pixels;
......@@ -26,7 +25,4 @@ pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig);
pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int32_t y0, int32_t y1);
pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel);
#endif
\ No newline at end of file
#include "pgm.h"
int main(){
pgm a;
pgm_read_from_file(&a, "mandrill.pgm");
matrix_print(a.pixels);
matrix_destroy(&a.pixels);
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment