# SDL\_LoadBMP\_RW returning "error reading from datastream"

**URL:** <https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517>\
**Category:** SDL Development\
**Created:** [October 14, 2011, 5:28am UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517 "2011-10-14T05:28:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuri\_David\_Santos](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/yuri_david_santos/32/2395_2.png) [@Yuri\_David\_Santos](https://discourse.libsdl.org/u/Yuri_David_Santos)\
**Post date:** [October 14, 2011, 5:28am UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/1 "2011-10-14T05:28:09Z")

</div>

Hi!

I’m new to this mailing list. I have googled arround but I’ve found no  
answers to this. I followed the first tutorial in this link:

[http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)

In Linux it works great (all the 4 parts), but in Windows, the same code  
results in the error message “error reading from datastream”. The code is  
exactly as in the tutorial above. Anyone have any ideas?

Thanks in advance!–  
Yuri

---

<div class="post-metadata">

**Author:** ![Yuri\_David\_Santos](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/yuri_david_santos/32/2395_2.png) [@Yuri\_David\_Santos](https://discourse.libsdl.org/u/Yuri_David_Santos)\
**Post date:** [October 14, 2011, 6:23pm UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/2 "2011-10-14T18:23:53Z")

</div>

I found that this problem occurs just for some BMP files, not all. Some 8  
bits BMP.  
Another problem I’m having is to load PNG files. With IMG\_Load it works, but  
using the IMG\_Load\_RW to load from memory or from a memory pointer, the  
SDL\_GetError() says “Error reading the PNG file”. I don’t know what this can  
be. The libpng.dll is in the same folder that the SDL\_image.dll, and is  
loading successfully directly from the file (with IMG\_Load).

Anybody get this problem too?

Thanks in advance!

2011/10/14 Yuri David Santos \> Hi!

> I’m new to this mailing list. I have googled arround but I’ve found no  
> answers to this. I followed the first tutorial in this link:
> 
> [http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)
> 
> In Linux it works great (all the 4 parts), but in Windows, the same code  
> results in the error message “error reading from datastream”. The code is  
> exactly as in the tutorial above. Anyone have any ideas?
> 
> Thanks in advance!
> 
> –  
> Yuri

–  
Yuri David Santos  
Aleva - Solu??es em Software  
www.aleva.com.br

---

<div class="post-metadata">

**Author:** ![Patrick\_Baggett](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@Patrick\_Baggett](https://discourse.libsdl.org/u/Patrick_Baggett)\
**Post date:** [October 14, 2011, 6:36pm UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/3 "2011-10-14T18:36:02Z")

</div>

Yuri,

It might be more productive if you can create a minimal test case that fails  
and links to PNG/BMP images that fail. For example a program that does  
something like:

#define FILENAME “myfile.png”  
int main() {

```
if(!TryIMG_Load(FILENAME)) {
    printf("IMG_Load() for %s failed!\n", FILENAME);
    return 1;
}

if(!TryIMG_Load_RW(FILENAME)) {
    printf("IMG_Load() for %s failed!\n", FILENAME);
    return 2;
}

printf("Test case success!\n");
return 0;

```

}

The problem with saying that it fails for some images and not others, and  
then not providing test cases is that if I were to test 1,000 images right  
now, how would I know if I was testing the “right kind” to trigger this bug?  
After trying 1,000 images, I’d sooner believe the problem is with _your_ code  
or maybe with \*your \* compiler that you used to build SDL/libpng/your app,  
even if it was in fact a bug in someone else’s code. Help us help you, give  
us the tools and data we need to figure out what the problem might be.

Patrick BaggettOn Fri, Oct 14, 2011 at 1:23 PM, Yuri David Santos wrote:

> I found that this problem occurs just for some BMP files, not all. Some 8  
> bits BMP.  
> Another problem I’m having is to load PNG files. With IMG\_Load it works,  
> but using the IMG\_Load\_RW to load from memory or from a memory pointer, the  
> SDL\_GetError() says “Error reading the PNG file”. I don’t know what this can  
> be. The libpng.dll is in the same folder that the SDL\_image.dll, and is  
> loading successfully directly from the file (with IMG\_Load).
> 
> Anybody get this problem too?
> 
> Thanks in advance!
> 
> 2011/10/14 Yuri David Santos
> 
> > Hi!
> > 
> > I’m new to this mailing list. I have googled arround but I’ve found no  
> > answers to this. I followed the first tutorial in this link:
> > 
> > [http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)
> > 
> > In Linux it works great (all the 4 parts), but in Windows, the same code  
> > results in the error message “error reading from datastream”. The code is  
> > exactly as in the tutorial above. Anyone have any ideas?
> > 
> > Thanks in advance!
> > 
> > –  
> > Yuri
> 
> –  
> Yuri David Santos  
> Aleva - Solu??es em Software  
> www.aleva.com.br
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Yuri\_David\_Santos](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/yuri_david_santos/32/2395_2.png) [@Yuri\_David\_Santos](https://discourse.libsdl.org/u/Yuri_David_Santos)\
**Post date:** [October 14, 2011, 7:17pm UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/4 "2011-10-14T19:17:46Z")

</div>

Hi, Patrick

Sorry for the lack of information. The BMP image that I said cannot be  
loaded was the one in this link:

[http://www.kekkai.org/roger/sdl/rwops/penguin.bmp](http://www.kekkai.org/roger/sdl/rwops/penguin.bmp)

I’m using Windows 7 32-bit and compiling with the MingW with this line:

g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll

My code:

#include “SDL/SDL.h”  
#include \<stdio.h\>

int main(int argc, char \*argv){  
SDL\_Init(SDL\_INIT\_VIDEO);

atexit(SDL\_Quit);

SDL\_Surface \*screen = SDL\_SetVideoMode(640, 480, 16, SDL\_DOUBLEBUF);

FILE \*file = fopen(“penguin.bmp”, “r”);  
if(!file) {  
printf(“Couldn’t load penguin.bmp\n”);  
exit(1);  
}

SDL\_RWops \*rw = SDL\_RWFromFP(file, 0);  
SDL\_Surface \*one = SDL\_LoadBMP\_RW(rw, 0);  
printf(“%s”, SDL\_GetError());

SDL\_FreeRW(rw);  
fclose(file);

SDL\_Rect rect;  
rect.x = rect.y = 20;  
rect.w = one → w;  
rect.y = one → h;  
SDL\_BlitSurface(one, NULL, screen, &rect);

SDL\_Flip(screen);

SDL\_Delay(3000);  
}

Remembering that this worked in Linux Ubuntu 10.04 with g++ and the same  
image. Ok, this is for BMPs.  
But for PNGs, I haven’t get any image to work in Windows. Again, in this  
case, I loaded PNGs in Linux without problem, but in Windows I get the  
message: “Error reading the PNG file”. The code used was:

char pBuffer[2157];  
FILE\* file = fopen(“cursor.png”, “r”);

fseek(file, 0, SEEK\_SET);  
fread(pBuffer, sizeof(char), 2157, file);  
SDL\_RWops\* rw = SDL\_RWFromMem(pBuffer, 2157);  
SDL\_Surface\* two = IMG\_Load\_RW(rw, 1);  
printf(“%s\n”, SDL\_GetError());

And built with:

g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll SDL\_image.dll

I have tested with the PNG file attached in this message. It works in Linux  
but not in Windows. It’s strange because loading the PNG file directly with  
IMG\_Load, it works even in Windows.  
I’m trying to use SDL\_RWops to load all my resources from one file, but I  
can’t find any Windows working example.

I’m grateful for any help.  
Thnaks in advance!

2011/10/14 Patrick Baggett \<baggett.patrick at [gmail.com](http://gmail.com)\>\> Yuri,

> It might be more productive if you can create a minimal test case that  
> fails and links to PNG/BMP images that fail. For example a program that does  
> something like:
> 
> #define FILENAME “myfile.png”  
> int main() {
> 
> ```
> if(!TryIMG_Load(FILENAME)) {
> printf("IMG_Load() for %s failed!\n", FILENAME);
> return 1;
> }
> 
> if(!TryIMG_Load_RW(FILENAME)) {
> printf("IMG_Load() for %s failed!\n", FILENAME);
> return 2;
> }
> 
> printf("Test case success!\n");
> return 0;
> 
> ```
> 
> }
> 
> The problem with saying that it fails for some images and not others, and  
> then not providing test cases is that if I were to test 1,000 images right  
> now, how would I know if I was testing the “right kind” to trigger this bug?  
> After trying 1,000 images, I’d sooner believe the problem is with _your_ code  
> or maybe with \*your \* compiler that you used to build SDL/libpng/your app,  
> even if it was in fact a bug in someone else’s code. Help us help you, give  
> us the tools and data we need to figure out what the problem might be.
> 
> Patrick Baggett
> 
> On Fri, Oct 14, 2011 at 1:23 PM, Yuri David Santos wrote:
> 
> > I found that this problem occurs just for some BMP files, not all. Some 8  
> > bits BMP.  
> > Another problem I’m having is to load PNG files. With IMG\_Load it works,  
> > but using the IMG\_Load\_RW to load from memory or from a memory pointer, the  
> > SDL\_GetError() says “Error reading the PNG file”. I don’t know what this can  
> > be. The libpng.dll is in the same folder that the SDL\_image.dll, and is  
> > loading successfully directly from the file (with IMG\_Load).
> > 
> > Anybody get this problem too?
> > 
> > Thanks in advance!
> > 
> > 2011/10/14 Yuri David Santos
> > 
> > > Hi!
> > > 
> > > I’m new to this mailing list. I have googled arround but I’ve found no  
> > > answers to this. I followed the first tutorial in this link:
> > > 
> > > [http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)
> > > 
> > > In Linux it works great (all the 4 parts), but in Windows, the same code  
> > > results in the error message “error reading from datastream”. The code is  
> > > exactly as in the tutorial above. Anyone have any ideas?
> > > 
> > > Thanks in advance!
> > > 
> > > –  
> > > Yuri
> > 
> > –  
> > Yuri David Santos  
> > Aleva - Solu??es em Software  
> > www.aleva.com.br
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

–  
Yuri David Santos  
Aleva - Solu??es em Software  
www.aleva.com.br  
-------------- next part --------------  
A non-text attachment was scrubbed…  
Name: cursor.png  
Type: image/png  
Size: 2157 bytes  
Desc: not available  
URL: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20111014/90334457/attachment.png](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20111014/90334457/attachment.png)

---

<div class="post-metadata">

**Author:** ![Yuri\_David\_Santos](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/yuri_david_santos/32/2395_2.png) [@Yuri\_David\_Santos](https://discourse.libsdl.org/u/Yuri_David_Santos)\
**Post date:** [October 14, 2011, 10:29pm UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/5 "2011-10-14T22:29:34Z")

</div>

Hi! I don’t know why the error with the PNG was ocurring, but I think my  
file reading code was wrong. I will share the code that works for me:

#include “SDL/SDL.h”  
#include “SDL/SDL\_image.h”  
#include \<stdio.h\>  
#include  
#include

using namespace std;

int main(int argc, char \*argv){  
SDL\_Init(SDL\_INIT\_VIDEO);  
atexit(SDL\_Quit);  
SDL\_Surface \*screen = SDL\_SetVideoMode(800, 600, 16, SDL\_DOUBLEBUF);

SDL\_Rect r = {0, 0, 800, 600};  
SDL\_FillRect(screen, &r, SDL\_MapRGB(screen-\>format, 255, 0, 0));

SDL\_RWops \*rw;

char\* FILE\_NAME = “tex3.png”;

ifstream file (FILE\_NAME, ios::in | ios::binary | ios::ate);  
ifstream::pos\_type fileSize;  
char\* fileContents;  
if(file.is\_open())  
{  
fileSize = file.tellg();  
fileContents = new char[fileSize];  
file.seekg(0, ios::beg);  
if(!file.read(fileContents, fileSize)) cout \<\< “fail to read” \<\< endl;  
file.close();  
}

rw = SDL\_RWFromMem(fileContents, fileSize);  
SDL\_Surface \*two = IMG\_Load\_RW(rw, 1);  
printf(“%s\n”, SDL\_GetError());

SDL\_Rect rect, rect2;  
rect2.x = 0;  
rect2.y = 0;  
rect2.w = two → w;  
rect2.h = two → h;  
SDL\_BlitSurface(two, NULL, screen, &rect2);

SDL\_Flip(screen);

SDL\_Delay(3000);  
}

Problem solved!

2011/10/14 Yuri David Santos \> Hi, Patrick

> Sorry for the lack of information. The BMP image that I said cannot be  
> loaded was the one in this link:
> 
> [http://www.kekkai.org/roger/sdl/rwops/penguin.bmp](http://www.kekkai.org/roger/sdl/rwops/penguin.bmp)
> 
> I’m using Windows 7 32-bit and compiling with the MingW with this line:
> 
> g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll
> 
> My code:
> 
> #include “SDL/SDL.h”  
> #include \<stdio.h\>
> 
> int main(int argc, char \*argv){  
> SDL\_Init(SDL\_INIT\_VIDEO);
> 
> atexit(SDL\_Quit);
> 
> SDL\_Surface \*screen = SDL\_SetVideoMode(640, 480, 16, SDL\_DOUBLEBUF);
> 
> FILE \*file = fopen(“penguin.bmp”, “r”);  
> if(!file) {  
> printf(“Couldn’t load penguin.bmp\n”);  
> exit(1);  
> }
> 
> SDL\_RWops \*rw = SDL\_RWFromFP(file, 0);  
> SDL\_Surface \*one = SDL\_LoadBMP\_RW(rw, 0);  
> printf(“%s”, SDL\_GetError());
> 
> SDL\_FreeRW(rw);  
> fclose(file);
> 
> SDL\_Rect rect;  
> rect.x = rect.y = 20;  
> rect.w = one → w;  
> rect.y = one → h;  
> SDL\_BlitSurface(one, NULL, screen, &rect);
> 
> SDL\_Flip(screen);
> 
> SDL\_Delay(3000);  
> }
> 
> Remembering that this worked in Linux Ubuntu 10.04 with g++ and the same  
> image. Ok, this is for BMPs.  
> But for PNGs, I haven’t get any image to work in Windows. Again, in this  
> case, I loaded PNGs in Linux without problem, but in Windows I get the  
> message: “Error reading the PNG file”. The code used was:
> 
> char pBuffer[2157];  
> FILE\* file = fopen(“cursor.png”, “r”);
> 
> fseek(file, 0, SEEK\_SET);  
> fread(pBuffer, sizeof(char), 2157, file);  
> SDL\_RWops\* rw = SDL\_RWFromMem(pBuffer, 2157);  
> SDL\_Surface\* two = IMG\_Load\_RW(rw, 1);  
> printf(“%s\n”, SDL\_GetError());
> 
> And built with:
> 
> g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll SDL\_image.dll
> 
> I have tested with the PNG file attached in this message. It works in Linux  
> but not in Windows. It’s strange because loading the PNG file directly with  
> IMG\_Load, it works even in Windows.  
> I’m trying to use SDL\_RWops to load all my resources from one file, but I  
> can’t find any Windows working example.
> 
> I’m grateful for any help.  
> Thnaks in advance!
> 
> 2011/10/14 Patrick Baggett \<baggett.patrick at [gmail.com](http://gmail.com)\>
> 
> > Yuri,
> > 
> > It might be more productive if you can create a minimal test case that  
> > fails and links to PNG/BMP images that fail. For example a program that does  
> > something like:
> > 
> > #define FILENAME “myfile.png”  
> > int main() {
> > 
> > ```
> > if(!TryIMG_Load(FILENAME)) {
> > printf("IMG_Load() for %s failed!\n", FILENAME);
> > return 1;
> > }
> > 
> > if(!TryIMG_Load_RW(FILENAME)) {
> > printf("IMG_Load() for %s failed!\n", FILENAME);
> > return 2;
> > }
> > 
> > printf("Test case success!\n");
> > return 0;
> > 
> > ```
> > 
> > }
> > 
> > The problem with saying that it fails for some images and not others, and  
> > then not providing test cases is that if I were to test 1,000 images right  
> > now, how would I know if I was testing the “right kind” to trigger this bug?  
> > After trying 1,000 images, I’d sooner believe the problem is with _your_ code  
> > or maybe with \*your \* compiler that you used to build SDL/libpng/your  
> > app, even if it was in fact a bug in someone else’s code. Help us help you,  
> > give us the tools and data we need to figure out what the problem might be.
> > 
> > Patrick Baggett
> > 
> > On Fri, Oct 14, 2011 at 1:23 PM, Yuri David Santos wrote:
> > 
> > > I found that this problem occurs just for some BMP files, not all. Some 8  
> > > bits BMP.  
> > > Another problem I’m having is to load PNG files. With IMG\_Load it works,  
> > > but using the IMG\_Load\_RW to load from memory or from a memory pointer, the  
> > > SDL\_GetError() says “Error reading the PNG file”. I don’t know what this can  
> > > be. The libpng.dll is in the same folder that the SDL\_image.dll, and is  
> > > loading successfully directly from the file (with IMG\_Load).
> > > 
> > > Anybody get this problem too?
> > > 
> > > Thanks in advance!
> > > 
> > > 2011/10/14 Yuri David Santos
> > > 
> > > > Hi!
> > > > 
> > > > I’m new to this mailing list. I have googled arround but I’ve found no  
> > > > answers to this. I followed the first tutorial in this link:
> > > > 
> > > > [http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)
> > > > 
> > > > In Linux it works great (all the 4 parts), but in Windows, the same code  
> > > > results in the error message “error reading from datastream”. The code is  
> > > > exactly as in the tutorial above. Anyone have any ideas?
> > > > 
> > > > Thanks in advance!
> > > > 
> > > > –  
> > > > Yuri
> > > 
> > > –  
> > > Yuri David Santos  
> > > Aleva - Solu??es em Software  
> > > www.aleva.com.br
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> –  
> Yuri David Santos  
> Aleva - Solu??es em Software  
> www.aleva.com.br

–  
Yuri David Santos  
Aleva - Solu??es em Software  
www.aleva.com.br

---

<div class="post-metadata">

**Author:** ![Patrick\_Baggett](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@Patrick\_Baggett](https://discourse.libsdl.org/u/Patrick_Baggett)\
**Post date:** [October 15, 2011, 12:31am UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/6 "2011-10-15T00:31:23Z")

</div>

> Hi, Patrick
> 
> Sorry for the lack of information. The BMP image that I said cannot be  
> loaded was the one in this link:
> 
> [http://www.kekkai.org/roger/sdl/rwops/penguin.bmp](http://www.kekkai.org/roger/sdl/rwops/penguin.bmp)
> 
> I’m using Windows 7 32-bit and compiling with the MingW with this line:
> 
> g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll
> 
> My code:
> 
> #include “SDL/SDL.h”  
> #include \<stdio.h\>
> 
> int main(int argc, char \*argv){  
> SDL\_Init(SDL\_INIT\_VIDEO);
> 
> atexit(SDL\_Quit);
> 
> SDL\_Surface \*screen = SDL\_SetVideoMode(640, 480, 16, SDL\_DOUBLEBUF);
> 
> FILE \*file = fopen(“penguin.bmp”, “r”);

Probably should be “rb” as the second argument to fopen() since you aren’t  
reading text but binary data. Otherwise, any bytes that are the ‘\n’  
character get converted to ‘\r\n’ on Win32 platforms and other nastiness.  
I’m not near a compiler, but that might help.On Fri, Oct 14, 2011 at 2:17 PM, Yuri David Santos wrote:

> if(!file) {  
> printf(“Couldn’t load penguin.bmp\n”);  
> exit(1);  
> }
> 
> SDL\_RWops \*rw = SDL\_RWFromFP(file, 0);  
> SDL\_Surface \*one = SDL\_LoadBMP\_RW(rw, 0);  
> printf(“%s”, SDL\_GetError());
> 
> SDL\_FreeRW(rw);  
> fclose(file);
> 
> SDL\_Rect rect;  
> rect.x = rect.y = 20;  
> rect.w = one → w;  
> rect.y = one → h;  
> SDL\_BlitSurface(one, NULL, screen, &rect);
> 
> SDL\_Flip(screen);
> 
> SDL\_Delay(3000);  
> }
> 
> Remembering that this worked in Linux Ubuntu 10.04 with g++ and the same  
> image. Ok, this is for BMPs.  
> But for PNGs, I haven’t get any image to work in Windows. Again, in this  
> case, I loaded PNGs in Linux without problem, but in Windows I get the  
> message: “Error reading the PNG file”. The code used was:
> 
> char pBuffer[2157];  
> FILE\* file = fopen(“cursor.png”, “r”);
> 
> fseek(file, 0, SEEK\_SET);  
> fread(pBuffer, sizeof(char), 2157, file);  
> SDL\_RWops\* rw = SDL\_RWFromMem(pBuffer, 2157);  
> SDL\_Surface\* two = IMG\_Load\_RW(rw, 1);  
> printf(“%s\n”, SDL\_GetError());
> 
> And built with:
> 
> g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll SDL\_image.dll
> 
> I have tested with the PNG file attached in this message. It works in Linux  
> but not in Windows. It’s strange because loading the PNG file directly with  
> IMG\_Load, it works even in Windows.  
> I’m trying to use SDL\_RWops to load all my resources from one file, but I  
> can’t find any Windows working example.
> 
> I’m grateful for any help.  
> Thnaks in advance!
> 
> 2011/10/14 Patrick Baggett \<@Patrick\_Baggett\>
> 
> > Yuri,
> > 
> > It might be more productive if you can create a minimal test case that  
> > fails and links to PNG/BMP images that fail. For example a program that does  
> > something like:
> > 
> > #define FILENAME “myfile.png”  
> > int main() {
> > 
> > ```
> > if(!TryIMG_Load(FILENAME)) {
> > printf("IMG_Load() for %s failed!\n", FILENAME);
> > return 1;
> > }
> > 
> > if(!TryIMG_Load_RW(FILENAME)) {
> > printf("IMG_Load() for %s failed!\n", FILENAME);
> > return 2;
> > }
> > 
> > printf("Test case success!\n");
> > return 0;
> > 
> > ```
> > 
> > }
> > 
> > The problem with saying that it fails for some images and not others, and  
> > then not providing test cases is that if I were to test 1,000 images right  
> > now, how would I know if I was testing the “right kind” to trigger this bug?  
> > After trying 1,000 images, I’d sooner believe the problem is with _your_ code  
> > or maybe with \*your \* compiler that you used to build SDL/libpng/your  
> > app, even if it was in fact a bug in someone else’s code. Help us help you,  
> > give us the tools and data we need to figure out what the problem might be.
> > 
> > Patrick Baggett
> > 
> > On Fri, Oct 14, 2011 at 1:23 PM, Yuri David Santos wrote:
> > 
> > > I found that this problem occurs just for some BMP files, not all. Some 8  
> > > bits BMP.  
> > > Another problem I’m having is to load PNG files. With IMG\_Load it works,  
> > > but using the IMG\_Load\_RW to load from memory or from a memory pointer, the  
> > > SDL\_GetError() says “Error reading the PNG file”. I don’t know what this can  
> > > be. The libpng.dll is in the same folder that the SDL\_image.dll, and is  
> > > loading successfully directly from the file (with IMG\_Load).
> > > 
> > > Anybody get this problem too?
> > > 
> > > Thanks in advance!
> > > 
> > > 2011/10/14 Yuri David Santos
> > > 
> > > > Hi!
> > > > 
> > > > I’m new to this mailing list. I have googled arround but I’ve found no  
> > > > answers to this. I followed the first tutorial in this link:
> > > > 
> > > > [http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)
> > > > 
> > > > In Linux it works great (all the 4 parts), but in Windows, the same code  
> > > > results in the error message “error reading from datastream”. The code is  
> > > > exactly as in the tutorial above. Anyone have any ideas?
> > > > 
> > > > Thanks in advance!
> > > > 
> > > > –  
> > > > Yuri
> > > 
> > > –  
> > > Yuri David Santos  
> > > Aleva - Solu??es em Software  
> > > www.aleva.com.br
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> –  
> Yuri David Santos  
> Aleva - Solu??es em Software  
> www.aleva.com.br
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Yuri\_David\_Santos](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/yuri_david_santos/32/2395_2.png) [@Yuri\_David\_Santos](https://discourse.libsdl.org/u/Yuri_David_Santos)\
**Post date:** [October 15, 2011, 12:48am UTC](https://discourse.libsdl.org/t/sdl-loadbmp-rw-returning-error-reading-from-datastream/18517/7 "2011-10-15T00:48:52Z")

</div>

Thanks, Patrick!

2011/10/14 Patrick Baggett \<baggett.patrick at [gmail.com](http://gmail.com)\>\>

> On Fri, Oct 14, 2011 at 2:17 PM, Yuri David Santos wrote:
> 
> > Hi, Patrick
> > 
> > Sorry for the lack of information. The BMP image that I said cannot be  
> > loaded was the one in this link:
> > 
> > [http://www.kekkai.org/roger/sdl/rwops/penguin.bmp](http://www.kekkai.org/roger/sdl/rwops/penguin.bmp)
> > 
> > I’m using Windows 7 32-bit and compiling with the MingW with this line:
> > 
> > g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll
> > 
> > My code:
> > 
> > #include “SDL/SDL.h”  
> > #include \<stdio.h\>
> > 
> > int main(int argc, char \*argv){  
> > SDL\_Init(SDL\_INIT\_VIDEO);
> > 
> > atexit(SDL\_Quit);
> > 
> > SDL\_Surface \*screen = SDL\_SetVideoMode(640, 480, 16, SDL\_DOUBLEBUF);
> > 
> > FILE \*file = fopen(“penguin.bmp”, “r”);
> 
> Probably should be “rb” as the second argument to fopen() since you aren’t  
> reading text but binary data. Otherwise, any bytes that are the ‘\n’  
> character get converted to ‘\r\n’ on Win32 platforms and other nastiness.  
> I’m not near a compiler, but that might help.
> 
> > if(!file) {  
> > printf(“Couldn’t load penguin.bmp\n”);  
> > exit(1);  
> > }
> > 
> > SDL\_RWops \*rw = SDL\_RWFromFP(file, 0);  
> > SDL\_Surface \*one = SDL\_LoadBMP\_RW(rw, 0);  
> > printf(“%s”, SDL\_GetError());
> > 
> > SDL\_FreeRW(rw);  
> > fclose(file);
> > 
> > SDL\_Rect rect;  
> > rect.x = rect.y = 20;  
> > rect.w = one → w;  
> > rect.y = one → h;  
> > SDL\_BlitSurface(one, NULL, screen, &rect);
> > 
> > SDL\_Flip(screen);
> > 
> > SDL\_Delay(3000);  
> > }
> > 
> > Remembering that this worked in Linux Ubuntu 10.04 with g++ and the same  
> > image. Ok, this is for BMPs.  
> > But for PNGs, I haven’t get any image to work in Windows. Again, in this  
> > case, I loaded PNGs in Linux without problem, but in Windows I get the  
> > message: “Error reading the PNG file”. The code used was:
> > 
> > char pBuffer[2157];  
> > FILE\* file = fopen(“cursor.png”, “r”);
> > 
> > fseek(file, 0, SEEK\_SET);  
> > fread(pBuffer, sizeof(char), 2157, file);  
> > SDL\_RWops\* rw = SDL\_RWFromMem(pBuffer, 2157);  
> > SDL\_Surface\* two = IMG\_Load\_RW(rw, 1);  
> > printf(“%s\n”, SDL\_GetError());
> > 
> > And built with:
> > 
> > g++ -o test.exe main.cpp -lmingw32 libSDLmain.a SDL.dll SDL\_image.dll
> > 
> > I have tested with the PNG file attached in this message. It works in  
> > Linux but not in Windows. It’s strange because loading the PNG file directly  
> > with IMG\_Load, it works even in Windows.  
> > I’m trying to use SDL\_RWops to load all my resources from one file, but I  
> > can’t find any Windows working example.
> > 
> > I’m grateful for any help.  
> > Thnaks in advance!
> > 
> > 2011/10/14 Patrick Baggett \<baggett.patrick at [gmail.com](http://gmail.com)\>
> > 
> > > Yuri,
> > > 
> > > It might be more productive if you can create a minimal test case that  
> > > fails and links to PNG/BMP images that fail. For example a program that does  
> > > something like:
> > > 
> > > #define FILENAME “myfile.png”  
> > > int main() {
> > > 
> > > ```
> > > if(!TryIMG_Load(FILENAME)) {
> > > printf("IMG_Load() for %s failed!\n", FILENAME);
> > > return 1;
> > > }
> > > 
> > > if(!TryIMG_Load_RW(FILENAME)) {
> > > printf("IMG_Load() for %s failed!\n", FILENAME);
> > > return 2;
> > > }
> > > 
> > > printf("Test case success!\n");
> > > return 0;
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > The problem with saying that it fails for some images and not others, and  
> > > then not providing test cases is that if I were to test 1,000 images right  
> > > now, how would I know if I was testing the “right kind” to trigger this bug?  
> > > After trying 1,000 images, I’d sooner believe the problem is with _your_ code  
> > > or maybe with \*your \* compiler that you used to build SDL/libpng/your  
> > > app, even if it was in fact a bug in someone else’s code. Help us help you,  
> > > give us the tools and data we need to figure out what the problem might be.
> > > 
> > > Patrick Baggett
> > > 
> > > On Fri, Oct 14, 2011 at 1:23 PM, Yuri David Santos wrote:
> > > 
> > > > I found that this problem occurs just for some BMP files, not all. Some  
> > > > 8 bits BMP.  
> > > > Another problem I’m having is to load PNG files. With IMG\_Load it works,  
> > > > but using the IMG\_Load\_RW to load from memory or from a memory pointer, the  
> > > > SDL\_GetError() says “Error reading the PNG file”. I don’t know what this can  
> > > > be. The libpng.dll is in the same folder that the SDL\_image.dll, and is  
> > > > loading successfully directly from the file (with IMG\_Load).
> > > > 
> > > > Anybody get this problem too?
> > > > 
> > > > Thanks in advance!
> > > > 
> > > > 2011/10/14 Yuri David Santos
> > > > 
> > > > > Hi!
> > > > > 
> > > > > I’m new to this mailing list. I have googled arround but I’ve found no  
> > > > > answers to this. I followed the first tutorial in this link:
> > > > > 
> > > > > [http://www.kekkai.org/roger/sdl/rwops/rwops.html](http://www.kekkai.org/roger/sdl/rwops/rwops.html)
> > > > > 
> > > > > In Linux it works great (all the 4 parts), but in Windows, the same  
> > > > > code results in the error message “error reading from datastream”. The code  
> > > > > is exactly as in the tutorial above. Anyone have any ideas?
> > > > > 
> > > > > Thanks in advance!
> > > > > 
> > > > > –  
> > > > > Yuri
> > > > 
> > > > –  
> > > > Yuri David Santos  
> > > > Aleva - Solu??es em Software  
> > > > www.aleva.com.br
> > > > 
> > > > * * *
> > > > 
> > > > SDL mailing list  
> > > > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > > > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> > > 
> > > * * *
> > > 
> > > SDL mailing list  
> > > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> > 
> > –  
> > Yuri David Santos  
> > Aleva - Solu??es em Software  
> > www.aleva.com.br
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

–  
Yuri David Santos  
Aleva - Solu??es em Software  
www.aleva.com.br
