Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:24:46+00:00 2026-05-24T06:24:46+00:00

I’m using this code to get the pixels from a bmp file. I have

  • 0

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->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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:24:47+00:00Added an answer on May 24, 2026 at 6:24 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In order to apply a triggered animation to all ToolTip s in my app,
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.