Skip to content
Snippets Groups Projects
Commit 0c482e66 authored by jorge.leitemac's avatar jorge.leitemac :juggling_tone5:
Browse files

ajout gfx mais marche pas

parent ef2cc7a4
No related branches found
No related tags found
No related merge requests found
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
#include "gfx.h"
#include "pgm.h"
static void renderPgm(struct gfx_context_t *context, const pgm *const orig) {
gfx_clear(context, COLOR_BLACK);
uint32_t intensity;
uint32_t color;
for (int i = 0; i < orig->pixels.m; i++)
{
for (int j = 0; j < orig->pixels.n; j++)
{
intensity = orig->max;
color = MAKE_COLOR(intensity,intensity,intensity);
gfx_putpixel(context, i, j, color);
}
}
}
int main(){
int boxBlur[] = {1,1,1,1,1,1,1,1,1};
int boxBlur[] = { 1, 2, 1, 2, 4, 2, 1, 2, 1};
pgm p;
pgm_read_from_file(&p, "./img/mandrill.pgm");
pgm_write_to_file(&p, "./img/mandrill2.pgm");
int longueur = p.pixels.m;
int hauteur = p.pixels.n;
struct gfx_context_t *ctxt = gfx_create("image", longueur, hauteur);
if (!ctxt) {
fprintf(stderr, "Graphics initialization failed!\n");
return EXIT_FAILURE;
}
while (gfx_keypressed() != SDLK_ESCAPE) {
renderPgm(ctxt, &p);
gfx_present(ctxt);
}
gfx_destroy(ctxt);
pgm ph;
pgm_photomaton(&ph,&p);
pgm_write_to_file(&ph, "./img/photomaton_mandrill.pgm");
......@@ -32,21 +64,24 @@ int main(){
pgm_symmetry_cent(&symC, &p);
pgm_write_to_file(&symC, "./img/symC_mandrill.pgm");
matrix kernel;
matrix_init_from_array(&kernel, 3, 3, boxBlur);
matrix_print(kernel);
pgm conv;
pgm_conv(&conv, &p, &kernel, 16);
pgm_write_to_file(&conv, "./img/conv_mandrill.pgm");
//SYM CROP
pgm crop;
pgm_crop(&crop, &p, 100, 200, 100, 200);
pgm_write_to_file(&crop, "./img/crop_mandrill.pgm");
matrix kernel;
matrix_init_from_array(&kernel, 3, 3, boxBlur);
matrix_print(kernel);
pgm conv;
pgm_conv(&conv, &p, &kernel, 9);
pgm_write_to_file(&crop, "./img/conv_mandrill.pgm");
matrix_destroy(&(kernel));
matrix_destroy(&(conv.pixels));
matrix_destroy(&(conv.pixels));
matrix_destroy(&(p.pixels));
matrix_destroy(&(symH.pixels));
matrix_destroy(&(symV.pixels));
......
......@@ -217,44 +217,50 @@ pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig){
return success;
}
//Kernel = matrice de convolution
pgm_error pgm_conv(pgm *conv, const pgm *const orig,const matrix *const kernel, double norm){
conv->max = orig->max;
//conv = orig * kernel
int cpt = 0;
int32_t valeurs[orig->pixels.m * orig->pixels.n];
//Index pour data pixels par rapport au kernel
int nC = 0;
int mC = 0;
int moy = 0;
//Evite division par 0
norm = (norm == 0) ? 1 : norm;
//Itère sur pixels n et m
for (int n = 0; n < orig->pixels.m; n++)
{
for (int m = 0; m < orig->pixels.n; m++)
{
//Pour chaque element de orig pixels :
//POURQUOI /2 ????
mC = m-(kernel->m-1)/2;
nC = n-(kernel->n-1)/2;
//On calcule les index
mC = m-kernel->m-1;
nC = n-kernel->n-1;
//On parcourt les éléments de la matrice kernel (on superpose kernel a orig)
for (int i = 0; i < kernel->m; i++)
{
for (int j = 0; j < kernel->n; j++)
{
if(mC <= orig->pixels.m && nC <= orig->pixels.n && nC > 0 && mC > 0){
moy += orig->pixels.data[nC][mC] * kernel->data[i][j];
//Si on dépasse pas les bords de l'image et que les index ne sont pas négatifs
if(mC < orig->pixels.m && nC < orig->pixels.n && nC >= 0 && mC >= 0){
moy += orig->pixels.data[nC][mC] * kernel->data[i][j]; //On ajoute à la moyenne le produit des 2 éléments des matrices superposées
}
nC++;
}
nC = n-(kernel->n-1)/2;
nC = n-kernel->n-1;
mC++;
}
moy = moy / norm;
if(moy < 0){
moy = 0;
}
......@@ -262,8 +268,11 @@ pgm_error pgm_conv(pgm *conv, const pgm *const orig,const matrix *const kernel,
moy = orig->max;
}
//Ajout au tableau
valeurs[cpt] = moy;
cpt++;
//On doit reinitialiser la moyenne pour le prochain pixel
moy = 0;
}
......
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