p{}.intensity ignores -intensity

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

p{}.intensity ignores -intensity

Post by snibgo »

Is there a good reason for %[fx:p{0,0}.intensity] to ignore the -intensity setting? Seems like a bug to me.

For example:

convert xc:srgb(10%,20%,30%) -format %[fx:100*p{0,0}.intensity] INFO:
18.5962

convert xc:srgb(10%,20%,30%) -intensity Brightness -format %[fx:100*p{0,0}.intensity] INFO:
18.5962

convert xc:srgb(10%,20%,30%) -intensity RMS -format %[fx:100*p{0,0}.intensity] INFO:
18.5962

convert xc:srgb(10%,20%,30%) -intensity Average -format %[fx:100*p{0,0}.intensity] INFO:
18.5962

Tested with v6.9.0-0 under Windows 8.1.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: p{}.intensity ignores -intensity

Post by fmw42 »

I am not aware that you can use ".intensity" as if it were a color channel selector. I do not know if is correct or not and whether it relates to your issue. What happens if you leave off ".intensity". Does -intensity work with %[pixel: ...] rather than %[fx: ...]?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: p{}.intensity ignores -intensity

Post by snibgo »

"pixel:" returns a colour rather than a number. In this case:

%IM%convert xc:srgb(10%,20%,30%) -intensity RMS -format %[pixel:100*p{0,0}.intensity] INFO:
srgb(1859.62%,1859.62%,1859.62%)

Leaving off intensity returns the same value as using it:

%IM%convert xc:srgb(10%,20%,30%) -format %[fx:100*p{0,0}.intensity] INFO:
18.5962
%IM%convert xc:srgb(10%,20%,30%) -format %[fx:100*p{0,0}] INFO:
18.5962

Leaving it off also ignores the "-intensity" setting.

The fx documentation http://www.imagemagick.org/script/fx.php lists "intensity" among the other channel selectors:

c
m
y
k
intensity
hue
saturation
lightness
luma

The one I really want is "brightness", the maximum of the RGB channels, but that isn't valid here. Why not? I don't know.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: p{}.intensity ignores -intensity

Post by snibgo »

It seems that magick\fx.c uses MagickPixelIntensityToQuantum(&pixel), which is defined in color-private.h as a fixed formula: 0.212656*pixel->red+0.715158*pixel->green+0.072186*pixel->blue

I think it should use GetPixelIntensity(image,&pixel) probably in both places: the default with no channel qualifier, and with ".intensity".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: p{}.intensity ignores -intensity

Post by fmw42 »

snibgo wrote:The fx documentation http://www.imagemagick.org/script/fx.php lists "intensity" among the other channel selectors:
I see them there, but I am not sure they are channel selectors. I see nothing that actually says they are.

I assume that they are used as:

%[fx:intensity]

would return the intensity of every pixel in the image. Similarly for hue and saturation.

You can get the equivalent of Brightness from

%{fx:max(max(u.r,u.g),u.b)]

or perhaps

