Results 1 to 10 of 10

Thread: Posterizes an image with results identical to Gimp's Posterize command

  1. #1
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Posterizes an image with results identical to Gimp's Posterize command

    How can i posterizes an image with results identical to Gimp's Posterize command? i've written the following code which make posterize with value 5, but it doesn't work correctly, cause the result image is not an identical to gimp's posterize tool at 5 value. it differs a little when i increase scale to 400% and look at result image and the image after gimp's posterize... any idea?

    Qt Code:
    1. for(int x=0; x<image.width(); x++)
    2. for(int y=0; y<image.height(); y++)
    3. result.setPixel(x, y, 51+51*(int)(qGray(image.pixel(x, y))/51));
    To copy to clipboard, switch view to plain text mode 


    PS: Posterize - it's reduce number of colors (is available, for ex., in GIMP at "Colors -> Posterize...")
    Last edited by suseway; 28th November 2010 at 11:23.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    Are you using exact same algorithm as GIMP ?

  3. #3
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    I don't know, which algorythm use gimp, but i want make 100% effect as gimp does

  4. #4
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    gimp is open source...
    http://www.gimp.org/source/#source

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    There are dozens of ways to reduce colors in an image - simple decimation; color map reindexing; median cut algorithm and many others, many of which also operate over a variety of color spaces.

    Unless you know precisely which algorithm the Gimp uses, along with how it is implemented, the only way to make your results "100% effect as gimp does" is to use the Gimp through scripting and a system call. And even that may not be identical across versions of the Gimp. Or you could examine their source code - keeping in mind that reuse of someone else's code opens the door to a complex of legal issues - but even this won't guarantee that your results will be the same across past or future versions of the Gimp.

    Why do you care that the results match exactly?

  6. #6
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    I had looked at gimp sources and found in gimpoperationposterize.c:

    Qt Code:
    1. static gboolean
    2. gimp_operation_posterize_process (GeglOperation *operation,
    3. void *in_buf,
    4. void *out_buf,
    5. glong samples,
    6. const GeglRectangle *roi)
    7. {
    8. GimpOperationPointFilter *point = GIMP_OPERATION_POINT_FILTER (operation);
    9. GimpPosterizeConfig *config = GIMP_POSTERIZE_CONFIG (point->config);
    10. gfloat *src = in_buf;
    11. gfloat *dest = out_buf;
    12. gfloat levels;
    13.  
    14. if (! config)
    15. return FALSE;
    16.  
    17. levels = config->levels - 1.0;
    18.  
    19. while (samples--)
    20. {
    21. dest[RED_PIX] = RINT (src[RED_PIX] * levels) / levels;
    22. dest[GREEN_PIX] = RINT (src[GREEN_PIX] * levels) / levels;
    23. dest[BLUE_PIX] = RINT (src[BLUE_PIX] * levels) / levels;
    24. dest[ALPHA_PIX] = src[ALPHA_PIX];
    25.  
    26. src += 4;
    27. dest += 4;
    28. }
    29.  
    30. return TRUE;
    31. }
    To copy to clipboard, switch view to plain text mode 


    Seems that the code do this posterize operation... but i don't know yet what exactly this code does and how can i rewrite this for Qt..

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    The code is dirt simple. Each pixel is stored as a quadruple value of red, green, blue and alpha, each represented as a float ranging from 0 to 1. The alpha channel is simply copied. The other channels are multiplied by the number of levels desired, converted to an integer by rounding, multiplied by the number of levels and stored in the corresponding output pixel channel. It's plain-vanilla C code, and should be trivial to convert to Qt image operations.

    Again, however, there is no guarantee that this algorithm won't change in subsequent Gimp releases, or that is matches what has been implemented in the past.

  8. #8
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    Hello. I'm trying to implement this algorythm to Qt image operations, but it's create new original image (instead of posterized image).
    Here is my code:
    Qt Code:
    1. int r, g, b, a;
    2. QRgb val;
    3. double levels = 5.0; // posterize value 5.0
    4. levels -= 1.0; // by following gimp code
    5. int newRed, newGreen, newBlue;
    6. // starting loop
    7. for(int x=0; y<img.width(); x++)
    8. for(int y=0; y<img.height(); y++) {
    9. val = img.pixel(x, y);
    10. r = qRed(val);
    11. g = qGreen(val);
    12. b = qBlue(val);
    13. a = qAlpha(val);
    14.  
    15. qDebug() << "r = " << r;
    16. qDebug() << "g = " << g;
    17. qDebug() << "b = " << b;
    18. // lines 21-23 from GIMP...
    19. newRed = (rint) ( (double)r / 255.0 * levels) / levels;
    20. newGreen = (rint) ( (double)g / 255.0 * levels) / levels;
    21. newBlue = (rint) ( (double)b / 255.0 * levels) / levels;
    22.  
    23. qDebug() << "newRed = " << newRed;
    24. qDebug() << "newGreen = " << newGreen;
    25. qDebug() << "newBlue = " << newBlue;
    26.  
    27. // make picture by direct pixel operation
    28. postherized_img.setPixel(x, y, qRgba(newRed, newGreen, newBlue, a));
    29. }
    30. // save the picture
    31. postherized_img.save("/home/user/Desktop/test_posterized.jpg", "JPG", 100);
    To copy to clipboard, switch view to plain text mode 

    What i do wrong?
    Last edited by suseway; 1st December 2010 at 14:38.

  9. #9
    Join Date
    May 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    Quote Originally Posted by suseway View Post
    Hello. I'm trying to implement this algorythm to Qt image operations, but it's create new original image (instead of posterized image).
    Here is my code:
    Qt Code:
    1. int r, g, b, a;
    2. QRgb val;
    3. double levels = 5.0; // posterize value 5.0
    4. levels -= 1.0; // by following gimp code
    5. int newRed, newGreen, newBlue;
    6. // starting loop
    7. for(int x=0; y<img.width(); x++)
    8. for(int y=0; y<img.height(); y++) {
    9. val = img.pixel(x, y);
    10. r = qRed(val);
    11. g = qGreen(val);
    12. b = qBlue(val);
    13. a = qAlpha(val);
    14.  
    15. qDebug() << "r = " << r;
    16. qDebug() << "g = " << g;
    17. qDebug() << "b = " << b;
    18. // lines 21-23 from GIMP...
    19. newRed = (rint) ( (double)r / 255.0 * levels) / levels;
    20. newGreen = (rint) ( (double)g / 255.0 * levels) / levels;
    21. newBlue = (rint) ( (double)b / 255.0 * levels) / levels;
    22.  
    23. qDebug() << "newRed = " << newRed;
    24. qDebug() << "newGreen = " << newGreen;
    25. qDebug() << "newBlue = " << newBlue;
    26.  
    27. // make picture by direct pixel operation
    28. postherized_img.setPixel(x, y, qRgba(newRed, newGreen, newBlue, a));
    29. }
    30. // save the picture
    31. postherized_img.save("/home/user/Desktop/test_posterized.jpg", "JPG", 100);
    To copy to clipboard, switch view to plain text mode 

    What i do wrong?

    You have to round the values correctly. Look my code (it worked and generated an identical result as GIMP).

    Qt Code:
    1. QImage img = inputImg.toImage();
    2. QRgb val;
    3. int levels = 2.0; // Posterize level two;
    4. levels--;
    5. double sr, sg, sb;
    6. int dr, dg, db;
    7. for (int i=0; i<img.width(); ++i) {
    8. for (int j=0; j<img.height(); ++j) {
    9. val = img.pixel(i, j);
    10. sr = qRed(val)/255.0;
    11. sg = qGreen(val)/255.0;
    12. sb = qBlue(val)/255.0; // to make sr, sg, sb between 0 and 1
    13. dr = 255 * qRound(sr * levels)/levels;
    14. dg = 255 * qRound(sg * levels)/levels;
    15. db = 255 * qRound(sb * levels)/levels; // qRound does the same as RINT (rounding and NOT TRUNCATING its argument).
    16. img.setPixel(i, j, qRgb(dr, dg, db));
    17. }
    18. }
    19. outputImg = QPixmap::fromImage(img);
    To copy to clipboard, switch view to plain text mode 

    Please notice that I didn't care about the alpha channel because it's a JPG image (no alpha) and it was not modified. But if the source image has an alpha channel you MUST copy the information to the destination (and change the function qRgb to qRgba, and the type QRgb to QRgba).

    EDIT: Oh my god! I've answered a thread from 5 (FIVE!!) years ago [=P]

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Posterizes an image with results identical to Gimp's Posterize command

    Oh my god! I've answered a thread from 5 (FIVE!!) years ago
    Well, at least the OP can finally get on with this project...

Similar Threads

  1. Alpha Channel support is broken for GIMP png and bmp files
    By mschifter in forum Qt Programming
    Replies: 3
    Last Post: 25th June 2015, 21:52
  2. How to edit selection with posterize and threshold
    By suseway in forum Qt Programming
    Replies: 0
    Last Post: 25th October 2010, 08:35
  3. PyQt QImage and BMP in 16bits 565 created from The Gimp
    By sebcbien in forum Qt Programming
    Replies: 0
    Last Post: 11th August 2010, 14:49
  4. GIMP and GPL
    By mhoover in forum General Discussion
    Replies: 1
    Last Post: 30th May 2010, 08:10
  5. Replies: 4
    Last Post: 14th July 2009, 03:27

Tags for this Thread

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.