Page 1 of 1

"convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T19:26:57-07:00
by akr
I found "convert -level 20%,100%,0.5" doesn't preserve black.

I made a simple PGM file which is gradation from black to white (using Ruby).
The leftest pixel is black (value 0) and the rightest pixel (value 255) is white.

"convert -level 20%,100%,0.5" converts the black pixel to a dark gray (value 16 = 020).

I guess it it not intentional behavior.

I tested ImageMagick-6.7.4 taken with svn.

Code: Select all

% ruby -e 'STDOUT.binmode; print "P5\n256 1\n255\n", (0..255).to_a.pack("C*")' > src.pgm
% convert -level 20%,100%,0.5 src.pgm dst.pgm
% od -c src.pgm 
0000000   P   5  \n   2   5   6       1  \n   2   5   5  \n  \0 001 002
0000020 003 004 005 006  \a  \b  \t  \n  \v  \f  \r 016 017 020 021 022
0000040 023 024 025 026 027 030 031 032 033 034 035 036 037       !   "
0000060   #   $   %   &   '   (   )   *   +   ,   -   .   /   0   1   2
0000100   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @   A   B
0000120   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R
0000140   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `   a   b
0000160   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r
0000200   s   t   u   v   w   x   y   z   {   |   }   ~ 177 200 201 202
0000220 203 204 205 206 207 210 211 212 213 214 215 216 217 220 221 222
0000240 223 224 225 226 227 230 231 232 233 234 235 236 237 240 241 242
0000260 243 244 245 246 247 250 251 252 253 254 255 256 257 260 261 262
0000300 263 264 265 266 267 270 271 272 273 274 275 276 277 300 301 302
0000320 303 304 305 306 307 310 311 312 313 314 315 316 317 320 321 322
0000340 323 324 325 326 327 330 331 332 333 334 335 336 337 340 341 342
0000360 343 344 345 346 347 350 351 352 353 354 355 356 357 360 361 362
0000400 363 364 365 366 367 370 371 372 373 374 375 376 377
0000415
% od -c dst.pgm                              
0000000   P   5  \n   2   5   6       1  \n   2   5   5  \n 020 017 017
0000020 016 016  \r  \f  \f  \v  \v  \n  \n  \t  \t  \b  \b  \b  \a  \a
0000040 006 006 006 005 005 004 004 004 004 003 003 003 002 002 002 002
0000060 002 001 001 001 001 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000100  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0 001 001 001 001 001 001
0000120 002 002 002 002 002 003 003 003 004 004 004 004 005 005 006 006
0000140 006  \a  \a  \b  \b  \b  \t  \t  \n  \n  \v  \v  \f  \f  \r 016
0000160 016 017 017 020 021 021 022 023 023 024 025 025 026 027 030 030
0000200 031 032 033 034 034 035 036 037       !   "   "   #   $   %   &
0000220   '   (   )   *   +   ,   -   .   /   1   2   3   4   5   6   7
0000240   8   :   ;   <   =   ?   @   A   B   D   E   F   G   I   J   K
0000260   M   N   P   Q   R   T   U   W   X   Z   [   ]   ^   `   a   c
0000300   d   f   h   i   k   l   n   p   q   s   u   v   x   z   |   }
0000320 177 201 203 204 206 210 212 214 216 217 221 223 225 227 231 233
0000340 235 237 241 243 245 247 251 253 255 257 261 263 265 267 272 274
0000360 276 300 302 304 307 311 313 315 317 322 324 326 331 333 335 340
0000400 342 344 347 351 353 356 360 363 365 370 372 375 377
0000415

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T20:03:49-07:00
by fmw42
On IM 6.7.4.0 Q16 Mac OSX Snow Leopard

convert -size 256x256 gradient: grad.pgm

identify -verbose grad.pgm

Gray:
min: 0 (0)
max: 65535 (1)
mean: 32767.5 (0.5)
standard deviation: 18992.4 (0.289805)
kurtosis: -1.20004
skewness: 1.26355e-12
Colors: 256
Histogram:
256: ( 0, 0, 0) #000000000000 black



convert grad.pgm -level 20%,100%,0.5 grad_20x100_0p5.pgm

identify -verbose grad_20x100_0p5.pgm
Gray:
min: 0 (0)
max: 65535 (1)
mean: 17816 (0.271855)
standard deviation: 19395.8 (0.295961)
kurtosis: -0.419133
skewness: 0.936594
Colors: 205
Histogram:
256: ( 0, 0, 0) #000000000000 black

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T20:34:22-07:00
by akr
I agree that several black pixels are exist in dst.pgm.

