Skip to content
Snippets Groups Projects
traitementPGM.c 2.03 KiB
Newer Older
Og's avatar
Og committed
#include "traitementPGM.h"

poulpe's avatar
poulpe committed
/**
 * @brief The read of PGM file in P5 format 
 * 
 * @param p The PGM struct where data is set and stored
 * @param filename The filename to read all data
 * @return int32_t Return -1 if error
 */
int32_t pgm_read_from_file(pgm *p, char *filename)
{
    if (filename == NULL || p == NULL)
    char header[BUFSIZ];
    int32_t c = 0, r = 0, intensity = 0;
    FILE *f = fopen(filename, "rb");

    fscanf(f, "%s", header);
    fscanf(f, "%d %d", &r, &c); // r = Y number of line
    fscanf(f, "%d", &intensity);

    matrix_init(&p->pixels, c, r);
    p->max = intensity;

    for (uint32_t i = 0; i < p->pixels.col; i += 1)
        for (uint32_t j = 0; j < p->pixels.row; j += 1)
            fread(&p->pixels.data[i][j], sizeof(uint16_t), 1, f);
Og's avatar
Og committed
    }

    fclose(f);
poulpe's avatar
poulpe committed
/**
 * @brief Write output file PGM version P2 beceause P5 not working but is best
 * 
 * @param p The PGM pointer of struct to use
 * @param filename The filename of file to create and write inside
 */
void pgm_write_to_file(pgm *p, char *filename)
{
    FILE *f = fopen(filename, "w");
Og's avatar
Og committed

    fprintf(f, "P2\n");
    fprintf(f, "%d %d\n", p->pixels.row, p->pixels.col);
    fprintf(f, "%d\n", p->max);
Og's avatar
Og committed

    for (uint32_t i = 0; i < p->pixels.col; i += 1)
    {
        for (uint32_t j = 0; j < p->pixels.row; j += 1)
        {
            fprintf(f, "%d ", p->pixels.data[i][j]);
Og's avatar
Og committed
        }
        fprintf(f, "\n");
Og's avatar
Og committed
    }

    fclose(f);
poulpe's avatar
poulpe committed
/**
 * @brief Display the image with SDL 
 * 
 * @param img The image PGM to display
 * @param filname The name of the window or the filename displayed 
 */
void PrintImagePGM(pgm img, char * filname)
{
    printf("Image sélectionnée '%s' de taille %dx%d lumMax:%d\n", filname, img.pixels.col, img.pixels.row, img.max);
    struct gfx_context_t *ImagePGM = initWindow(img.pixels.row, img.pixels.col, filname);
    dessineTab2D(ImagePGM, img.pixels.data, img.pixels.col, img.pixels.row);
    gfx_present(ImagePGM);
    waitSpacePress();
    gfx_destroy(ImagePGM);
Og's avatar
Og committed
}