Jump to content
  • Advertisement
Sign in to follow this  

strange memory/file problem

This topic is 4131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in all my years of programming, this is by far the most mysterious error i've come across. i have a program that generates a bitmap and saves it to a file. my test image has a greater red value on the right and a greater green value on the bottom. it should look like this with a size of 196,662 bytes: but for some reason it turns out like this with a size of 197,174 bytes: after careful study it appears that any time a pixel component is supposed to be written as 0A, it writes it as 0D 0A instead. this doesn't make any sense, because my fwrite function only writes 3 chars at a time. so it can't really be a fwrite problem. i'm so unbelievably confused! EDIT: if it makes any difference, the pixel components are converted from double values from 0.0 to 1.0 to unsigned char values from 0 to 255 like this: image2.buf[x].r = (unsigned char)(int)( image.buf[x].rgb.r * 255.0 ); image2.buf[x].g = (unsigned char)(int)( image.buf[x].rgb.g * 255.0 ); image2.buf[x].b = (unsigned char)(int)( image.buf[x].rgb.b * 255.0 );

Share this post


Link to post
Share on other sites
Advertisement
Are you padding everything properly?
My 24bpp BMP loader loads
RGBRGBRGB___
RGBRGBRGB___
..... from the file
and the file lines are padded out to some multiple of 4 bytes.
and if it is 24bpp, then there is also one extra byte of padding on that line.


//BMP header stuff...
//.....
//.....
//Load the Data
unsigned int stride = ((sizeX * channels)%4)==0?(sizeX * channels):((sizeX * channels)+1);
unsigned int bytesUsed = sizeX * channels;
unsigned int padding = stride-bytesUsed ;

for(size_t y = 0; y < sizeY; y++)
{
byte *LinePtr = &(data[stride*y]);

if ( !fread ( LinePtr, bytesUsed, ifile ) )
{ fclose(ifile); return false; }

if ( fseek (ifile, padding, SEEK_CUR) )
{ fclose(ifile); return false; }

}


Share this post


Link to post
Share on other sites
0x0A is the newline character ("\n"), which is translated to a CRLF (0x0D 0x0A, "\r\n") when writing text files on Windows. Open the output file in binary mode instead (e.g. with fopen(fn, "wb")) and it should work correctly.

Share this post


Link to post
Share on other sites
Open the file in binary mode:

Using C++ streams:
std::ofstream os("file.ext", std::ios::out | std::ios::binary);

or

Using C files:
FILE* os = fopen("file.ext", "wb");

Share this post


Link to post
Share on other sites
ahh thanks lots everyone. i never would have thought of that on my own. i've always wondered what binary mode was for.

Share this post


Link to post
Share on other sites
It's for, well, writing binary data. :)

Another way you could have helped yourself would be to look up what the characters 0D and 0A are. "Carriage return" and "line feed"... hmm? :) Sounds like it's treating ends of lines specially. "But wait, this is a bitmap; it doesn't have lines, it has rows." And off to Google we go, yes? :)

Share this post


Link to post
Share on other sites
Visual C++ (or 2003 .Net anyway, not sure about Express) padds out data structures to cache-aligned boundaries, so your pixel struct/class might be 4 bytes instead of 3. I've had similar troubles with .bmp files before, you'll have to load it as an array of chars and then load it into your data structure manually.

Share this post


Link to post
Share on other sites
I assume that's drawing sin(x*y) > 0? It's fun to load into a paint program and zoom out by integer factors (i.e. doing unfiltered pixel sampling), since the number of repeated patterns along each side increases proportionally with the zoom factor. That surprised me when I first encountered it [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!