After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 170801 - Converting image from grayscale to black&white is painfully slow
Converting image from grayscale to black&white is painfully slow
Status: RESOLVED FIXED
Product: GIMP
Classification: Other
Component: General
2.2.x
Other All
: Normal normal
: ---
Assigned To: GIMP Bugs
GIMP Bugs
Depends on:
Blocks:
 
 
Reported: 2005-03-18 14:46 UTC by Xuân Baldauf
Modified: 2008-01-15 12:50 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Image where to 1bit conversion is either slow or buggy (with GIMP 2.2.4) (19.24 KB, image/png)
2005-03-18 17:36 UTC, Xuân Baldauf
Details

Description Xuân Baldauf 2005-03-18 14:46:31 UTC
1. Open a large grayscale image of your choice (e.g. larger than 2000*2000
pixels, maybe a scan result from your scanner)
2. Use "Tools/Color Tools/Threshold" to apply some threshold choosen
arbitrarily. This tool is pretty fast.
3. Now you have a 8bit grayscale image, which actually consists only of color
values "0" and color values "255". Converting it to a 1bit black&white image
should be a very fast operation (replacing 0 (8bit) by 0 (1bit) and replacing
255 (8bit) by 1 (1bit)), but it is not:
4. Choose "Image/Mode/Indexed...", and there:
4.1 Colormap: "Use black and white (1-bit) palette)" (this should be the fastest
option possible)
4.2 Dithering: "none" (this should be the fastest option possible)
4.3 Now hit "OK"
5. The conversion will work, but it will take many seconds. For an example image
of "2460*3483", it takes 34 seconds on my computer (while applying the Threshold
tool, which  basically does the same, only needs maybe 2 seconds).

This slow speed is not acceptable for interactive image processing, and this
slowness is not necessary at all.
Comment 1 weskaggs 2005-03-18 17:07:32 UTC
Indeed it is very slow, so I will confirm this bug report.  There is, however, a
workaround.  I found with a little experimentation that if you use the "Optimal
Palette" option for step 4.1, then the conversion occurs in a flash, literally a
fraction of a second for me.  This happens, I think, because if you use this
option, by the time the algorithm reaches the stage of actually converting the
image, it has built a color histogram and "knows" that there are exactly two
colors in the image, and can assign colors directly, without having to test
whether black or white is the best match for each pixel.

Because of the existence of this workaround, I am going to lower the severity,
but this *is* something that should be looked into.
Comment 2 Xuân Baldauf 2005-03-18 17:36:31 UTC
Created attachment 38911 [details]
Image where to 1bit conversion is either slow or buggy (with GIMP 2.2.4)

That's odd. I just tried to use your suggested workaround and was happy that
the conversion was as fast one should expect. However, the conversion was
plainly wrong, it produced an image which was just black (the input image was
nearly white).

I'm attaching a test case image. This may be a related bug.

Do following on the atached image:
1. Choose "Image/Mode/Indexed...", and there:
1.1 Colormap: optimal palette (wether 2 oder 256 colors, it does not matter)
1.2 Dithering: "none" (but I think it does not matter)
Comment 3 weskaggs 2005-03-18 18:02:11 UTC
Interesting -- clearly the generation of an optimal palette is not done
correctly for this image.  Because this is a different issue, I am going to open
a different bug report for it -- using your image as example, if you don't mind.

In any case, I've found a solution to the original problem, I think.  I found a
very simple change that gives a major speedup when converting grayscale images
to the mono palette.  It's now at least as fast as the Threshold tool, so I'm
going to resolve this bug as FIXED.  Still just as slow for RGB images, though.  

Committed to both branches:

2005-03-18  Bill Skaggs  <weskaggs@primate.ucdavis.edu>

	* app/core/gimpimage-convert.c:  when converting grayscale
	to mono, treat the palette as gray rather than rgb, giving
	more than tenfold speedup.  Fixes bug #170801.

The new problem is described in bug #170825
Comment 4 Xuân Baldauf 2005-03-18 18:08:42 UTC
I'd be pleased if you used my test case image. :-)

Thank you for fixing this bug so fast, so I just need to wait for the next
release. (Building for Windows would be rather messy, so I try to be patient...)
Comment 5 Adam D. Moss 2005-03-19 13:12:31 UTC
I'm interested in seeing the patch.  If you can't attach it then what revision
of gimpimage-convert.c contains the fix?
Comment 6 Manish Singh 2005-03-19 17:48:16 UTC
Revision 1.156. Here is the patch:

--- gimpimage-convert.c 27 Feb 2005 14:03:50 -0000      1.155
+++ gimpimage-convert.c 18 Mar 2005 17:34:34 -0000      1.156
@@ -4057,7 +4057,6 @@ initialize_median_cut (GimpImageBaseType
         }

       if (palette_type == GIMP_WEB_PALETTE  ||
-          palette_type == GIMP_MONO_PALETTE ||
           palette_type == GIMP_CUSTOM_PALETTE)
         {
           switch (dither_type)
Comment 7 Adam D. Moss 2005-03-19 18:14:01 UTC
Thanks.

That's interesting.  I expect it to yield poorer results though, to some extent
(probably most obviously darker, or lighter, in mid-greys, that is a damaged
gamma), because the greyscale colour-matching and error-quantifying is in a
comparatively non-perceptually-uniform space (that's half the reason why it's
faster, though it's not as big a quality difference in a greyscale source as an
RGB source).

Were side-by-side comparisons done?  I hope so, though I could do some here if
anyone cares.


Comment 8 Manish Singh 2005-03-19 18:21:59 UTC
I don't think anyone has done side-by-side comparisons.
Comment 9 Adam D. Moss 2005-03-20 12:18:10 UTC
Okay, I've just done some tests.  There are noticable qualitative gamma-related
differences, but not so strong that I'm really going to lose any sleep.
Comment 10 Adam D. Moss 2005-03-20 12:26:09 UTC
Note that the analysis in comment #1 is correct; the 'optimal' palette finder
does a first pass to see if there are already a suitable number of colours, and
then knows that it can skip both the expensive quantization and dithering stages.

The 'mono' palette option doesn't even bother to start this pre-pass because it
could only possibly pay off the extra effort if the entire image is pure black
and pure white, which is expected to be a comparatively rare occurance.

Comment 11 Xuân Baldauf 2005-03-20 13:06:07 UTC
<quote>
The 'mono' palette option doesn't even bother to start this pre-pass because it
could only possibly pay off the extra effort if the entire image is pure black
and pure white, which is expected to be a comparatively rare occurance.
</quote>

I don't think that this operation is so rare, because for getting an "optimal
distribution between black and white" for grayscale images (for example: you
want to generate faximilies the receiver actually is able to read), you use the
"threshold" operation, and then a "convert to 1bit" operation to actually adjust
the internal memory requirements.

So at least for me, this chain of operations is rather frequent.
Comment 12 Adam D. Moss 2005-03-20 14:01:48 UTC
> and then a "convert to 1bit" operation to actually adjust
> the internal memory requirements.

If you mean GIMP's internal memory requirements you're wasting your time -- all
Indexed images are 8/16bpp internally (the same as greyscale images), so you're
not saving any memory (instead you may be using quite a lot more, for various
reasons).
Comment 13 Xuân Baldauf 2005-03-20 14:04:13 UTC
I mean OpenOffice's internal memory requirements. For having 10 or 20 fax pages
in one OOo document, it quite makes a difference wether they are 8bit or 1bit.