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 - 139388 Views - Last Post: 22 April 2010 - 06:56 AM Rate Topic: -----

#1 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

How to read and write file image *bmp,*jpg,*ico and so on using c++

Post icon  Posted 27 June 2007 - 04:10 AM

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++
Is This A Good Question/Topic? 1

Replies To: How to read and write file image *bmp,*jpg,*ico and so on using c++

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 1

#6 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

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 :
***************************************
...
		 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++.
Was This Post Helpful? 1

#7 couta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-January 09

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?
Was This Post Helpful? 0
  • +
  • -

#8 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: How to read and write file image *bmp,*jpg,*ico and so on using c++

Posted 26 January 2009 - 09:38 AM

Have a look at this thread, it might help you:
http://www.vbforums....p/t-261522.html
Was This Post Helpful? 0
  • +
  • -

#17 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

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
Was This Post Helpful? 2

#18 couta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-January 09

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

   
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

Was This Post Helpful? 0
  • +
  • -

#19 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

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
Was This Post Helpful? 1

#20 Guest_digitest*


Reputation:

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:
  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.


View Postmatthew180, 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

This post has been edited by JackOfAllTrades: 20 April 2010 - 06:12 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0

#21 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#22 Guest_digitest*


Reputation:

Re: How to read and write file image *bmp,*jpg,*ico and so on using c++

Posted 21 April 2010 - 11:29 PM

View PostJackOfAllTrades, 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.
Was This Post Helpful? 0

#23 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1