How to read an image?

Apr 30, 2008 at 1:51pm
Hello,
i want to read an image and to insert it as a input for a function
this is my part of code:

///////////////////////////////////////
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;

bool search(BYTE *bt)
{
return true;
}
int main () {
ifstream myfile;

if (myfile.is_open())
{
search((BYTE*)myfile.open("C:\search.png"));
myfile.close();
cout<< "function success";
}
else
cout<< "unable to open file";
return 0;
}
/////////////////////////////////////////

but i got this error:

////////////////////////////////////////
OPEN.CPP(16) : error C2440: 'type cast' : cannot convert from 'void' to 'unsigned char *'
Expressions of type void cannot be converted to other types
/////////////////////////////////////////

i haven't some much knowledge in c++ and i don't know how to do it exactly, if someone can help me i will be gratefull.
thanks.
Apr 30, 2008 at 8:55pm
Firt of all, please use the code tag so that your code is easier to read.
Secondly, please think before pasting bits of code that you may have found on the net anywhere in your program.
This :
1
2
3
bool search(BYTE *bt) {
   return true;
}

Is pointless.

Here :
 
search((BYTE*)myfile.open("C:\search.png"));


You cast myfile.open(); which returns a "bool" to a BYTE*, which you then put in your function which just returns true anyway. Again, pointless.

You should probably think, then try something like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;

int main () {
	ifstream myfile;
	myfile.open("C:\search.png");

	if (myfile.is_open()) {
		myfile.close();
		cout<< "function success";
	} else {
		cout<< "unable to open file";
	}

	return 0;
}
May 1, 2008 at 11:05pm
Hello,
thanks a lot it works like you describe it, but how to pass the image to my function search??? i have wrote tha function like that
1
2
3
bool search(BYTE *bt) {
   return true;
}

to just make the thing easier because the function should have an image like an input and a boolean(something that exist in the image=true or not=false) like an output.It's possible or not???
and thanks one more time.
May 1, 2008 at 11:11pm
Hi.
Are you wanting to pass just the raw data from the image? Or more information? I am not familiar with PNG file format, but instead the TGA format.

In my code I have a structure for loading the image

1
2
3
4
5
6
struct TGA {
 int Height;
 int Width;
 // Etc
 BYTE *data;
}


You need to understand the format of the image you are working with. And then once the file is open you can read the header (if it has one) and them the bytes into the *data stream. From there you can pass this off to a function.

I could provide you with some sample code showing how to load a TGA image in a couple of hours (after work) if you'd like?
May 1, 2008 at 11:40pm
Yes of course and thakns
May 1, 2008 at 11:46pm
Ok. I will post up some links tonight after work for you. The code is an extract from a 3D engine I developed. It's part of the texture loading system that loads TGA textures to be displayed on models/objects.

Z.
May 2, 2008 at 12:33am
Ok, I guess its up to the OP to figure out what he wants.
There is BMP -> TGA -> PNG formats

BMP - raw bitmap data, no compression, most bmps are up to 24bits - which is only rgb. It is amazingly inconvenient to put 32bits into a bmp. I don't mean impossible, I mean its a damn hassle to do so. However, it is amazing easy to manipulate, after the file header there is no compression and just raw bitmap data, you can take this data and blit it and it will display correctly (abet upside down since bitmaps 0,0 are bottom left instead of upper left and their heights are given in negatives)

TGA - supports 32 bit, contains runlength compression - which isn't that hard to figure out, but for starting out it may be enough just to figure out how to manipulate just bmps. The compression means its not in a format that be simply blitted to the screen. It'll require at least a decompresser.

PNG - Better compression than TGA and is nearly BMP quality. Back in school most people made their 3d engines using this format.

The easiest by far is BMP - you can load the file properly in something like under 8 lines of code.
Last edited on May 2, 2008 at 12:36am
May 2, 2008 at 9:58pm
fedora: drop me an email at public01 _at_ zaita.com

I will reply with my TGA loading code :) and some BMP Loading code I think I have.
Last edited on May 2, 2008 at 9:58pm
Topic archived. No new replies allowed.