我正在使用以下代码从bmp文件中获取像素。已经在之前的代码中读取了头和调色板,所以我的FILE *指向像素数组的开头。
它可以正确读取第一行,并返回值为1000,这是正确的。但当尝试读取第二行像素时,它会返回0。
以下是该函数接收FILE *并读取像素行并尝试将其保存到bmp_type中。fila_alineada是对齐的行大小,必须这样做是因为填充。
以下是该函数接收FILE *并读取像素行并尝试将其保存到bmp_type中。fila_alineada是对齐的行大小,必须这样做是因为填充。
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->paleta.colores[ *ptmp++ ];
}
}
return true;
我已经尝试了不同的bmp文件!问题出在这里,还是我应该考虑重新审查整个代码? 希望有人能帮助我,提前感谢。