Skip to content
Snippets Groups Projects
Commit fd0927b7 authored by Vincent Andrey's avatar Vincent Andrey
Browse files

Commit des fichiers du tp, il ne manque que le SDL et commentaires

parents
No related branches found
No related tags found
No related merge requests found
*.o
\ No newline at end of file
P5
3 3
255
slhg`]aZW
File added
File added
This diff is collapsed.
File added
File added
Source diff could not be displayed: it is too large. Options to address this: view the blob.
File added
File added
Makefile 0 → 100644
CC = gcc -Wall -Wextra -g -fsanitize=address -fsanitize=leak -lm
TraitImage: TraitImage.o TraitImageMain.o matrix.o
$(CC) $^ -o $@
TraitImage.o: TraitImage.c TraitImage.h
$(CC) $^ -c $<
matrix.o: matrix.c matrix.h
$(CC) $^ -c $<
TraitImageMain.o: TraitImageMain.c
$(CC) $^ -c $<
clean:
rm -f *.o TraitImage
rebuild: clean TraitImage
\ No newline at end of file
File added
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include "matrix.h"
#include "TraitImage.h"
pgm_error pgm_read_from_file(pgm *p, char *filename){
FILE *f = fopen(filename, "r");
if (f==NULL){
printf("Ce fichier n'existe pas.\n");
return failure;
}
char tab[20];
int lines;
int columns;
int max;
fscanf(f,"%s %d %d %d", tab, &lines, &columns, &max);
printf("%s\n%d\n%d\n%d\n", tab, lines, columns, max);
p->max = max;
matrix_alloc(&(p->pixels), lines, columns);
for(int i = 0; i < lines; i++){
for(int k = 0; k < columns; k++){
uint8_t tmp = 0;
fread(&tmp,sizeof(uint8_t),1,f);
p->pixels.data[i][k] = (int)tmp;
}
}
//matrix_print(p->pixels);
fclose(f);
return success;
}
pgm_error pgm_write_to_file(pgm *p, char *filename){
FILE *fp = fopen(filename, "w");
fprintf(fp, "P5\n%d %d\n%d\n", p->pixels.n, p->pixels.m, p->max);
for(int i = 0; i <p->pixels.m; i++){
for(int k = 0; k < p->pixels.n; k++){
uint8_t tmp = p->pixels.data[i][k];
fwrite(&tmp,sizeof(uint8_t),1,fp);
}
}
fclose(fp);
matrix_destroy(&p->pixels);
return success;
}
pgm_error pgm_negative(pgm *neg, const pgm *const orig){
matrix_alloc(&(neg->pixels), orig->pixels.m, orig->pixels.n);
neg->max = orig->max;
for(int i = 0; i < orig->pixels.m; i++){
for(int k = 0; k < orig->pixels.n; k++){
neg->pixels.data[i][k] = orig->max - 1 - orig->pixels.data[i][k];
}
}
return success;
}
pgm_error pgm_symmetry_hori(pgm *sym, const pgm *const orig){
matrix_alloc(&(sym->pixels), orig->pixels.m, orig->pixels.n);
sym->max = orig->max;
for(int i = 0; i < orig->pixels.m; i++){
for(int k = 0; k < orig->pixels.n; k++){
sym->pixels.data[i][k] = orig->pixels.data[i][sym->pixels.m-k-1];
}
}
return success;
}
pgm_error pgm_symmetry_vert(pgm *sym, const pgm *const orig){
matrix_alloc(&(sym->pixels), orig->pixels.m, orig->pixels.n);
sym->max = orig->max;
for(int i = 0; i < orig->pixels.m; i++){
for(int k = 0; k < orig->pixels.n; k++){
sym->pixels.data[i][k] = orig->pixels.data[sym->pixels.m-i-1][k];
}
}
return success;
}
pgm_error pgm_symmetry_cent(pgm *sym, const pgm *const orig){
matrix_alloc(&(sym->pixels), orig->pixels.m, orig->pixels.n);
sym->max = orig->max;
for(int i = 0; i < orig->pixels.m; i++){
for(int k = 0; k < orig->pixels.n; k++){
sym->pixels.data[i][k] = orig->pixels.data[sym->pixels.m-i-1][sym->pixels.m-k-1];
}
}
return success;
}
pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig){
matrix_alloc(&(photomaton->pixels), orig->pixels.m, orig->pixels.n);
photomaton->max = orig->max;
int ix = 0;
int iy = 0;
for(int i = 0; i < orig->pixels.m; i+=2){ //Va aux deux lignes de pixels suivants à chaque boucle
iy = 0;
for(int k = 0; k < orig->pixels.n; k+=2){ //Va au groupe de 2x2 pixels suivant sur les deux lignes actuelles à chaque boucle
photomaton->pixels.data[ix][iy] = orig->pixels.data[i][k]; //Pixel en haut à gauche (groupes pixels 2x2),
photomaton->pixels.data[((orig->pixels.m)/2)+ix][iy] = orig->pixels.data[i+1][k]; //Pixel bas gauche
photomaton->pixels.data[ix][((orig->pixels.n)/2)+iy] = orig->pixels.data[i][k+1]; //pixel haut droite
photomaton->pixels.data[((orig->pixels.m)/2)+ix][((orig->pixels.n)/2)+iy] = orig->pixels.data[i+1][k+1]; //pixel bas droite
iy++;
}
ix++;
}
return success;
}
pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int32_t y0, int32_t y1){
crop->max = orig->max;
matrix_extract_submatrix(&(crop->pixels), orig->pixels, x0,x1,y0,y1);
return success;
}
pgm_error pgm_conv(pgm *conv, const pgm *const orig, const matrix *const kernel, int32_t norme){
matrix_alloc(&(conv->pixels), orig->pixels.m, orig->pixels.n);
conv->max = orig->max;
for(int i=0; i < orig->pixels.m; i++){
for (int k=0; k < orig->pixels.n; k++){
int pixel_val = 0;
for(int x = -(kernel->m/2); x < (kernel->m/2) + 1; x++){
for(int y = -(kernel->n/2); y < (kernel->n/2) + 1; y++){
if((i + x < 0) || (i + x > orig->pixels.m - 1) || (k + y <0) || (k + y >orig->pixels.n - 1)){
pixel_val += 0;
}
else{
pixel_val += orig->pixels.data[i+x][k+y]*kernel->data[x+(kernel->m/2)][y+(kernel->n/2)]/norme;
}
}
}
if(pixel_val>conv->max){
pixel_val=conv->max;
}
else if(pixel_val<0){
pixel_val=0;
}
conv->pixels.data[i][k] = pixel_val;
}
}
return success;
}
\ No newline at end of file
#include "matrix.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef enum _pgm_error {
success, failure
} pgm_error;
typedef struct _pgm{
int32_t max;
matrix pixels;
} pgm;
pgm_error pgm_read_from_file(pgm *p, char *filename);
pgm_error pgm_write_to_file(pgm *p, char *filename);
pgm_error pgm_negative(pgm *neg, const pgm *const orig);
pgm_error pgm_symmetry_hori(pgm *sym, const pgm *const orig);
pgm_error pgm_symmetry_vert(pgm *sym, const pgm *const orig);
pgm_error pgm_symmetry_cent(pgm *sym, const pgm *const orig);
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 pgm_conv(pgm *conv, const pgm *const orig, const matrix *const kernel, int32_t norme);
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
#include "TraitImage.h"
int main (void){
pgm p;
pgm neg;
pgm hori;
pgm vert;
pgm cent;
pgm photomat;
pgm crop;
pgm conv;
matrix kernel;
int32_t tab[] = {1,1,1,1,1,1,1,1,1};
matrix_init_from_array(&kernel, 3, 3, tab, 9);
pgm_read_from_file(&p, "Images/mandrill.pgm");
pgm_negative(&neg, &p);
pgm_symmetry_hori(&hori, &p);
pgm_symmetry_vert(&vert, &p);
pgm_symmetry_cent(&cent, &p);
pgm_photomaton(&photomat, &p);
pgm_crop(&crop, &p, 500, 300, 500, 200);
pgm_conv(&conv, &p, &kernel, 9);
pgm_write_to_file(&neg, "Images/neg.pgm");
pgm_write_to_file(&hori, "Images/hori.pgm");
pgm_write_to_file(&vert, "Images/vert.pgm");
pgm_write_to_file(&cent, "Images/cent.pgm");
pgm_write_to_file(&photomat, "Images/photomat.pgm");
pgm_write_to_file(&crop, "Images/crop.pgm");
pgm_write_to_file(&conv, "Images/conv.pgm");
matrix_destroy(&p.pixels);
matrix_destroy(&kernel);
}
\ No newline at end of file
gfx.c 0 → 100644
/// @file gfx.c
/// @author Florent Gluck
/// @date November 6, 2016
/// Helper routines to render pixels in fullscreen graphic mode.
/// Uses the SDL2 library.
#include "gfx.h"
/// Create a fullscreen graphic window.
/// @param title Title of the window.
/// @param width Width of the window in pixels.
/// @param height Height of the window in pixels.
/// @return a pointer to the graphic context or NULL if it failed.
struct gfx_context_t* gfx_create(char *title, uint width, uint height) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) goto error;
SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, width, height);
uint32_t *pixels = malloc(width*height*sizeof(uint32_t));
struct gfx_context_t *ctxt = malloc(sizeof(struct gfx_context_t));
if (!window || !renderer || !texture || !pixels || !ctxt) goto error;
ctxt->renderer = renderer;
ctxt->texture = texture;
ctxt->window = window;
ctxt->width = width;
ctxt->height = height;
ctxt->pixels = pixels;
SDL_ShowCursor(SDL_DISABLE);
gfx_clear(ctxt, COLOR_BLACK);
return ctxt;
error:
return NULL;
}
/// Draw a pixel in the specified graphic context.
/// @param ctxt Graphic context where the pixel is to be drawn.
/// @param x X coordinate of the pixel.
/// @param y Y coordinate of the pixel.
/// @param color Color of the pixel.
void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color) {
if (x < ctxt->width && y < ctxt->height)
ctxt->pixels[ctxt->width*y+x] = color;
}
/// Clear the specified graphic context.
/// @param ctxt Graphic context to clear.
/// @param color Color to use.
void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) {
int n = ctxt->width*ctxt->height;
while (n)
ctxt->pixels[--n] = color;
}
/// Display the graphic context.
/// @param ctxt Graphic context to clear.
void gfx_present(struct gfx_context_t *ctxt) {
SDL_UpdateTexture(ctxt->texture, NULL, ctxt->pixels, ctxt->width*sizeof(uint32_t));
SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL);
SDL_RenderPresent(ctxt->renderer);
}
/// Destroy a graphic window.
/// @param ctxt Graphic context of the window to close.
void gfx_destroy(struct gfx_context_t *ctxt) {
SDL_ShowCursor(SDL_ENABLE);
SDL_DestroyTexture(ctxt->texture);
SDL_DestroyRenderer(ctxt->renderer);
SDL_DestroyWindow(ctxt->window);
free(ctxt->pixels);
ctxt->texture = NULL;
ctxt->renderer = NULL;
ctxt->window = NULL;
ctxt->pixels = NULL;
SDL_Quit();
free(ctxt);
}
/// If a key was pressed, returns its key code (non blocking call).
/// List of key codes: https://wiki.libsdl.org/SDL_Keycode
/// @return the key that was pressed or 0 if none was pressed.
SDL_Keycode gfx_keypressed() {
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN)
return event.key.keysym.sym;
}
return 0;
}
gfx.h 0 → 100644
#ifndef _GFX_H_
#define _GFX_H_
#include <stdint.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#define MAKE_COLOR(r,g,b) ((uint32_t)b|((uint32_t)g<<8)|((uint32_t)r<<16))
#define COLOR_GET_B(color) (color & 0xff)
#define COLOR_GET_G(color) ((color >> 8) & 0xff)
#define COLOR_GET_R(color) ((color >> 16) & 0xff)
#define COLOR_BLACK 0x00000000
#define COLOR_RED 0x00FF0000
#define COLOR_GREEN 0x0000FF00
#define COLOR_BLUE 0x000000FF
#define COLOR_WHITE 0x00FFFFFF
#define COLOR_YELLOW 0x00FFFF00
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned char uchar;
struct gfx_context_t {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
uint32_t *pixels;
int width;
int height;
};
extern void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color);
extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color);
extern struct gfx_context_t* gfx_create(char *text, uint width, uint height);
extern void gfx_destroy(struct gfx_context_t *ctxt);
extern void gfx_present(struct gfx_context_t *ctxt);
extern SDL_Keycode gfx_keypressed();
#endif
CC=gcc
CFLAGS=-Wall -Wextra -std=c11 -g -fsanitize=leak -fsanitize=undefined -fsanitize=address
LIBS=-lSDL2
all: gfx_example
gfx_example: gfx_example.c gfx.c
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)
clean:
rm -f gfx_example
/// @file gfx.c
/// @author Florent Gluck
/// @date November 6, 2016
/// Helper routines to render pixels in fullscreen graphic mode.
/// Uses the SDL2 library.
#include "gfx.h"
/// Create a fullscreen graphic window.
/// @param title Title of the window.
/// @param width Width of the window in pixels.
/// @param height Height of the window in pixels.
/// @return a pointer to the graphic context or NULL if it failed.
struct gfx_context_t* gfx_create(char *title, uint width, uint height) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) goto error;
SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, width, height);
uint32_t *pixels = malloc(width*height*sizeof(uint32_t));
struct gfx_context_t *ctxt = malloc(sizeof(struct gfx_context_t));
if (!window || !renderer || !texture || !pixels || !ctxt) goto error;
ctxt->renderer = renderer;
ctxt->texture = texture;
ctxt->window = window;
ctxt->width = width;
ctxt->height = height;
ctxt->pixels = pixels;
SDL_ShowCursor(SDL_DISABLE);
gfx_clear(ctxt, COLOR_BLACK);
return ctxt;
error:
return NULL;
}
/// Draw a pixel in the specified graphic context.
/// @param ctxt Graphic context where the pixel is to be drawn.
/// @param x X coordinate of the pixel.
/// @param y Y coordinate of the pixel.
/// @param color Color of the pixel.
void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color) {
if (x < ctxt->width && y < ctxt->height)
ctxt->pixels[ctxt->width*y+x] = color;
}
/// Clear the specified graphic context.
/// @param ctxt Graphic context to clear.
/// @param color Color to use.
void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) {
int n = ctxt->width*ctxt->height;
while (n)
ctxt->pixels[--n] = color;
}
/// Display the graphic context.
/// @param ctxt Graphic context to clear.
void gfx_present(struct gfx_context_t *ctxt) {
SDL_UpdateTexture(ctxt->texture, NULL, ctxt->pixels, ctxt->width*sizeof(uint32_t));
SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL);
SDL_RenderPresent(ctxt->renderer);
}
/// Destroy a graphic window.
/// @param ctxt Graphic context of the window to close.
void gfx_destroy(struct gfx_context_t *ctxt) {
SDL_ShowCursor(SDL_ENABLE);
SDL_DestroyTexture(ctxt->texture);
SDL_DestroyRenderer(ctxt->renderer);
SDL_DestroyWindow(ctxt->window);
free(ctxt->pixels);
ctxt->texture = NULL;
ctxt->renderer = NULL;
ctxt->window = NULL;
ctxt->pixels = NULL;
SDL_Quit();
free(ctxt);
}
/// If a key was pressed, returns its key code (non blocking call).
/// List of key codes: https://wiki.libsdl.org/SDL_Keycode
/// @return the key that was pressed or 0 if none was pressed.
SDL_Keycode gfx_keypressed() {
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN)
return event.key.keysym.sym;
}
return 0;
}
#ifndef _GFX_H_
#define _GFX_H_
#include <stdint.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#define MAKE_COLOR(r,g,b) ((uint32_t)b|((uint32_t)g<<8)|((uint32_t)r<<16))
#define COLOR_GET_B(color) (color & 0xff)
#define COLOR_GET_G(color) ((color >> 8) & 0xff)
#define COLOR_GET_R(color) ((color >> 16) & 0xff)
#define COLOR_BLACK 0x00000000
#define COLOR_RED 0x00FF0000
#define COLOR_GREEN 0x0000FF00
#define COLOR_BLUE 0x000000FF
#define COLOR_WHITE 0x00FFFFFF
#define COLOR_YELLOW 0x00FFFF00
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned char uchar;
struct gfx_context_t {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
uint32_t *pixels;
int width;
int height;
};
extern void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color);
extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color);
extern struct gfx_context_t* gfx_create(char *text, uint width, uint height);
extern void gfx_destroy(struct gfx_context_t *ctxt);
extern void gfx_present(struct gfx_context_t *ctxt);
extern SDL_Keycode gfx_keypressed();
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment