Jose Ricardo Citerio
Jose Ricardo Citerio

Reputation: 79

How can I read a raw image in C without special libreries?

I am making an University project, I gotta read a raw image in C, and save the values into a matriz and then apply a gaussian blur, and I think I am reading them wrong cause, I got this on Win console for a 5 x 5 pixel raw image:

228 228 228 228 228
228 228 228 228 228
228 228 228 228 228
228 228 228 228 228
228 228 228 228 228

This is when i printf the dinamic matriz, and my partner in linux is getting just zero's, Here my code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>




int main()
{
    FILE *info_image, *image_raw;
    info_image = fopen("picture.inf","r");
    int **matriz_image, test;

    int i, j, rows, colums;


    //i read dimension image
    fscanf(info_image,"%i %i",&colums, &rows);


    //i create dinamic rows
    matriz_image = (int **) malloc (rows*sizeof(int*));

    //i create dinamic colums
    for(i=0;i<rows;i++)
    {

         matriz_image[i] = (int*) malloc (colums*sizeof(int)); 



    }

    //i open image raw
    image_raw = fopen("picture.raw","r");

    //i copy values to matriz_image
    for(i=0;i<rows;i++)
    {
        for(j=0;j<colums;j++)
        {

            //fscanf(image_raw,"%i",*(*(matriz_image+i)+j)); 
            fscanf(image_raw,"%i",&test);
            *(*(matriz_image+i)+j)=test;
            //printf("%i \n", test); 

        }

    }


    //i print matriz
    for(i=0;i<rows;i++)
    {
        for(j=0;j<colums;j++)
        {

            printf("%i ",*(*(matriz_image+i)+j)); 
            //printf("%i ",matriz_image[i][j]); 


        }
        printf("\n");

    }






    getch();

}

Upvotes: 4

Views: 5446

Answers (1)

timrau
timrau

Reputation: 23058

As long as you cannot open it with a text editor, it is not reasonable to read the file with fscanf(). Instead you should try fread(). Also you should open file with mode "rb" for non-plain-text files.

image_raw = fopen("picture.raw", "rb");
for (i = 0; i < rows; ++i) {
    fread(matriz_image[i], sizeof(int), columns, image_raw);
}

Upvotes: 2

Related Questions