Results 1 to 2 of 2

Thread: Image contrast

  1. #1
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Image contrast

    Hello,

    i must implement a brightness/contrast filter.

    So far I've managed to brighten an image as follows :

    Qt Code:
    1. QImage ConvolutionFilter:: brighten(QImage& source, int factor)
    2. {
    3. if (factor < -255 || factor > 255) return source;
    4. int red, green, blue;
    5.  
    6. /* for ( int y=0; y<source.height(); y++ ) {
    7.   uchar *p = source.scanLine(y);
    8.   for (int x = 0; x < source.width(); x ++) {
    9. red = int((*p++) + factor);
    10. red = (red < 0x00) ? 0x00 : (red > 0xff) ? 0xff : red;
    11.   green = int((*p++)+ factor);
    12. green = (green < 0x00) ? 0x00 : (green > 0xff) ? 0xff : green;
    13.   blue = int((*p++) + factor);
    14. blue = (blue < 0x00) ? 0x00 : (blue > 0xff) ? 0xff : blue ;
    15.   int alpha = int(*p++);
    16.   QRgb rgbaValue = qRgba(red,green,blue, alpha);
    17. source.setPixel(x,y, rgbaValue);
    18. }
    19. }*/
    20.  
    21. int pixels = source.width() * source.height();
    22. unsigned int *data = (unsigned int *)source.bits();
    23. for (int i = 0; i < pixels; ++i) {
    24. red= qRed(data[i])+ factor;
    25. red = (red < 0x00) ? 0x00 : (red > 0xff) ? 0xff : red;
    26. green= qGreen(data[i])+factor;
    27. green = (green < 0x00) ? 0x00 : (green > 0xff) ? 0xff : green;
    28. blue= qBlue(data[i])+factor;
    29. blue = (blue < 0x00) ? 0x00 : (blue > 0xff) ? 0xff : blue ;
    30. data[i] = qRgba(red, green, blue, qAlpha(data[i]));
    31. }
    32. return source;
    33. }
    To copy to clipboard, switch view to plain text mode 

    But I'm totally stuck with contrast.

    I would deeply appreciate if someone could give me some hints how to handle this task.

    Regards.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Image contrast

    for those interested i came up with this code, the contrast can be applied recursively :

    Qt Code:
    1. QImage ConvolutionFilter::contrast(QImage& source, int factor)
    2. {
    3. //contrast :new color = 128 + Contrast *( old color - 128)
    4. if (factor < 0) return source;
    5. if (factor > 20) return source;
    6. double contrast = (100.0+factor)/100.0;
    7. double red, green, blue;
    8. int pixels = source.width() * source.height();
    9. unsigned int *data = (unsigned int *)source.bits();
    10. for (int i = 0; i < pixels; ++i) {
    11. red= 128+ contrast*(qRed(data[i])-128);
    12. red = (red < 0x00) ? 0x00 : (red > 0xff) ? 0xff : red;
    13. green= 128+ contrast*(qGreen(data[i])-128);
    14. green = (green < 0x00) ? 0x00 : (green > 0xff) ? 0xff : green;
    15. blue= 128+ contrast*(qBlue(data[i])-128);
    16. blue = (blue < 0x00) ? 0x00 : (blue > 0xff) ? 0xff : blue ;
    17. data[i] = qRgba(red, green, blue, qAlpha(data[i]));
    18. }
    19. return source;
    20. }
    To copy to clipboard, switch view to plain text mode 


    Comments and improvements are welcome ...

Similar Threads

  1. Image Commander v.1.2
    By piotrek in forum Qt-based Software
    Replies: 0
    Last Post: 20th April 2009, 10:59
  2. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.