×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Unhappy No errors and a crash ! Yes I could use some help..

    Don't know what to really put here.. You'd need to know some theory first..

    Generally I'm trying to hide (watermark) information (text) into an image. I do that with ANDing the blue pixel with 254->"11111110". So the LSB of the pixel is 0 and then I add the bits of the text the user puts (after making the text to a binary table).

    Variables aside, which you don't know what they represent without the UI, do you see any problems with the code below? Qt turns out no errors.. But when I run it, it crashes (windows 7).

    So there goes:

    Code:
    void MainWindow::on_InsertWatermarkButton_clicked()
    {
        if (imageloaded==false)
        {
            ui->ImagePreviewLabel->setText("No image is loaded, you idiot !");
        }
        else
        {
        QString watermarkdata = ui->WatermarkTextEdit->toPlainText();
        int length = watermarkdata.size();
    
        while (length%8!=0)							// for the Blowfish encryption
        {
        length++;
        }
    
        image->convertToFormat(QImage::Format_RGB888);                  // convert to 24-bit image, 8 bits per pixel
    
        numx = image->width();                                          //width in pixels probably
        numy = image->height();                                         //height
    
        if (3*7*(length*8)>3*numx*numy)                               //if more, it doesn't fit according to the key
        {
        QMessageBox msgBox;
        msgBox.setText("Watermark text is\ntoo big for this image.");
        msgBox.exec();
        }
        else
        {
            QString pass = ui->BlowfishLineEdit->text();
            int passlength = pass.size();
    
            char *key = new char[passlength];                       //table holding the bytes of password
    
            key = pass.toAscii().data();
    
            char *bytes = new char[length];                         //table holding the bytes of the watermark message
    
            bytes = watermarkdata.toAscii().data();
    
            CBlowFish blow( reinterpret_cast<unsigned char*>(key), passlength);
    
            blow.Encrypt(reinterpret_cast<unsigned char*>(bytes),length);       // Blowfish encryption
    
    
            int *binary=new int[length*8];			//this will hold the binary table
    
            QString *bin = new QString[256];						//look up table
    
                    bin[0]="00000000";
                    bin[1]="00000001";
                    bin[2]="00000010";
                      ...........
                      ...........
                      ...........
                    bin[253]="11111101";
                    bin[254]="11111110";
                    bin[255]="11111111";
    
            QString temp;				//temporary string
            int a=0;                                //counter
            for (int i=0;i<length;i++)
            {
                temp=bin[bytes[i]];
    
                binary[a]  = temp[0].digitValue();		//MSB
                binary[a+1]= temp[1].digitValue();		// .
                binary[a+2]= temp[2].digitValue();		// .
                binary[a+3]= temp[3].digitValue();		// .
                binary[a+4]= temp[4].digitValue();		// .
                binary[a+5]= temp[5].digitValue();		// .
                binary[a+6]= temp[6].digitValue();		// .
                binary[a+7]= temp[7].digitValue();		//LSB
                a=a+8;
            }
    
    
            for (int h=0;h<a;h++)		//converting from ascii to binary
            binary[h]-=48;
    
    
            int x,y;
            int g=3;
    
            int cnt=0;
            for (int s=0;s<length*8;s++)
            {
            x=3*7*g;						//random number generator
    
                if (x>numx)
                y=(int) (x/numx);
    
            QRgb value = image->pixel(x,y);
            int red = qRed(value);
            int green = qGreen(value);
            int blue = qGreen (value);
            blue = blue & 254;                                  //ANDing pixels with 254->"11111110"
            blue+=binary[cnt];                                  //adding information
            value = qRgb( red, green, blue);
    
            image->setPixel(x, y, value);
    
            cnt++;
            g++;
            }
            delete [] key;
            delete [] bin;
            delete [] bytes;
            delete [] binary;
    
            QString temp2= "Watermarked ";
            QString savefilename = temp2 + filename;
            image->save(savefilename,"BMP",100);
    
            ui->ImagePreviewLabel->setPixmap(QPixmap::fromImage(*image));
            ui->WatermarkTextEdit->setPlainText("Watermarked:\nImage is saved in a Bitmap format");
            delete image;
            }
        }
    
    }

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: No errors and a crash ! Yes I could use some help..

    How does it crash? Have you tried putting a breakpoint on qFatal?

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: No errors and a crash ! Yes I could use some help..

    I'll admit that my C++ was learned in the gutters, but this sequence seems flaky to me:

    Code:
            char *key = new char[passlength];                       //table holding the bytes of password
    
            key = pass.toAscii().data();
    
            char *bytes = new char[length];                         //table holding the bytes of the watermark message
    
            bytes = watermarkdata.toAscii().data();
    The first line allocates a string "passlength" long and stores it in "key", but the very next line then stores whatever "pass.toAscii().data()" returns over the top of the string pointer you just stored. Same thing for "bytes". If nothing else, it's a storage leak (unless the "data" function returns a type which has an assignment operator to char* that uses the existing string -- pretty obscure).

    That binary array conversion stuff is kind going around behind Robin Hood's barn to get things done, but can't seen anything to it that should cause a crash. (Why not just code the "bin" array as octal or hex constants, and skip the conversion entirely, or maybe code them like "0x10110101" and then just compress the hex digits to bits?)

    Most of the rest seems unlikely to cause a crash -- are you sure it's not due to one of the routines you're calling from your code? (Getting a stack traceback would definitely help here.)

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Talking Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by danhicksbyron View Post
    I'll admit that my C++ was learned in the gutters, but this sequence seems flaky to me:

    Code:
            char *key = new char[passlength];                       //table holding the bytes of password
    
            key = pass.toAscii().data();
    
            char *bytes = new char[length];                         //table holding the bytes of the watermark message
    
            bytes = watermarkdata.toAscii().data();
    The first line allocates a string "passlength" long and stores it in "key", but the very next line then stores whatever "pass.toAscii().data()" returns over the top of the string pointer you just stored. Same thing for "bytes". If nothing else, it's a storage leak (unless the "data" function returns a type which has an assignment operator to char* that uses the existing string -- pretty obscure).

    That binary array conversion stuff is kind going around behind Robin Hood's barn to get things done, but can't seen anything to it that should cause a crash. (Why not just code the "bin" array as octal or hex constants, and skip the conversion entirely, or maybe code them like "0x10110101" and then just compress the hex digits to bits?)

    Most of the rest seems unlikely to cause a crash -- are you sure it's not due to one of the routines you're calling from your code? (Getting a stack traceback would definitely help here.)

    Thanks for the reply,
    I'm not a programmer too, and I'm also new to Qt :]

    pass is a QString.
    And
    int passlength = pass.size(); (how many bytes is it)

    also

    QByteArray QString::toAscii () const (http://qt.nokia.com/doc/4.6/qstring.html#toAscii)
    Returns an 8-bit ASCII representation of the string as a QByteArray.
    and

    char * QByteArray::data () (http://qt.nokia.com/doc/4.6/qbytearray.html#data)
    Returns a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is '\0'-terminated, i.e. the number of bytes in the returned character string is size() + 1 for the '\0' terminator
    Do you see anything wrong with it?
    Thanks

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by codestain View Post

    char * QByteArray::data () (http://qt.nokia.com/doc/4.6/qbytearray.html#data)


    Do you see anything wrong with it?
    Thanks
    QByteArray::data () returns pointer to internal data stored in QByteArray and then you call delete on it:
    Code:
    key = pass.toAscii().data();
    bytes = watermarkdata.toAscii().data();
    
    delete [] key;
    delete [] bytes;
    Also note, this forum is devoted to Qt questions, not to general C++ programming.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Re: No errors and a crash ! Yes I could use some help..

    I call delete when the entire process is done..
    Why not delete, it will eat your memory for no use.. But I tried removing those lines, still crashes..

    Also note, this forum is devoted to Qt questions, not to general C++ programming
    Don't you consider questions based on the Qt Class reference, Qt questions?

    Still, I appreciate your help!

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by codestain View Post
    Why not delete, it will eat your memory for no use.. But I tried removing those lines, still crashes..
    Byte array owns that data, you cannot free it. Consider this test application:
    Code:
    #include <QCoreApplication>
    #include <QByteArray>
    #include <QDebug>
    
    int
    main (int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
        QByteArray array("test array");
    
        /* Very stupid! */
        delete [] array.data();
    
        return app.exec();
    }
    Quote Originally Posted by codestain View Post
    Don't you consider questions based on the Qt Class reference, Qt questions?
    If you misuse Qt code do to lack of basic C++ knowledge it doesn't really make it Qt question.

    You can ask on Windows developers' forum how to produce crash backtrace on Windows platform. It's basic knowledge and it will allow you to find every crash reason in 1 minute.

  8. #8
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: No errors and a crash ! Yes I could use some help..

    pass is a QString.
    I missed that detail. Definitely no need to allocate the new arrays.

    I agree with divanov that deleting those strings is at least partly the problem.

    You'd have a MUCH better idea if you could get a traceback -- have you tried it with that breakpoint on qFatal??

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Arrow Re: No errors and a crash ! Yes I could use some help..

    Debugger shows me that it crashes inside this loop, when it goes for the second round:


    Code:
            for (int i=0;i<length;i++)
            {
                temp=bin[bytes[i]];
    
                binary[a]  = temp[0].digitValue();		//MSB
                binary[a+1]= temp[1].digitValue();		// .
                binary[a+2]= temp[2].digitValue();		// .
                binary[a+3]= temp[3].digitValue();		// .
                binary[a+4]= temp[4].digitValue();		// .
                binary[a+5]= temp[5].digitValue();		// .
                binary[a+6]= temp[6].digitValue();		// .
                binary[a+7]= temp[7].digitValue();		//LSB
                a=a+8;
            }
    The inferior stopped because it received a signal from the Operating System.

    Signal name:SIGSEGV
    Signal meaning: Segmentation fault

  10. #10
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by codestain View Post
    Debugger shows me that it crashes inside this loop, when it goes for the second round:
    Code:
            for (int i=0;i<length;i++)
            {
                temp=bin[bytes[i]];
    
                binary[a]  = temp[0].digitValue();		//MSB
                binary[a+1]= temp[1].digitValue();		// .
                binary[a+2]= temp[2].digitValue();		// .
                binary[a+3]= temp[3].digitValue();		// .
                binary[a+4]= temp[4].digitValue();		// .
                binary[a+5]= temp[5].digitValue();		// .
                binary[a+6]= temp[6].digitValue();		// .
                binary[a+7]= temp[7].digitValue();		//LSB
                a=a+8;
            }
    "bytes" is actually internal array of bytes belonging to watermark. "length" is length of "watermark" string ceiled to closest number proportional to 8. Thus, "i" will go out of "bytes" every time your string length is not proportional to 8.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by divanov View Post
    "bytes" is actually internal array of bytes belonging to watermark. "length" is length of "watermark" string ceiled to closest number proportional to 8. Thus, "i" will go out of "bytes" every time your string length is not proportional to 8.
    bytes has size of length, and length is always multiple of 8. bytes will have some empty slots if watermarkdata is not multiple of 8, since i increase length till it's multiple of 8.

  12. #12
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by codestain View Post
    bytes has size of length, and length is always multiple of 8. bytes will have some empty slots if watermarkdata is not multiple of 8, since i increase length till it's multiple of 8.
    As I said already, bytes belongs to QByteArray and any manipulations with length doesn't have any effect to it.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Re: No errors and a crash ! Yes I could use some help..

    This code-logic actually works with MFC classes. I have no idea why it doesn't here..

  14. #14
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by codestain View Post
    This code-logic actually works with MFC classes. I have no idea why it doesn't here..
    Actually not:
    http://www.augustcouncil.com/~tgibson/tutorial/arr.html
    Last edited by divanov; 2010-03-04 at 19:43.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    Re: No errors and a crash ! Yes I could use some help..

    Quote Originally Posted by divanov View Post
    Actually not.
    Trust me it works..

    If you have an idea just post.

    Yes. I'm not a programmer.
    Last edited by codestain; 2010-03-04 at 20:15.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×