Results 1 to 10 of 10

Thread: QImage and "raw" grey scale

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QImage and "raw" grey scale

    Hi,

    with Qt3 I have done this a lot, and now I need to convert and display 16bit gray scale.
    But what ever I tried so far either gives me a uniform gray image (even if the buffer is empty) or no image at all.
    I am a bit lost, maybe one of you can see what it is I am missing?
    Efficiency is not an issue.
    I just want to see the image.
    The buffer is checked, and is valid, with a valid image (saved to file through grabbing lib). plus, I see in the debugger the contents.
    I never get a black image even if the buffer is all full with '0'.
    After setting the data in to the image, my QImage object returns false on isNull().
    So I am quite sure my problem is format issue, not data issue.

    Here is my code :
    Qt Code:
    1. QImage convert16to8mono(unsigned short *inBuff, int sizeBytes, unsigned char *outBuff)
    2. {
    3. //convert 16 to 8 bit
    4. for(int i=0; i<sizeBytes/2; i++)
    5. outBuff[i] = (((double)inBuff[i]) / 0xFFFF) * 255;
    6.  
    7. QImage qimage(1280,960,QImage::Format_Indexed8);
    8. qimage.setNumColors(256);
    9. for(int i=0; i<256; i++)
    10. qimage.setColor(i,qRgb(i,i,i));
    11.  
    12. //trail 1
    13. //qimage.loadFromData(outBuff,sizeBytes/2,0/*BMP*/); //this is raw data, so there are no format specific headers etc.
    14. //trail 2
    15. memcpy(qimage.bits(),outBuff,sizeBytes/2);
    16.  
    17. //trail 3 :
    18. // for(int y=0,i=0;y<960; y++)
    19. // for(int x=0; x<1280; x++,i++)
    20. // qimage.setPixel(x,y,outBuff[i]);
    21.  
    22. return qimage;
    23. }
    To copy to clipboard, switch view to plain text mode 

    P.S
    the contents of outBuff is also checked, and it has plausible values.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: QImage and "raw" grey scale

    What is the structure of data in outBuf? Values 0-255 representing pixel colours? Could you check the exact values there? Are they different than 0?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QImage and "raw" grey scale

    Hi Witek,

    yes, outBuff are values 0-255.
    And as I said, I checked them, and they are not 0 (well some are, but its not a null buffer).

    However, I tried this with out a camera, then the original buffer and outBuff contain only 0 and I expected a black image.
    Depending on which variation I am using in the code above, I either get a uniform gray image (even with a buffer full with zeros) or nothing.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QImage and "raw" grey scale

    If the image comming from a camera device create raw data image:

    1- bool RawImage::create( int width, int height, int channels )
    2- write your buffer RawImage::QByteArray data....
    3- at end QImage convertToQImage(bool cmyk = false , bool raw = false);

    ( idea from www.scribus.net code )

    Qt Code:
    1. ///// header
    2. #ifndef RAWIMAGE_H
    3. #define RAWIMAGE_H
    4.  
    5. #include <QtGui>
    6.  
    7. class RawImage : public QByteArray
    8. {
    9. public:
    10. RawImage();
    11. RawImage( int width, int height, int channels);
    12. ~RawImage();
    13. bool create( int width, int height, int channels);
    14. int width() const {
    15. return m_width;
    16. };
    17. int height() const {
    18. return m_height;
    19. };
    20. int channels() const {
    21. return m_channels;
    22. };
    23. uchar *bits() {
    24. return (uchar*)data();
    25. };
    26. uchar *scanLine(int row);
    27. void setAlpha(int x, int y, int alpha);
    28. QImage convertToQImage(bool cmyk = false , bool raw = false);
    29. private:
    30. int m_width;
    31. int m_height;
    32. int m_channels;
    33. };
    34.  
    35.  
    36. #endif
    37. ///// source
    38.  
    39. #include "rawimage.h"
    40.  
    41.  
    42. RawImage::RawImage()
    43. {
    44. m_width = 0;
    45. m_height = 0;
    46. m_channels = 0;
    47. resize(0);
    48. }
    49.  
    50. RawImage::RawImage( int width, int height, int channels )
    51. {
    52. create(width, height, channels);
    53. }
    54.  
    55. RawImage::~RawImage()
    56. {
    57. resize(0);
    58. }
    59.  
    60. bool RawImage::create( int width, int height, int channels )
    61. {
    62. m_width = width;
    63. m_height = height;
    64. m_channels = channels;
    65. int finalSize=width * height * channels;
    66. resize(finalSize);
    67. return (size()==finalSize);
    68. }
    69.  
    70. uchar *RawImage::scanLine(int row)
    71. {
    72. if (row < m_height)
    73. return (uchar*)(data() + (row * m_channels * m_width));
    74. else
    75. return (uchar*)data();
    76. }
    77.  
    78. void RawImage::setAlpha(int x, int y, int alpha)
    79. {
    80. uchar *d;
    81. if ((y < m_height) && (x < m_width))
    82. {
    83. d = (uchar*)(data() + (y * m_channels * m_width) + (x * m_channels));
    84. d[m_channels-1] = alpha;
    85. }
    86. }
    87.  
    88.  
    89. QImage RawImage::convertToQImage(bool cmyk, bool raw)
    90. {
    91. int chans = channels();
    92. QImage img = QImage(width(), height(), QImage::Format_ARGB32);
    93. QRgb *ptr;
    94. uchar *src;
    95. uchar cr, cg, cb, ck, ca;
    96. // img.create(width(), height(), 32);
    97. if (raw)
    98. {
    99. for (int i = 0; i < height(); i++)
    100. {
    101. ptr = (QRgb *)img.scanLine(i);
    102. src = scanLine(i);
    103. for (int j = 0; j < width(); j++)
    104. {
    105. *ptr++ = qRgba(src[0],src[1],src[2],src[3]);
    106. src += chans;
    107. }
    108. qDebug() << "## raw line -> " << i;
    109. }
    110. }
    111. else
    112. {
    113. // img.setAlphaBuffer( true );
    114. for (int i = 0; i < height(); i++)
    115. {
    116. ptr = (QRgb *)img.scanLine(i);
    117. src = scanLine(i);
    118. for (int j = 0; j < width(); j++)
    119. {
    120. if (chans > 1)
    121. {
    122. if (cmyk)
    123. {
    124. ck = src[3];
    125. cr = 255 - qMin(255, src[0] + ck);
    126. cg = 255 - qMin(255, src[1] + ck);
    127. cb = 255 - qMin(255, src[2] + ck);
    128. if (chans > 4)
    129. {
    130. ca = src[4];
    131. *ptr++ = qRgba(cr,cg,cb,ca);
    132. }
    133. else
    134. *ptr++ = qRgba(cr,cg,cb,255);
    135. }
    136. else
    137. {
    138. if (chans > 3)
    139. *ptr++ = qRgba(src[0],src[1],src[2],src[3]);
    140. else
    141. *ptr++ = qRgba(src[0],src[1],src[2],255);
    142. }
    143. }
    144. else
    145. *ptr++ = qRgba(src[0],src[0],src[0],255);
    146. src += chans;
    147. }
    148.  
    149. //////qDebug() << "## n line -> " << i;
    150.  
    151. }
    152. }
    153. return img;
    154. }
    To copy to clipboard, switch view to plain text mode 

    wath is your source data tiff?

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

    Default Re: QImage and "raw" grey scale

    The code looks fine... What happens if you use setPixel() to set data not from your buffer but some arbitrary one? Try drawing a red line or something across the image. My impression is that you can be having two copies of the image and you change not the one you are supposed to but the code you pasted doesn't prove that.

    Is the size of the destination image correct? Could you check after memcpy() if pixel() returns expected values?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QImage and "raw" grey scale

    Hi Witek,

    I think you are on the correct path.
    I tried the following:
    Instead of allocating the QImage in my function above, I allocate it there where I use it, and give it as a parameter to the conversion function.
    In addition, I use the setPixel() loop to fill it with white (index 255).
    (so not even using my buffer)

    Still same results - meaning - a uniform gray image. (even if I use (only) red in the color table.)

    Do you see something wrong in my use of QImage:
    Qt Code:
    1. QPixmap pixmap(1280,960);
    2. QImage qimage(1280,960,QImage::Format_Indexed8);
    3. convert16to8mono(qimage,buff,size*2,buff8);
    4. if(!qimage.isNull())
    5. pixmap.fromImage(qimage);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QImage and "raw" grey scale

    @patrik08:
    Thanks for the suggestion.
    But I am just interested to know why my code doesn't work.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QImage and "raw" grey scale

    So, after simplifying even more this is my code:
    Qt Code:
    1. void convert16to8mono(QImage &qimage,unsigned short *inBuff, int sizeBytes, unsigned char *outBuff)
    2. {
    3. qimage.setNumColors(1);
    4. qimage.setColor(0,qRgb(255,0,0));
    5.  
    6. for(int y=0,i=0;y<960; y++)
    7. for(int x=0; x<1280; x++,i++)
    8. {
    9. qimage.setPixel(x,y,0);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    and I use it like that:
    Qt Code:
    1. QPixmap pixmap(1280,960);
    2. QImage qimage(1280,960,QImage::Format_Indexed8);
    3. convert16to8mono(qimage,buff,size*2,buff8);
    4. if(!qimage.isNull())
    5. pixmap.fromImage(qimage);
    6.  
    7. ui.lblImage->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    result is a uniform gray image, instead of a red image....

    I am at a loss to what I still might be doing wrong...

    EDIT:
    I solved it.
    As always the problem ist between the chair and keyboard.
    I forgot that fromImage returns the new pixmap, and not changing the calling object.

    Thanks for helping.
    Last edited by high_flyer; 9th December 2008 at 17:00.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: QImage and "raw" grey scale

    Hi Dani,

    Ah... I think I see the problem. QPixmap::fromImage() is a static method returning a pixmap, thus it should be:
    Qt Code:
    1. QPixmap px = QPixmap::fromImage(qimage);
    To copy to clipboard, switch view to plain text mode 

    So the problem is not with the image, but with the pixmap you try to convert it to.

  10. The following user says thank you to wysota for this useful post:

    high_flyer (9th December 2008)

  11. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QImage and "raw" grey scale

    hehe - I just saw it by my self as you where posting your answer

    Thanks!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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.