%[fx:max(max(red,green),blue]


However, it is odd that intensity and lightness are there but not brightness. It is also unclear what luma represents, perhaps Y, but that should be the same as intensity. The other possible missing one would be equal weighted average.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: p{}.intensity ignores -intensity

Post by fmw42 »

These seem to show different values:

Code: Select all

convert xc:"srgb(10%,20%,30%)" -format "%[fx:100*intensity]" info:
18.5962

convert xc:"srgb(10%,20%,30%)" -format "%[fx:100*hue]" info:
58.334

convert xc:"srgb(10%,20%,30%)" -format "%[fx:100*saturation]" info:
49.9981

convert xc:"srgb(10%,20%,30%)" -format "%[fx:100*lightness]" info:
20.0008

convert xc:"srgb(10%,20%,30%)" -format "%[fx:100*luma]" info:
18.5955
Looks like luma and intensity are nearly identical.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: p{}.intensity ignores -intensity

Post by snibgo »

%[fx:intensity] doesn't return the intensity of every pixel in the image.

max(max( is a workaround for my situation, of course.

My suspicion, that replacing MagickPixelIntensityToQuantum with GetPixelIntensity cures the problem, is correct. I now get these results:

Code: Select all

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -format "%[fx:100*p{0,0}.intensity]" INFO:
18.5953

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity Brightness -format "%[fx:100*p{0,0}.intensity]" INFO:
30

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity RMS -format "%[fx:100*p{0,0}.intensity]" INFO:
21.6025

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity Average -format "%[fx:100*p{0,0}.intensity]" INFO:
20

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity Brightness -format "%[fx:100*p{0,0}.intensity]" INFO:
30

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -format "%[fx:100*p{0,0}]" INFO:
18.5953

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity Brightness -format "%[fx:100*p{0,0}]" INFO:
30

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity RMS -format "%[fx:100*p{0,0}]" INFO:
21.6025

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity Average -format "%[fx:100*p{0,0}]" INFO:
20

f:\web\im>c:\cygwin64\home\Alan\iminst32f\bin\convert xc:srgb(10%,20%,30%) -intensity Brightness -format "%[fx:100*p{0,0}]" INFO:
30
The diff from v6.9.0-0 is:

Code: Select all

--- /home/Alan/ImageMagick-6.9.0-0/magick/factory/fx.c	2015-01-04 06:01:13.078500800 +0000
+++ /home/Alan/ImageMagick-6.9.0-0//magick/fx.c	2015-01-04 05:56:09.622291000 +0000
@@ -1519,7 +1519,12 @@
         }
         case DefaultChannels:
         {
-          return(QuantumScale*MagickPixelIntensityToQuantum(&pixel));
+          //return(QuantumScale*MagickPixelIntensityToQuantum(&pixel));
+          PixelPacket ppix;
+          ppix.red=pixel.red;
+          ppix.green=pixel.green;
+          ppix.blue=pixel.blue;
+          return(QuantumScale*GetPixelIntensity(image,&ppix));
         }
         default:
           break;
@@ -1697,8 +1702,14 @@
         return(image->x_resolution);
       if (LocaleCompare(symbol,"image.resolution.y") == 0)
         return(image->y_resolution);
-      if (LocaleCompare(symbol,"intensity") == 0)
-        return(QuantumScale*MagickPixelIntensityToQuantum(&pixel));
+      if (LocaleCompare(symbol,"intensity") == 0) {
+        //return(QuantumScale*MagickPixelIntensityToQuantum(&pixel));
+        PixelPacket ppix;
+        ppix.red=pixel.red;
+        ppix.green=pixel.green;
+        ppix.blue=pixel.blue;
+        return(QuantumScale*GetPixelIntensity(image,&ppix));
+      }
       if (LocaleCompare(symbol,"i") == 0)
         return((double) x);
       break;

(I commented out the old code.)
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: p{}.intensity ignores -intensity

Post by fmw42 »

%[fx:intensity] doesn't return the intensity of every pixel in the image.
Correct. But it does when you use -fx, which is what I really meant. Sorry I was thinking one thing and writing another.

Code: Select all

convert xc:"srgb(10%,20%,30%)" xc:"srgb(5%,15%,15%)" +append -fx "intensity" txt:
# ImageMagick pixel enumeration: 2,1,65535,srgb
0,0: (18.5962%,18.5962%,18.5962%) #2F9B2F9B2F9B srgb(18.5962%,18.5962%,18.5962%)
1,0: (12.8725%,12.8725%,12.8725%) #20F420F420F4 srgb(12.8725%,12.8725%,12.8725%)


What I was really trying to say is that it I believed the syntax was intensity and not u.intensity, though that seems to be the same thing.

Code: Select all

convert xc:"srgb(10%,20%,30%)" -format "%[*intensity]" info:
18.5962

Code: Select all

convert xc:"srgb(10%,20%,30%)" -format "%[fx:100*u.intensity]" info:
18.5962


I am not sure about the use of -intensity XXX and you may be right about the need for a code change to make that work.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: p{}.intensity ignores -intensity

Post by magick »

We can reproduce the problem you posted and have a patch in ImageMagick 6.9.0-4 Beta, available by sometime tomorrow. Thanks.
Post Reply