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.
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) {
returntrue;
}
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>
usingnamespace 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;
}
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) {
returntrue;
}
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.
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?
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.
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.