Hi all,
I have a problem to read and write file image in extension (*bmp, *jpg,*ico and so on). if i use order fgetch(), it can't be read the character from image and only can be read file character as A...Za..z0..9..etc.
what are the order to read file image in C/C++ ?
Thanks.
ImamkomC++
How to read and write file image *bmp,*jpg,*ico and so on using c++read and write file image distinct with file char usually, may be hid
Page 1 of 1
11 Replies - 116286 Views - Last Post: 22 April 2010 - 06:56 AM
#1
How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 27 June 2007 - 04:10 AM
Replies To: How to read and write file image *bmp,*jpg,*ico and so on using c++
#2
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 30 June 2007 - 12:17 PM
Well you should be able to use fgetch() to read in a byte of data (not just alphanumeric characters) but you have to remember that the data that you get is not going to be very pretty to print to the screen... its data not ascii text.
You can find lots of information that the formats that that data will take for the various image formats. Each format is different and so you will want to either deal with each image type individually, or you will want to use a library which abstracts the finner points of image file formats. Most operating systems/platforms should support some native handling of images. For example Windows has lots of functions for loading BMP, ICO, and JPG files. Although Linux/Unix don't nativly handle such images, there are lots of associated libraries that do.
You can find lots of information that the formats that that data will take for the various image formats. Each format is different and so you will want to either deal with each image type individually, or you will want to use a library which abstracts the finner points of image file formats. Most operating systems/platforms should support some native handling of images. For example Windows has lots of functions for loading BMP, ICO, and JPG files. Although Linux/Unix don't nativly handle such images, there are lots of associated libraries that do.
#6
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 01 July 2007 - 08:44 AM
Hi all,
i will try to explain my post about read image file.
it's part of program to open a file :
***************************************
how to open or read image file ?
i have an image e.g. with extension *.ico
if i run to open with notepad in windows than see many character, but
if i using part program above to read all character from image file, than it doesn't to read all.
What's up ?
only fews character can be read. example i have name of file "animal.ico", and i try to implementaion
part of program like this :
actually, if my second program is true than automatic all charakter can be read till finish.
help me.
regard,
ImamkomC++.
i will try to explain my post about read image file.
it's part of program to open a file :
***************************************
...
FILE *input;
char get_char;
input = fopen("test.txt", "r");
while((get_char=fgetc(input))!= EOF){
cout<<get_char;
}
fclose(input);
...
how to open or read image file ?
i have an image e.g. with extension *.ico
if i run to open with notepad in windows than see many character, but
if i using part program above to read all character from image file, than it doesn't to read all.
What's up ?
only fews character can be read. example i have name of file "animal.ico", and i try to implementaion
part of program like this :
...
FILE *input;
char get_char;
input = fopen("animal.ico", "r");
while((get_char=fgetc(input))!= EOF){
cout<<get_char;
}
fclose(input);
...
actually, if my second program is true than automatic all charakter can be read till finish.
help me.
regard,
ImamkomC++.
#7
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 26 January 2009 - 07:47 AM
i have the same problem :s anybody can help me?
#8
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 26 January 2009 - 09:38 AM
#17
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 26 January 2009 - 10:28 AM
I would suggest you use a library like FreeImage or ImageMagick unless you are doing something *very* specific with the raw image data (even then I'd use one of those libs to load the image and just access the buffer directly).
If you must do it yourself, then I would do something like this:
1. stat() the file to get the size
2. use malloc() to allocate a byte (unsigned char) buffer large enough to hold the image (or use mmap() if the image is very large)
3. use open() and read() (*not* fopen() or fread()) to load the image into the allocated buffer
4. close the file
5. do whatever with the image data in the buffer
6. when you are done, release the buffer with free()
Matthew
If you must do it yourself, then I would do something like this:
1. stat() the file to get the size
2. use malloc() to allocate a byte (unsigned char) buffer large enough to hold the image (or use mmap() if the image is very large)
3. use open() and read() (*not* fopen() or fread()) to load the image into the allocated buffer
4. close the file
5. do whatever with the image data in the buffer
6. when you are done, release the buffer with free()
Matthew
#18
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 26 January 2009 - 11:02 AM
i have this and don't go to more then some lines.. not finish the file
what are wrong because with txt file works fine
int file;
file= open ("aa.jpg", O_RDWR , 0600);
int caracter,tam;
unsigned char byte[1];
while( tam=read(file, &byte, 1)>0)
{
cout << byte;
}
what are wrong because with txt file works fine
This post has been edited by couta: 26 January 2009 - 11:03 AM
#19
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 26 January 2009 - 12:21 PM
Why are you trying to read the file 1 byte at a time? Also, keep in mind that the image is binary data and you can't print to a terminal the full byte value range (0 to 255) since a terminal interprets byte values less than 32 as control characters, and byte values above 127 will vary depending on the terminal you are using.
Use stat() to get the file size and read the whole file with a single call to read()...
Matthew
Use stat() to get the file size and read the whole file with a single call to read()...
Matthew
#20 Guest_digitest*
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 20 April 2010 - 12:20 AM
Dear Mat,
I've tried to do it in the way you've written (myself), that is my code:
The result ss, as well as the mem, contains just several (3-4) symbols from the file, which is wrong because file size is 20kbytes! Anybody knows what to do about it?
I've also tried to use 'open' instead of '_rtl_open' function - but no luck.
I've tried to do it in the way you've written (myself), that is my code:
struct stat statbuf;
/* get information about the file */
stat("C:\\image.jpg", &statbuf);
/* display the information returned */
//ShowMessage(statbuf.st_size);
unsigned char *mem;
if ((mem =(char *)malloc(statbuf.st_size)) == NULL) ShowMessage("problem alloc");
int handle;
if ((handle = _rtl_open("C:\\image.jpg", O_BINARY)) == -1) ShowMessage("problem open file");
_rtl_read(handle, mem, statbuf.st_size);
AnsiString ss;
ss = *mem;
Memo1->Text=ss;
free(mem);
The result ss, as well as the mem, contains just several (3-4) symbols from the file, which is wrong because file size is 20kbytes! Anybody knows what to do about it?
I've also tried to use 'open' instead of '_rtl_open' function - but no luck.
matthew180, on 26 January 2009 - 09:28 AM, said:
I would suggest you use a library like FreeImage or ImageMagick unless you are doing something *very* specific with the raw image data (even then I'd use one of those libs to load the image and just access the buffer directly).
If you must do it yourself, then I would do something like this:
1. stat() the file to get the size
2. use malloc() to allocate a byte (unsigned char) buffer large enough to hold the image (or use mmap() if the image is very large)
3. use open() and read() (*not* fopen() or fread()) to load the image into the allocated buffer
4. close the file
5. do whatever with the image data in the buffer
6. when you are done, release the buffer with free()
Matthew
If you must do it yourself, then I would do something like this:
1. stat() the file to get the size
2. use malloc() to allocate a byte (unsigned char) buffer large enough to hold the image (or use mmap() if the image is very large)
3. use open() and read() (*not* fopen() or fread()) to load the image into the allocated buffer
4. close the file
5. do whatever with the image data in the buffer
6. when you are done, release the buffer with free()
Matthew
This post has been edited by JackOfAllTrades: 20 April 2010 - 06:12 AM
Reason for edit:: Added code tags.
#21
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 20 April 2010 - 06:16 AM
You can't display/manipulate binary data as a string. A binary file may/will contain embedded NULL characters, which will be interpreted as the end of the string, making all string related functions stop at that point. It might be easier to tell us what it is you're attempting to do.
#22 Guest_digitest*
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 21 April 2010 - 11:29 PM
JackOfAllTrades, on 20 April 2010 - 05:16 AM, said:
You can't display/manipulate binary data as a string. A binary file may/will contain embedded NULL characters, which will be interpreted as the end of the string, making all string related functions stop at that point. It might be easier to tell us what it is you're attempting to do.
What I am trying to do is reading(taking) some characters from the end part of jpg-file to manipulate after with them. For that, I need to read all of its contents into some kind of buffer, and that is what does not work, though windows notepad or any other viewer can do it!!!
Can you tell me some functions/methods for making "raw" reading of jpg file? I've just tried fgetc function and it was almost success, but from some position (somewhere after 400 chars) it began to produce the same wrong int code "-1", though file contains differs. I think the problem is in some "end/stop" character, after which reading process goes wrong or stops at all.
#23
Re: How to read and write file image *bmp,*jpg,*ico and so on using c++
Posted 22 April 2010 - 06:56 AM
HexDump is a program I write to display the contents of a file. This will display all of the bytes in a jpeg file.
Hexdump of course only reads in data into a buffer that is 16x64 = 1024 bytes -- much smaller than the size of a normal jpg. To determine the size of the buffer you will need you can do one of two things:
#1 you can actually use the data from inside of the file (the jpg knows how big it should be).
#2 you can can seekg() to the end of the file and then tellg() tell hoe many bytes are needed.
#3 you can use an OS-specific function that returns the file size.
Though if you are only interested in the data at a specific point in the file, you can just seekg() your way there, and NOT read in the entire file... you don't HAVE to load the whole thing into a buffer.
Note you can not use C-Strings with binary files, you CAN use C++ std::string which does not really care too much about nulls.
Hexdump of course only reads in data into a buffer that is 16x64 = 1024 bytes -- much smaller than the size of a normal jpg. To determine the size of the buffer you will need you can do one of two things:
#1 you can actually use the data from inside of the file (the jpg knows how big it should be).
#2 you can can seekg() to the end of the file and then tellg() tell hoe many bytes are needed.
#3 you can use an OS-specific function that returns the file size.
Though if you are only interested in the data at a specific point in the file, you can just seekg() your way there, and NOT read in the entire file... you don't HAVE to load the whole thing into a buffer.
Note you can not use C-Strings with binary files, you CAN use C++ std::string which does not really care too much about nulls.
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote







|