Results 1 to 6 of 6

Thread: Need some help with QImage::setPixel and QImage::width()

  1. #1
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need some help with QImage::setPixel and QImage::width()

    Hello, my program crashes at QImage::setPixel which calls QImage::width(), it crashes at QImage::width(), my backtrace:
    Qt Code:
    1. (gdb) bt
    2. #0 0x00ab9b9b in QImage::width (this=0x22cc94) at image\qimage.cpp:1555
    3. #1 0x00ac01ac in QImage::setPixel (this=0x22cc94, x=18, y=29,
    4. index_or_rgb=2280604) at image\qimage.cpp:4118
    5. #2 0x00404500 in MainWindow::putPixel (this=0x22cd60, image=@0x22cc94, x=18,
    6. y=29, red=34 '"', green=0 '\0', blue=248 '°', alpha=255 '*')
    7. at ..\OtlandMapEditor\mainwindow.cpp:441
    8. #3 0x00403e8a in MainWindow::loadItems (this=0x22cd60)
    9. at ..\OtlandMapEditor\mainwindow.cpp:368
    10. #4 0x00402b98 in MainWindow::onNew (this=0xa5f9418)
    11. at ..\OtlandMapEditor\mainwindow.cpp:154
    12. #5 0x00411fe6 in MainWindow::qt_metacall (this=0xa5f9418,
    13. _c=QMetaObject::InvokeMetaMethod, _id=0, _a=0x22ce78)
    14. at debug\moc_mainwindow.cpp:99
    15. #6 0x6a205fb8 in QMetaObject::metacall (object=0xa5f9418,
    16. cl=QMetaObject::InvokeMetaMethod, idx=31, argv=0x22ce78)
    17. at kernel\qmetaobject.cpp:237
    18. #7 0x6a216770 in QMetaObject::activate (sender=0xa5fa0c8, m=0x1256640,
    19. local_signal_index=1, argv=0x22ce78) at kernel\qobject.cpp:3278
    20. #8 0x00a1f630 in QAction::triggered (this=0x22ce90, _t1=false)
    21. at tmp/moc/debug_shared/moc_qaction.cpp:263
    22. #9 0x0022ce8c in ?? ()
    23. #10 0x0022ce90 in ?? ()
    24. #11 0x0022ce94 in ?? ()
    25. #12 0x0022ce98 in ?? ()
    26. #13 0x0022ce9c in ?? ()
    27. #14 0x0022cea0 in ?? ()
    28. #15 0x0022cea4 in ?? ()
    29. #16 0x0022cea8 in ?? ()
    30. #17 0x0022ceac in ?? ()
    31. #18 0x0022ceb0 in ?? ()
    32. #19 0x0022ceb4 in ?? ()
    33. #20 0x0022ceb8 in ?? ()
    34. #21 0x0022cebc in ?? ()
    35. #22 0x0022cec0 in ?? ()
    36. #23 0x0022cec4 in ?? ()
    37. #24 0x0022cec8 in ?? ()
    38. #25 0x0022cecc in ?? ()
    39. #26 0x0022ced0 in ?? ()
    40. #27 0x0022ced4 in ?? ()
    41. #28 0x0022ced8 in ?? ()
    42. #29 0x0022cedc in ?? ()
    43. #30 0x0022cee0 in ?? ()
    44. #31 0x0022cee4 in ?? ()
    45. #32 0x0022cee8 in ?? ()
    46. #33 0x0022ceec in ?? ()
    47. #34 0x0022cef0 in ?? ()
    48. #35 0x0022cef4 in ?? ()
    49. #36 0x0b833648 in ?? ()
    50. #37 0x0022d6ec in ?? ()
    51. #38 0x0022d6ec in ?? ()
    52. #39 0x00000001 in ?? ()
    53. #40 0x00e36a77 in QMenuPrivate::activateAction (this=0xb834748,
    54. action=0xa5fa0c8, action_e=QAction::Trigger, self=true)
    55. at widgets\qmenu.cpp:1085
    56. #41 0x00e3ab53 in QMenu::mouseReleaseEvent (this=0xb834718, e=0x22d6d4)
    57. at widgets\qmenu.cpp:2306
    58. #42 0x00a76a52 in QWidget::event (this=0xb834718, event=0x22d6d4)
    59. at kernel\qwidget.cpp:8259
    60. #43 0x00e3b1ab in QMenu::event (this=0xb834718, e=0x22d6d4)
    61. at widgets\qmenu.cpp:2415
    62. #44 0x00a2bd96 in QApplicationPrivate::notify_helper (this=0xa5f4098,
    63. receiver=0xb834718, e=0x22d6d4) at kernel\qapplication.cpp:4462
    64. #45 0x00a29fdf in QApplication::notify (this=0x22fe2c, receiver=0xb834718,
    65. e=0x22d6d4) at kernel\qapplication.cpp:4023
    66. #46 0x6a2014fe in QCoreApplication::notifyInternal (this=0x22fe2c,
    67. receiver=0xb834718, event=0x22d6d4) at kernel\qcoreapplication.cpp:731
    68. #47 0x010a5ce2 in QCoreApplication::sendSpontaneousEvent (receiver=0xb834718,
    69. event=0x22d6d4)
    70. at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:218
    71. #48 0x00a287ee in QApplicationPrivate::sendMouseEvent (receiver=0xb834718,
    72. event=0x22d6d4, alienWidget=0x0, nativeWidget=0xb834718,
    73. buttonDown=0x14eb41c, lastMouseReceiver=@0x14eb420, spontaneous=true)
    74. at kernel\qapplication.cpp:3120
    75. #49 0x00a9304b in QETWidget::translateMouseEvent (this=0xb834718,
    76. msg=@0x22dba8) at kernel\qapplication_win.cpp:3234
    77. #50 0x00a8e3e2 in QtWndProc@16 (hwnd=0x161638, message=514, wParam=0,
    78. lParam=589864) at kernel\qapplication_win.cpp:1659
    79. #51 0x775186ef in USER32!IsWindowVisible ()
    80. from C:\Windows\system32\user32.dll
    81. #52 0x00161638 in ?? ()
    82. #53 0x00000202 in ?? ()
    83. #54 0x77518876 in USER32!IsWindowVisible ()
    84. from C:\Windows\system32\user32.dll
    85. #55 0x00a8d8e1 in qt_is_translatable_mouse_event (message=0)
    86. at kernel\qapplication_win.cpp:1426
    87. #56 0x775189b5 in USER32!IsWindowVisible ()
    88. from C:\Windows\system32\user32.dll
    89. #57 0x00000000 in ?? ()
    90. (gdb)
    To copy to clipboard, switch view to plain text mode 

    my code:
    Qt Code:
    1. void MainWindow::loadItems()
    2. {
    3. if (m_loader)
    4. delete m_loader;
    5.  
    6. m_loader = new XLoader(this);
    7.  
    8. QString error = "Cannot load items.xml: ";
    9. switch (m_loader->loadItems("items.xml")) {
    10. case ERR_FILE_D_EXIST:
    11. error += "File does not exist";
    12. break;
    13. case ERR_CANNOT_OPEN:
    14. error += "Cannot open file";
    15. break;
    16. case ERR_MALFORMED_FILE:
    17. error += "Malformed XML File";
    18. break;
    19. default:
    20. case ERR_SUCCESS:
    21. error = "";
    22. break;
    23. }
    24.  
    25. if (!error.isEmpty()) {
    26. QMessageBox::information(this, tr("Fatal error"), tr("The following errors occured: %1").arg(error));
    27. return;
    28. }
    29.  
    30. QListWidget *listWidget = findChild<QListWidget *>("listWidget");
    31. for (ItemsSprites::ItemMap::iterator it = g_itemSprites->begin(); it != g_itemSprites->end(); ++it) {
    32. SpriteType *sType = it->second;
    33. if (sType) {
    34. ItemRet *itemRet = m_loader->item(it->first);
    35. unsigned short spriteId = g_itemSprites->getItemSpriteID(it->first, 0);
    36. ItemType *iType = g_itemTypes->getType(it->first);
    37. if (iType && iType->group == ITEM_GROUP_DEPRECATED) {
    38. qDebug() << "Found item but it's deprected.";
    39. continue;
    40. }
    41.  
    42. if (spriteId <= 1) {
    43. qDebug() << "Image Not Found";
    44. continue;
    45. }
    46.  
    47. char dump[4000];
    48. unsigned short size = g_itemSprites->getSpriteDump(spriteId, dump);
    49. if (size < 1) {
    50. qDebug() << "Image Not Found #2";
    51. continue;
    52. }
    53.  
    54. /// \todo better progress handling...
    55. if ((rand() % 2) == 1)
    56. incrementProgressBar();
    57.  
    58. qDebug() << "Everyting ok...";
    59. g_itemSprites->getSpriteDump(sType->imageID[1], dump);
    60. QImage image((uchar *)dump, 34, 34, QImage::Format_ARGB32_Premultiplied);
    61. if (!image.isNull()) {
    62. for (int n = 0; n < 33; ++n) {
    63. putPixel(image, n, 0, 0xFF, 0x00, 0x00, 0xFF);
    64. putPixel(image, n, 33, 0xFF, 0x00, 0x00, 0xFF);
    65. }
    66.  
    67. for (int p = 1; p < 33; ++p) {
    68. putPixel(image, 0, p, 0xFF, 0x00, 0x00, 0xFF);
    69. putPixel(image, 33, p, 0xFF, 0x00, 0x00, 0xFF);
    70. }
    71.  
    72. unsigned short x, y, npix;
    73. int state;
    74. int i,j;
    75. int posx=1, posy=1;
    76.  
    77. state=0;
    78. x=0;
    79. y=0;
    80. for (i=0; i < size;) {
    81. memcpy(&npix, dump+i, 2); // number of pixels (transparent or defined)
    82. i += 2;
    83. switch (state) {
    84. default: // state 0, drawing transparent pixels
    85. for (j=0; j < npix; ++j) {
    86. putPixel(image, posx+x, posy+y, 0x00, 0x00, 0x00, 0x00);
    87. if (x < 31) {
    88. x++;
    89. } else {
    90. x=0;
    91. y++;
    92. }
    93. }
    94. state=1;
    95. break;
    96. case 1: // state 1, drawing defined pixels
    97. for (j=0; j < npix; ++j) {
    98. putPixel(image, posx+x, posy+y, dump[i], dump[i+1], dump[i+2], 0xFF);
    99. i += 3;
    100. if (x < 31) {
    101. x++;
    102. } else {
    103. x=0;
    104. y++;
    105. }
    106. }
    107. state=0;
    108. break;
    109. }
    110. }
    111.  
    112. while (x < 32 && y < 32) {
    113. putPixel(image, posx+x, posy+y, 0x00, 0x00, 0x00, 0x00);
    114. if (x < 31) {
    115. x++;
    116. } else {
    117. x=0;
    118. y++;
    119. }
    120. }
    121.  
    122. QListWidgetItem *widgetItem = new QListWidgetItem;
    123. widgetItem->setIcon(QIcon(QPixmap::fromImage(image)));
    124. if (itemRet) {
    125. widgetItem->setText(itemRet->name);
    126. delete itemRet;
    127. } else {
    128. widgetItem->setText("Unknown");
    129. }
    130.  
    131. listWidget->addItem(widgetItem);
    132. qDebug() << "OK";
    133. ItemData *itemData = new ItemData;
    134. itemData->item = widgetItem;
    135. itemData->pixmap = QPixmap::fromImage(image);
    136. m_items.append(itemData);
    137. }
    138. }
    139. }
    140. }
    141.  
    142. void MainWindow::putPixel(QImage& image, int x, int y, uchar red, uchar green, uchar blue, uchar alpha)
    143. {
    144. if (image.isNull() || !image.valid(x, y))
    145. return;
    146.  
    147. int width, height, rowstride, n_channels;
    148. uchar *pixels, *p;
    149.  
    150. n_channels = image.depth() / 8;
    151.  
    152. Q_ASSERT(image.hasAlphaChannel());
    153. Q_ASSERT(n_channels == 4);
    154.  
    155. width = image.width();
    156. height = image.height();
    157.  
    158. Q_ASSERT(x >= 0 && x < width);
    159. qDebug() << "y = " << y << ", height = " << height << ".";
    160. Q_ASSERT(y >= 0 && y < height);
    161.  
    162. rowstride = image.bytesPerLine();
    163. pixels = image.bits();
    164.  
    165. p = pixels + y * rowstride + x * n_channels;
    166. p[0] = red;
    167. p[1] = green;
    168. p[2] = blue;
    169. p[3] = alpha;
    170.  
    171. image.setPixel(x, y, QRgb(p));
    172. }
    To copy to clipboard, switch view to plain text mode 

    it crashes here:
    Qt Code:
    1. for (j=0; j < npix; ++j) {
    2. putPixel(image, posx+x, posy+y, dump[i], dump[i+1], dump[i+2], 0xFF);
    To copy to clipboard, switch view to plain text mode 
    then in here:
    Qt Code:
    1. image.setPixel(x, y, QRgb(p));
    To copy to clipboard, switch view to plain text mode 

    any ideas?
    Last edited by Fallen_; 15th May 2011 at 19:41.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Need some help with QImage::setPixel and QImage::width()

    Don't know if this is your problem or not, but my Linux box won't compile this line:
    Qt Code:
    1. image.setPixel(x, y, QRgb(p));
    To copy to clipboard, switch view to plain text mode 
    The compiler spits out the following:
    error: cast from ‘uchar*’ to ‘QRgb’ loses precision
    and it looks like you have some garbage in line #2 of your backtrace.

    Try this:
    Qt Code:
    1. image.setPixel(x,y,*(reinterpret_cast<QRgb *>(p)));
    To copy to clipboard, switch view to plain text mode 
    or the Qt way:
    Qt Code:
    1. image.setPixel(x, y, qRgba(int(red),int(green),int(blue),int(alpha)));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need some help with QImage::setPixel and QImage::width()

    Quote Originally Posted by norobro View Post
    Don't know if this is your problem or not, but my Linux box won't compile this line:
    Qt Code:
    1. image.setPixel(x, y, QRgb(p));
    To copy to clipboard, switch view to plain text mode 
    The compiler spits out the following: and it looks like you have some garbage in line #2 of your backtrace.

    Try this:
    Qt Code:
    1. image.setPixel(x,y,*(reinterpret_cast<QRgb *>(p)));
    To copy to clipboard, switch view to plain text mode 
    or the Qt way:
    Qt Code:
    1. image.setPixel(x, y, qRgba(int(red),int(green),int(blue),int(alpha)));
    To copy to clipboard, switch view to plain text mode 
    still the same <,<

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Need some help with QImage::setPixel and QImage::width()

    Well, your putPixel() function works fine here:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. void putPixel(QImage& image, int x, int y, uchar red, uchar green, uchar blue, uchar alpha)
    4. {
    5. if (image.isNull() || !image.valid(x, y)) return;
    6. int width, height, rowstride, n_channels;
    7. uchar *pixels, *p;
    8. n_channels = image.depth() / 8;
    9. Q_ASSERT(image.hasAlphaChannel());
    10. Q_ASSERT(n_channels == 4);
    11. width = image.width();
    12. height = image.height();
    13. Q_ASSERT(x >= 0 && x < width);
    14. // qDebug() << "y = " << y << ", height = " << height << ".";
    15. Q_ASSERT(y >= 0 && y < height);
    16. rowstride = image.bytesPerLine();
    17. pixels = image.bits();
    18. p = pixels + y * rowstride + x * n_channels;
    19. p[0] = alpha;
    20. p[1] = red;
    21. p[2] = green;
    22. p[3] = blue;
    23. image.setPixel(x, y, *reinterpret_cast<QRgb *>(p));
    24. // image.setPixel(x, y, qRgba(int(red),int(green),int(blue),int(alpha)));
    25. }
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication a(argc, argv);
    30.  
    31. QLabel *label = new QLabel;
    32. QPixmap pix(290,290);
    33. QPainter painter(&pix);
    34. painter.setBrush(QBrush(QColor(102,205,170)));
    35. painter.setFont(QFont("Arial",75));
    36. painter.drawRect(0,0,290,290);
    37. painter.setPen(QColor(34,139,34));
    38. painter.drawText(QRect(0,20,290,290), Qt::AlignCenter, "Qt");
    39. QImage image(pix.toImage());
    40. for(int x=25;x<50;++x)
    41. for(int y=25;y<50;++y)
    42. putPixel(image, x, y, 0x22, 0x00, 0xf8, 0xff);
    43. label->setPixmap(QPixmap::fromImage(image));
    44. label->show();
    45.  
    46. return a.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need some help with QImage::setPixel and QImage::width()

    Its still the same using your code:
    Qt Code:
    1. image.setPixel(x, y, *reinterpret_cast<QRgb *>(p));
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need some help with QImage::setPixel and QImage::width()

    Are you using threads? Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. prob on setpixel in Qimage ?
    By Askar in forum Qt Programming
    Replies: 1
    Last Post: 3rd March 2010, 06:23
  2. QImage setPixel does not set alpha value
    By sanjayshelke in forum Qt Programming
    Replies: 7
    Last Post: 20th February 2009, 10:15
  3. [SOLVED] QImage setPixel alpha issue
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2008, 15:06
  4. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 17:11
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.