PDA

View Full Version : Add QByteArray



CodeHunt
1st May 2012, 16:20
I have two binary files, of which I have no clue about their data organization. I have them as QByteArray stream and overlap them meaning a[i]=b[i]+c[i].

I made an attempt with

QByteArray x,y,z
for(k=0;k<x.size();k++){
for(l=0;l<y.size();l++){
if(k=l){
z[k]=x[k]+y[l];
z.append(z[k]);
}
}
}

I know that it was a naive attempt but couldn't do more as I have no clue about the internal data organization, all I know is they are binary. Any suggestions or help please !!

ChrisW67
1st May 2012, 23:53
Your attempt uses two loops where one is required and contains a number of coding errors that stop it even compiling. If X.size() == Y.size() then:


QByteArray x(10, 0x0A);
QByteArray y(10, 0x0B);
QByteArray z;

Q_ASSERT(x.size() == y.size());
for (int i = 0; i < x.size(); ++i)
z.append( x.at(i) + y.at(i) );
qDebug() << x.toHex();
qDebug() << y.toHex();
qDebug() << z.toHex();



Really though, your problem is potentially more difficult. You have two binary blobs (X & Y) of indeterminate length and structure and (for whatever reason) you want to byte-wise add them to get a third binary blob. Here are the obvious questions:

What is the desired behaviour if X & Y are different lengths?
What is the desired behaviour if the sum of bytes overflows a byte?
Are you after the sum or the exclusive-or of the bytes?
Are you trying to implement an encryption algorithm?

CodeHunt
2nd May 2012, 00:14
X and Y will be of same length always. If there is an overflow, then it should be rounded to the nearest decimal. I don't expect it to overflow as I am sure that the array size will be the same and therefore the values.I am concerned about the sum.Its not encryption algorithms, but deals with images largely.

ChrisW67
2nd May 2012, 00:42
Bytes are integers: there's no such beast as "rounded to the nearest decimal". You will never get fractional parts.

Overflowing has nothing to do with the size of the arrays. It is about the size of the elements in the array. If byte A has the value 130 and byte B the value 134 then the sum, 264, cannot fit in a byte, i.e. it overflows the byte which can only hold a number in the range 0 to 255.


unsigned char a = 130;
unsigned char b = 134;
unsigned char c = a + b;
qDebug() << c;
// prints 8 not 264


If you are manipulating images then you really should look at the capability of QImage and QPainter

CodeHunt
2nd May 2012, 01:02
I understand that, as I know that all the value in my program range between 0-127 ( if any negative values encountered,absolute values are considered). I am give two binary files which are written by the same program and I know the values of data in the files. And I also know that those binary files are extracted from an image and they will of size in terms of pixels. Any ideas on how can I operate on the values so that I could get the output similar to what I get on placing one image exactly over the other.

ChrisW67
2nd May 2012, 02:02
There's more than one way to place an image over another (http://en.wikipedia.org/wiki/Alpha_compositing). The result may be only the image on top, or you only see the image underneath where the top image had transparent pixels, or you see a blended version of both images regardless of transparency etc.

It's not generally possible without knowing the precise nature of the bytes. Are they raw pixel values or are they some compressed or well-known format? Is the image colour or monochrome? Are the colour channels red/green/blue or some other colour model? Do the images have an alpha channel?

Another thought:

I understand that, as I know that all the value in my program range between 0-127 ( if any negative values encountered,absolute values are considered).
A signed byte can hold numbers in the range -128 to 127. The absolute value of the negative extreme can still cause overflow issues if you add two of them.

CodeHunt
3rd May 2012, 11:31
I am writing the binary files using following method.
//....
typedef std::basic_ofstream<unsigned char, std::char_traits<unsigned char> > uofstream;
uofstream op(filename, ios::out | ios::binary);
op.write(size, 5);
op.write(RGrBl, width*height*3);
op.write(a,width*height*difference);/difference is int size[4]
op.close();



I want to add the values at each BYTE in the files created by above function. I am unable to write it because of exception at run time saying bad memory allocation, and referring to mlock.c

I just want to add the hex values at all the bytes but I am getting exceptions at time saying bad memory allocation and referring to mlock.c. I am using

//x and y are read from files created using the above
QByteArray alpha; for(int k=0;k<qMin(x.size(), y.size());k++){ alpha.append(x[k]-y[k]); } qDebug()<<z.toHex();
QByteArray beta; for(int j=0;j<qMin(x.size(),alpha.size());++j) { alpha.append(x[j]+z[j]); } qDebug()<<beta.toHex();

Where do you think I am going wrong ?
Thanks

ChrisW67
3rd May 2012, 23:29
Where do you think I am going wrong ?
You should use
... tags around your code ;)

mlock.c is not part of Qt. Run your code in your debugger until it crashes and then look at the backtrace to find the offending part of your code.

This code:


//x and y are read from files created using the above
QByteArray alpha; for(int k=0;k<qMin(x.size(), y.size());k++){ alpha.append(x[k]-y[k]); } qDebug()<<z.toHex();
QByteArray beta; for(int j=0;j<qMin(x.size(),alpha.size());++j) { alpha.append(x[j]+z[j]); } qDebug()<<beta.toHex();

You add byte differences to an array called alpha then print an array called z.
You then add byte sums to the array called alpha then print the array called beta.
Neither debug output is likely to be what you expect.

Also, you second loop is bounds are set based x and alpha size but you are using them to index an unrelated z[] which is probably empty or, if not, smaller than either x or alpha. Out-of-bounds memory accesses are a recipe for program termination.