Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 25th, 2006, 9:33 AM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
video file binary access

i need to open a *.mjpeg video file in 1's and 0's and manupilate them.
	char ch;
	ifstream open ("movie.mjpeg" , ios::in | ios::binary);

	for(int i=0; i<40 ;i++)
	{
		open.get(ch);
		cout<<ch;
	}
	open.close();
i tried this out just to see if i can extract binary numbers from the file but it does not work.
i get some encrypted data like: 06014 ╪ ─ ▼ ☺♣☺☺☺☺☺☺ ☺☻♥♦♣
so how can i access the video file as a binary file..?
thanks
hbe02 is offline   Reply With Quote
Old Apr 25th, 2006, 10:00 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
All file I/O is binary ones and zeros. Binary mode versus text mode (a Windows phenomenon) has nothing to do with that fact. If you don't like the way your output looks, it's because your output device is expecting YOU to convert it to some presentational form. This is why such things as ASCII were invented. Calling a series of binary digits (say, 00100001) an 'A' is a convention, not a fact. You may as well say that some particular dot pattern in a 5 x 7 matrix is the true 'A' and all others are imposters. Just curious: when you 'cout' a supposed-character from an mpeg file, what did you EXPECT your output device to produce? Shakespeare? Lebenty-jillion little tiny pictures? If you want all possible binary values to result in readable (by readable, I mean visually) entities, then you need to apply some conversion scheme. ASCII-encoded hexadecimal is a popular choice, ala the 'hex editor'. It isn't exactly as if you were the very first person to stumble upon the desire to do this, and encountered this phenomenon-less phenomenon.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 25th, 2006, 12:20 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
ive tried using cout like this:
cout<<static_cast<int>(ch)<<endl;
which gives me output as such:
48
54
48
49
52
-1
-40
-1
-60
0
31
0
0
1
5
1
1
1
1
1
1
0
0
0
0
0
0
0
0
1
2
3
4
5
6
7
8
9
10
11
Press any key to continue
where each number is the ASCII for the symbols ive got...
one question though.. why did i recieve negative numbers. i thought acsi was from 0 to 255 ?
so what i will do is convert each number to a 8 bit binary and handle the rest as i wish.. is this the best way to go about it..?
hbe02 is offline   Reply With Quote
Old Apr 25th, 2006, 2:49 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Doesn't matter what ASCII is. True ASCII is only 0-127, anyway. You specified a char, ch. That's an 8-bit signed binary entity with no other discernible characteristics. Your compiler and your machine have no clue that you wish it to be, say, ASCII. Then you cast to an int, which is signed, also. The thang sees the bits as a signed integer, probably 32 bits long on your machine, instead of a mere 8. If you can express what you WANT to see, someone here can soitenly he'p ya. You mainly need to distinguish between what bits actually are and how they are interpreted. The EXACT same pattern may represent some numerical value if interpreted as a binary integer, another if interpreted as a float, and another if interpreted as text. Then there's binary-coded decimal, binary-coded hexadecimal, forty-leben forms of EBCDIC, on and on.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 25th, 2006, 9:07 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
As DaWei said, "char" is a signed value. Use "unsigned char" for your ch variable instead.
The Dark is offline   Reply With Quote
Old Apr 26th, 2006, 4:16 AM   #6
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
i will definitely change to unsigned...
but let me tell you what im trying to do. i want to read this *.Mjpeg file and make a packet of each frame of that movie.. send it from server to client and reassemble the frame on the client side to once again get the same *.Mjpeg file.
i think this method of reading the file will work, but as DaWie said, isnt using an integer a waste of memory space.? so my method is not optimal.. so how can i work to improve this.. since i need to read the first 40 bits which tell me how big the first frame is. i read it then the next 40 bits which tell me the length of the second frame and so on..
Dawie, you mentioned binary-coded decimal, binary-coded hexadecimal... so these are different datatypes i can use..? how can i learn about them.?
hbe02 is offline   Reply With Quote
Old Apr 26th, 2006, 4:27 AM   #7
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
here's a littlee update on my work:
void convert(int, char * &, int );
int main()
{

	char ch;
	
	ifstream open ("movie.mjpeg" , ios::in | ios::binary);

	for(int i=0; i<40 ;i++)
	{
                          char * binary = new char[8];
		open.get(ch);
		convert(static_cast<int>(ch),binary,8);
		cout<<binary<<endl;
	}
	open.close();

	//stream();	
	//reform();
	
	return 0;
}

void convert(int value , char * &field, int field_size)
{
	unsigned int exp=1;
	for (int i=1; i<field_size; i++)
		{
			exp = exp * 2;
		}
	
	for (i=field_size-1; i!=-1; i--) 
	{
		if (value >= exp) 
		{
			
			field[i] = '1';
			
			value = value - exp;
		}
		else 
		{
			field[i] = '0';
		}
	
		exp = exp / 2;
	}
	field[field_size] = NULL;
}

what i did is read a character form the file, convert its ASCII to an 8 bit binary number stored in a char* . and i just displayed the binary on screen.
it works fine except for the negative numbers i obtained before, and i was not able to change the char ch to unsigned char. because ifstream .get()or .read() do not take unsigned char as parameter, they take char& and char* respectively. how can i solve this problem of the negative ASCII values?
hbe02 is offline   Reply With Quote
Old Apr 26th, 2006, 9:52 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It's going to seem like I'm belaboring this point, but it's one you MUST come to grips with. A byte comprises eight bits, each of which may assume one of only two values. There are only 256 possible distinguishable byte values. A fairly useless set, wouldn't you say? What we do is arrange for them to be interpreted in differing ways at different times. We also arrange to use them in clusters so that we may increase the number of possible values. We still have to have agreement amongst ourselves how we are clustering them and how we are interpreting them. This set of four, 01000111 01011000 01110110 00011110, means one thing if I tell you it is a binary integer. It means something else entirely if I tell you it is a floating point number. It means another thing if I tell you it is four ASCII characters. It means something else if I tell you it is an RGB pixel with an alpha channel. Sign is merely another imposed concept.

The means of reading them from a file is the same, because neither the means nor the medium care what they are going to be used for. Shine your shoes with 'em, if you like.

Actual snip from a program to read a bitmap file: The principal applies to wave files, mpegs, whatever you care to rip into.
   ifstream inFile (fName.c_str (), ios::in | ios::binary);
   if (!inFile.is_open ()) return uhOh ("File didn't open: getImage");
   ...
   ...
   inFile.read ((char *) bmpImage.pImageData, bmpImage.imageInfo.nWidth * 
                                     bmpImage.imageInfo.nHeight * nBytesEntry);
   if (!inFile.good ())
   {
      delete [] bmpImage.pImageData;
      return uhOh ("File read failure: getImage: image data");
   }
The packets sent from client to server are normally perfectly happy to transmit any bytes you care to stash in them. Pattern-insensitive. To make them easier to deal with as a DEFINED entity, we agree to format and pack them in a given way. You've obviously touched on that, as you say, "first 40 bits", which suggests that you understand there is an underlying arrangement, a protocol. Just take the raw data and peel it appropriately.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC