Thamous
Thamous

Reputation: 1

fread causing array to become unreadable

I'm trying to program an edge detection method for bmp images but I cannot access the array that holds the RGB values for the pixels after using fread to read the data in.

The image writes out fine from the array that fread read into, unaltered naturally, so I really am not sure what is going on here. I suspect it has something to do with the way I've allocated the array but I'm not sure what would need to be changed. Any help is very much appreciated.

struct fileHeader{
    unsigned short type;
    unsigned int size;
    unsigned short reservedOne, reservedTwo;
    unsigned int offset;
}Header;

struct infofileHeader{
    unsigned int size;
    int width, height;
    unsigned short planes;
    unsigned short bits;
    unsigned int compression;
    unsigned int imageSize;
    int xResolution, yResolution;
    unsigned int colors;
    unsigned int importantColors;
} infoHeader;

struct pixel{
    unsigned char Red, Green, Blue;
};

int main(int argc, char *argv[])
{
    int i;
    FILE* image = fopen(argv[1], "rb");
    FILE *testFile = fopen("out.bmp", "wb");

    fread(&Header.type, 2, 1, image);
    fread(&Header.size, 4, 1, image);
    fread(&Header.reservedOne, 2, 1, image);
    fread(&Header.reservedTwo, 2, 1, image);
    fread(&Header.offset, 4, 1, image);

    fread(&infoHeader.size, 4, 1, image);
    fread(&infoHeader.width, 4, 1, image);
    fread(&infoHeader.height, 4, 1, image);
    fread(&infoHeader.planes, 2, 1, image);
    fread(&infoHeader.bits, 2, 1, image);
    fread(&infoHeader.compression, 4, 1, image);
    fread(&infoHeader.imageSize, 4, 1, image);
    fread(&infoHeader.xResolution, 4, 1, image);
    fread(&infoHeader.yResolution, 4, 1, image);
    fread(&infoHeader.colors, 4, 1, image);
    fread(&infoHeader.importantColors, 4, 1, image);

    printf("%X\n", Header.type);
    printf("%d\n", Header.size);
    printf("%d\n", Header.offset);

    printf("Horizontal Resolution: %d\n", infoHeader.width);
    printf("Vertical Resolution: %d\n", infoHeader.height);
    printf("Total Resolution:Test %d\n", infoHeader.width * infoHeader.height);


    struct pixel **pixelArray = malloc(infoHeader.height * sizeof(pixelArray));
        if(pixelArray == NULL)printf("Could not allocate memory");

    for(i=0; i<infoHeader.height; i++){
        pixelArray[i] = (struct pixel*) malloc(infoHeader.width * sizeof(struct pixel));
            if(pixelArray[i] == NULL)printf("Could not allocate memory");
    }


    fread(pixelArray, sizeof(struct pixel), infoHeader.width*infoHeader.height, image);

    fclose(image);

    fwrite(&Header.type, 2, 1, testFile);
    fwrite(&Header.size, 4, 1, testFile);
    fwrite(&Header.reservedOne, 2, 1, testFile);
    fwrite(&Header.reservedTwo, 2, 1, testFile);
    fwrite(&Header.offset, 4, 1, testFile);

    fwrite(&infoHeader.size, 4, 1, testFile);
    fwrite(&infoHeader.width, 4, 1, testFile);
    fwrite(&infoHeader.height, 4, 1, testFile);
    fwrite(&infoHeader.planes, 2, 1, testFile);
    fwrite(&infoHeader.bits, 2, 1, testFile);
    fwrite(&infoHeader.compression, 4, 1, testFile);
    fwrite(&infoHeader.imageSize, 4, 1, testFile);
    fwrite(&infoHeader.xResolution, 4, 1, testFile);
    fwrite(&infoHeader.yResolution, 4, 1, testFile);
    fwrite(&infoHeader.colors, 4, 1, testFile);
    fwrite(&infoHeader.importantColors, 4, 1, testFile);

    fwrite(pixelArray, sizeof(struct pixel), 50000, testFile);

    /*for(i=0; i<infoHeader.height; i++){
        free(pixelArray[i]);
    }
    free(pixelArray);*/

    fclose(testFile);

    return 0;
}

Upvotes: 0

Views: 96

Answers (1)

Diego
Diego

Reputation: 1819

...
fread(pixelArray, sizeof(struct pixel), infoHeader.width*infoHeader.height, image);
...

You cannot read the pixels in that way since you did different allocations for each row.

Instead, you have to individually call fread for each row.

Upvotes: 3

Related Questions