开发者

Fread returning 0, reading pixels from BMP file

I'm using this code to get the pixels from a bmp file. I have already read the headers and the palette in previous lines, so my FILE * is pointing to the beggining of the pixels array. It reads the first row OK, returns 1000 which is what it should, but when it tries to read the second row of pixels it returns 0.

This is the function that receivs the FILE * , reads the pixels rows and tries to save them into a bmp_type. fila_alineada is the aligned row size, had to do this because of padding.

bool leer_pixels_8bpp(   FILE *fbmp, bmp_t *imagen,
                            const uint32_t fila_alineada,
                            const bool btopdown ){
int32_t i;
long x, y;
int32_t height, width, contador;
uint8_t *ptmp;

uint8_t bufferfila[fila_alineada];

height = imagen->infoheader.height;
width  = imagen->infoheader.width;
contador  = height;

i = btopdown ? 1 : -1;
y = btopdown ? 0 : ( height - 1 );

for ( ; contador--; y += i ) /* row loop */
{
    /* reading row */
    if ( fread( bufferfila, sizeof( uint8_t ), fila_alineada, fbmp ) != fila_alineada )
    {    /* HERE is the PROBLEM, it reads ok once, but in the second loop it returns 0 */
        fprintf( stderr, "Error reading pixels row.\n" );
        return false;
    }

    ptmp = bufferfila;
        /* saving pixels into bmp_t */
    for ( x = 0L; x < width; x++ )
    {
        imagen->pixels[y][x] = imagen-开发者_Python百科>paleta.colores[ *ptmp++ ];
    }

}

return true;

}

I've tried with differents bmp's! The problem is here or should i consider reviewing the entire code? Hope someone can help me, thanks in advance.


It seems from your code that you are not reading what the width of the image is ... rather you are reading an amount of fila_alineada. You mentioned that fila_alineada was the aligned row size because of "padding", but BMP files should only have enough padding at the end to extend each row out to a multiple of 4-bytes ... that should be a value though that is easily calculated from the BITMAPINFOHEADER by dividing the actual pixel-array data-size with the number of rows in the image. The pixel-array data-size is stored at offset 0x22 in the header. The height, as I'm sure you've correctly deduced, is at offset 0x16. So the argument fila_alineada is pretty much redundant (i.e., you can remove it), and you may be calculating this value incorrectly. I would simply use the information in the header to calculate the size of the buffer necessary to save the information in a given row of the image.

Secondly, if you simply have tried to copy the BITMAPINFOHEADER information from the file into a buffer that is some representative header structure, keep in mind that the compiler may have padded the structure for byte-alignment purposes ... therefore for safety reasons you should not simply read the entire header from a file and then attempt to use memcpy() to write the buffer into a BITMAPINFOHEADER structure. You should read the header values one-by-one from the file, and store those values individually into any structure that represents the BITMAPINFOHEADER. Otherwise, if you do the former, by simply trying to read the first N bytes of the file, and copy that into a structure, you could end up, because of byte-alignment issues, copying in values that are incorrect, and therefore all the values you try to read back from that structure will not represent the values associated with your bitmap file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