How can i teach my image binary values?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <fstream>
#include <iostream>
#include <bitset>
using namespace std;

int main()
{ifstream file1;
ofstream file2;
file1.open("F:\\exam.jpg",ifstream::binary);
file2.open("C:\\exam.bmp",ofstream::binary);
char *a,*b;
long size;
file1.seekg(0,ifstream::end);
size=file1.tellg();
file1.seekg(0);
a=new char[size];
file1.read(a,size);
file2.write(a,size);
b=new char[size];
file1>>b;
for(int i=0;i<size;i++){
bitset<8>binary(b[i]);
for(int j=0;j<size;j+=2){
if((binary[j]=0)&&(binary[j++]=0))
   b[j]='a';    
   if((binary[j]=0)&&(binary[j++]=1)) 
   b[j]='b';
   if((binary[j]=1)&&(binary[j++]=0))
   b[j]='c';
   if((binary[j]=1)&&(binary[j++]=1))
   b[j]='d';
      cout<<b[j];
        }}
delete[] a;
delete[] b;
    system("PAUSE");
    return EXIT_SUCCESS;
}
wat?

What are you trying to do?
Ok:)Excuse me.I forgot to gloss.I want to teach a image file binary values.
exam.jpg image file's binary value:11111111...
If u bed another.jpg before exam.jpg.Another image file's binary value (another.jpg):11111111...

This results are same.But this is impossible.Because this pictures are not same.
I don't follow you explanation. Your use of the words "gloss", "teach", and "bed" are not standard English.

If you are just looking at the initial bytes, these are likely to be the same for the same type of file, as the first part of a jpeg file is the header, not the data.

As the i/p file is a .jpg and the o/p is a .bmp, are you trying to convert a image to a different format?
I am not trying to convert.I want to learn image binary datas.
Such as this:http://www.c-sharpcorner.com/UploadFile/hscream/ImagetoBinary02132007164649PM/ImagetoBinary.aspx

But i don't use Visual C++ and .Net.I use Dev-C++.
Ok... So what is your program intended to do?

So far...

A standard start..

1
2
3
4
5
6
7
#include <fstream>
#include <iostream>
#include <bitset>
using namespace std;

int main()
{


Open two files (file1 = .jpg for reading, file2 = .bmp for output)...

1
2
3
4
ifstream file1;
ofstream file2;
file1.open("F:\\exam.jpg",ifstream::binary);
file2.open("C:\\exam.bmp",ofstream::binary);


Declare variables for buffer pointers and size

1
2
char *a,*b;
long size;


Obtain file size by seeking to end then resetting file pointer

1
2
3
file1.seekg(0,ifstream::end);
size=file1.tellg();
file1.seekg(0);


allocate memory

a=new char[size];

read file1 into a, then write it out to file2 (this is what made me think you wanted to do conversion, as file1 = .jpg and file2 = .bmp)

1
2
file1.read(a,size);
file2.write(a,size);


Allocate a second buffer

b=new char[size];

Read file into b (why >> this time? does file pointer need to be reset here?)

file1>>b;

Process the file (will look at this more closely later)
- for each byte in buffer b do???

1
2
3
4
5
6
7
8
9
10
11
12
13
for(int i=0;i<size;i++){
bitset<8>binary(b[i]);
for(int j=0;j<size;j+=2){
if((binary[j]=0)&&(binary[j++]=0))
   b[j]='a';    
   if((binary[j]=0)&&(binary[j++]=1)) 
   b[j]='b';
   if((binary[j]=1)&&(binary[j++]=0))
   b[j]='c';
   if((binary[j]=1)&&(binary[j++]=1))
   b[j]='d';
      cout<<b[j];
        }}

Delete buffers

1
2
delete[] a;
delete[] b;


Standard ending

1
2
3
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
Thank u.But this is not my question's answer.This is my codes.And i know this.
I want to learn this:exam.jpg' binary values:111011111011110001100101000110001100001100..
00:a
01:b
10:c
11:d

exam.jpg's convert binary values:dcddc....


another.jpg's binary values:01010000011111100001010101010101....
another.jpg's convert binary values:bbaabddda.....
Ok?
Well, equality in C++ is tested with ==, so your tests like

if((binary[j]=0)&&(binary[j++]=0))

are wrong. And the use of j++ in the array accessor ([j++]) is going to throw your +=2 loop

Do you mean

if((binary[j]==0)&&(binary[j+1]=0))

Then

for(int j=0;j<size;j+=2){

is from 0 to the size of the image, which is typically kilobytes, whereas you're using j to access an 8 bit bitset, so it's only vaid from 0 - 7.

Just tried to run your code - it dies with an access violation when trying to access the bitset at index 8 -- as expected.
I think he wants to be able to create an image file using his own data.
I don't recommend JPG for that. Pick a lossless file format, like PNG, TGA, BMP, or GIF.
In any case, you'll need a library to convert your raw image data to a file of that type.
Google around "libpng", etc.
Good luck!
Thank u for ur helps.
Topic archived. No new replies allowed.