MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

ImageResize does not linearize before resize?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116988] ImageResize does not linearize before resize?
  • From: Jari Paljakka <spam at here.invalid>
  • Date: Sun, 6 Mar 2011 05:45:29 -0500 (EST)

Hi,

While trialing Mathematica 8, I noticed ImageResize[] doesn't seem to linearize 
the colorspace before resizing.

Is this intentional?

Reasoning why it would be a nice option to have: 
http://www.4p8.com/eric.brasseur/gamma.html

Of course, to "fix" this properly, image needs to have information in what 
colorspace it is in in the first place, but to have
the option available directly in ImageResize would be nice, as I suspect it 
could be made quite a bit faster.

This non-linearization is probably assumed for all other image functions too?

Example how to expose the issue (and how to fix, albeit slow and convoluted):

(used "Dalai Lama" pic from the link above, which is constructed to exhibit the 
issue)
(remove compilation target etc if you don't have a compiler - was testing how 
to make corrections run "faster")

Clear[srgb2linear, linear2srgb, linearresize, image]; 
srgb2linear = Compile[{{Csrgb, _Real, 1}}, 
       With[{\[Alpha] = 0.055}, Table[Piecewise[
             {{C/12.92, 
        C <= 0.04045}, {((C + \[Alpha])/(1 + \[Alpha]))^2.4, 
                 C > 0.04045}}], {C, Csrgb}]], RuntimeAttributes -> 
         {Listable}, Parallelization -> True, CompilationTarget -> 
         "C", RuntimeOptions -> "Speed"]; 
linear2srgb = Compile[{{Clinear, _Real, 1}}, 
       With[{\[Alpha] = 0.055}, Table[Piecewise[
             {{12.92*C, 
        C <= 0.0031308}, {(1 + \[Alpha])*C^(1/2.4) - \[Alpha], 
                 C > 0.0031308}}], {C, Clinear}]], 
       RuntimeAttributes -> {Listable}, Parallelization -> True, 
       CompilationTarget -> "C", RuntimeOptions -> "Speed"]; 
linearresize[image_Image, scaling_] := 
   Module[{}, ((Image[#1, ColorSpace -> "RGB"] & )[
            linear2srgb[ImageData[#1, Interleaving -> True]]] & )[
       ImageResize[(Image[#1, ColorSpace -> "RGB"] & )[
           srgb2linear[ImageData[image, Interleaving -> True]]], 
         scaling]]]
image = Image["Dalai Lama", "Real"]

In[432]:= ImageResize[ImageResize[image, Scaled[1/2]], Scaled[2]]

Out:= Screwed up picture

In[433]:= linearresize[linearresize[image, Scaled[1/2]], Scaled[2]]

Out:= Correct image (of course sampled)



  • Prev by Date: Re: Patterns with conditions
  • Next by Date: Re: Select from Tuplet using logical expression
  • Previous by thread: Re: random line in a very large file
  • Next by thread: Re: ImageResize does not linearize before resize?