main.c
//Esta funcion lee imagen RGB de archivo en formato BMP
Esta funcion lee imagen RGB de archivo en formato BMP
Esta funcion lee imagen RGB de archivo en formato BMP
void readRGBImageFromBMPFile(char *filename)
    {
        FILE *fd;
        int width, height;
        int i, j;
        int n;
        fd = fopen(filename, "rb");
        if (fd == NULL)
        {
            printf("Error: fopen failed\n");
            return;
        }
        unsigned char header[54];
        n=fread(header, sizeof(unsigned char), 54, fd);
        if (n==0) {printf("No se pudieron leer datos\n"); exit(0);}
        width = *(int*)&header[18];
        height = *(int*)&header[22];
        int padding = 0;
        while ((width * 3 + padding) % 4 != 0)
        {
            padding++;
        }
        int widthnew = width * 3 + padding;
        unsigned char* data = (unsigned char *)malloc(widthnew * sizeof (unsigned int));
        for (i = 0; i<height; i++)
        {
            n=fread(data, sizeof(unsigned char), widthnew, fd);
            if (n==0) {printf("No se pudieron leer datos\n"); exit(0);}
            for (j = 0; j < width * 3; j += 3)
            {
                int index = (i * width * 3) + (j);
                pInputImage->prgb[index + 0] = data[j + 2];
                pInputImage->prgb[index + 1] = data[j + 1];
                pInputImage->prgb[index + 2] = data[j + 0];
            }
        }
        free(data);
        fclose(fd);
    }
void readRGBImageFromBMPFile ( char * filename ) { FILE * fd ; int width , height ; int i , j ; int n ; fd = fopen ( filename , " " ) ; if ( fd == NULL ) { printf ( " \n " ) ; return ; } unsigned char header [ 54 ] ; n = fread ( header , sizeof ( unsigned char ) , 54 , fd ) ; if ( n == 0 ) { printf ( " \n " ) ; exit ( 0 ) ; } width = * ( int * ) & header [ 18 ] ; height = * ( int * ) & header [ 22 ] ; int padding = 0 ; while ( ( width * 3 + padding ) % 4 != 0 ) { padding ++ ; } int widthnew = width * 3 + padding ; unsigned char * data = ( unsigned char * ) malloc ( widthnew * sizeof ( unsigned int ) ) ; for ( i = 0 ; i < height ; i ++ ) { n = fread ( data , sizeof ( unsigned char ) , widthnew , fd ) ; if ( n == 0 ) { printf ( " \n " ) ; exit ( 0 ) ; } for ( j = 0 ; j < width * 3 ; j += 3 ) { int index = ( i * width * 3 ) + ( j ) ; pInputImage -> prgb [ index + 0 ] = data [ j + 2 ] ; pInputImage -> prgb [ index + 1 ] = data [ j + 1 ] ; pInputImage -> prgb [ index + 2 ] = data [ j + 0 ] ; } } free ( data ) ; fclose ( fd ) ; }
Esta funcion lee imagen RGB de archivo en formato BMP
Esta funcion lee imagen RGB de archivo en formato BMP
// Read header // Capture dimensions // Calculate padding // Compute new width, which includes padding // Allocate temporary memory to read widthnew size of data // Read row by row of data and remove padded data. // Read widthnew length of data // Retain width length of data, and swizzle RB component. // BMP stores in BGR format, my usecase needs RGB format