But that is not correspond to the black pixel in src.pgm.

I simplified the test to a single pixel black image.

The result image contains a pixel which color is grey11.

Code: Select all

% ruby -e 'STDOUT.binmode; print "P5\n1 1\n255\n\0"' > src.pgm 
% convert -level 20%,80%,0.5 src.pgm dst.pgm              
% identify -verbose dst.pgm                     
Image: dst.pgm
  Format: PNM (Portable anymap)
  Class: DirectClass
  Geometry: 1x1+0+0
  Resolution: 72x72
  Print size: 0.0138889x0.0138889
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    Gray:
      min: 28 (0.109804)
      max: 28 (0.109804)
      mean: 28 (0.109804)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
  Histogram:
         1: ( 28, 28, 28) #1C1C1C grey11
  Rendering intent: Undefined
  Interlace: None
  Background color: white
  Border color: rgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 1x1+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Undefined
  Orientation: Undefined
  Properties:
    date:create: 2011-12-18T12:27:02+09:00
    date:modify: 2011-12-18T12:27:02+09:00
    signature: a6bff0c1eb666b1a338e99909f596f62bcd99e647c51d5b0d3f1abd573e457ed
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 12b
  Number pixels: 1b
  Pixels per second: 2.2518PB
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.6.0-4 2010-11-16 Q16 http://www.imagemagick.org

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T21:12:52-07:00
by fmw42
It makes no sense to apply -level to a one-pixel image. -level needs to have more than one pixel as the image has to have at least two pixels to be able to stretch to full black and full white. Even two pixels may be problematic depending upon the values you use for the black and white points.

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T22:23:41-07:00
by akr
I see.

I try to explain the problem again using the 256 pixel image.

I made a single line gradation image, src.pgm, from black to white (using Ruby).
The pixel value is 0 to 255. The value of leftmost pixel is 0.

I convert it using ImageMagick: convert -level 20%,100%,0.5 src.pgm dst.pgm

I checked values in the result image, dst.pgm, using pnmnoraw (of netpbm).
The values is 16 ... 0 ... 255. The value of leftmost pixel is 16.

So, "convert -level 20%,100%,0.5" converts 0 to 16 : black to dark gray.
Is it intentional?

Code: Select all

% ruby -e 'STDOUT.binmode; print "P5\n256 1\n255\n", (0..255).to_a.pack("C*")' > src.pgm
% convert -level 20%,100%,0.5 src.pgm dst.pgm
% pnmnoraw src.pgm                           
P2
256 1
255
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
255
% pnmnoraw dst.pgm 
P2
256 1
255
16 15 15 14 14 13 12 12 11 11 10 10 9 9 8 8 8
7 7 6 6 6 5 5 4 4 4 4 3 3 3 2 2 2
2 2 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2
2 2 2 2 3 3 3 4 4 4 4 5 5 6 6 6 7
7 8 8 8 9 9 10 10 11 11 12 12 13 14 14 15 15
16 17 17 18 19 19 20 21 21 22 23 24 24 25 26 27 28
28 29 30 31 32 33 34 34 35 36 37 38 39 40 41 42 43
44 45 46 47 49 50 51 52 53 54 55 56 58 59 60 61 63
64 65 66 68 69 70 71 73 74 75 77 78 80 81 82 84 85
87 88 90 91 93 94 96 97 99 100 102 104 105 107 108 110 112
113 115 117 118 120 122 124 125 127 129 131 132 134 136 138 140 142
143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175
177 179 181 183 186 188 190 192 194 196 199 201 203 205 207 210 212
214 217 219 221 224 226 228 231 233 235 238 240 243 245 248 250 253
255

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T23:28:26-07:00
by fmw42
what does IM identify -verbose yourimage.pgm say about the min and max values and histogram. I ran that above and found that everything was fine. Min value after your command was still 0.

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-17T23:37:09-07:00
by akr
It is not surprised that the min is 0
because the pixel at x=42 to x=60 in dst.pgm is value 0.

The problem is the pixel at x=0 to x=41 is not value 0.

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-18T11:37:02-07:00
by fmw42
I cannot say for sure, but remember that you are doing a non-linear gamma operation, I believe, after stretching. But perhaps it is done in a different order. One would have to check the code.

I will drop out of this conversation, so you can reply and hopefully the IM developers can take over and explain what might be happening.

Re: "convert -level 20%,100%,0.5" doesn't preserve black

Posted: 2011-12-18T15:54:07-07:00
by magick
We can reproduce the problem you reported and have a patch. Look for it in ImageMagick 6.7.0-1 Beta within a day or two. Thanks.