You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pivot.apache.org by "Bill van Melle (JIRA)" <ji...@apache.org> on 2011/03/25 22:05:05 UTC

[jira] [Created] (PIVOT-723) Better GrayscaleDecorator

Better GrayscaleDecorator
-------------------------

                 Key: PIVOT-723
                 URL: https://issues.apache.org/jira/browse/PIVOT-723
             Project: Pivot
          Issue Type: Improvement
          Components: wtk-effects
    Affects Versions: 2.0
            Reporter: Bill van Melle
            Priority: Minor


The current GrayscaleDecorator doesn't work properly when the decorated component uses transparency -- it turns the transparent pixels black, which seems unlikely to be what you want.   Here's a new version of GrayscaleDecorator#prepare that creates the BufferedImage in a transparency-aware way:

    public Graphics2D prepare(Component component, Graphics2D graphics) {
        this.graphics = graphics;

        int width = component.getWidth();
        int height = component.getHeight();
        
        // To convert to gray, we create a BufferedImage in the grayscale color space
        // into which the decorated component draws, and we output the resulting image.
        // The naive way to create the buffer is
        //     new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        // but that doesn't respect transparency.  Hence the following more complicated method.
        
        if (bufferedImage == null
                || bufferedImage.getWidth() < width
                || bufferedImage.getHeight() < height) {
            ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false,
                                                        Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
            WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
            bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
        }

        bufferedImageGraphics = bufferedImage.createGraphics();
        bufferedImageGraphics.setClip(graphics.getClip());

        return bufferedImageGraphics;
    }


--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (PIVOT-723) Better GrayscaleDecorator

Posted by "Bill van Melle (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/PIVOT-723?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Bill van Melle updated PIVOT-723:
---------------------------------

    Attachment: error-round.png

Sample png with transparency to demonstrate the issue.  Taken from the Wikimedia commons page http://commons.wikimedia.org/wiki/Tango_icon where there are lots more examples.

> Better GrayscaleDecorator
> -------------------------
>
>                 Key: PIVOT-723
>                 URL: https://issues.apache.org/jira/browse/PIVOT-723
>             Project: Pivot
>          Issue Type: Improvement
>          Components: wtk-effects
>    Affects Versions: 2.0
>            Reporter: Bill van Melle
>            Priority: Minor
>         Attachments: error-round.png
>
>
> The current GrayscaleDecorator doesn't work properly when the decorated component uses transparency -- it turns the transparent pixels black, which seems unlikely to be what you want.   Here's a new version of GrayscaleDecorator#prepare that creates the BufferedImage in a transparency-aware way:
>     public Graphics2D prepare(Component component, Graphics2D graphics) {
>         this.graphics = graphics;
>         int width = component.getWidth();
>         int height = component.getHeight();
>         
>         // To convert to gray, we create a BufferedImage in the grayscale color space
>         // into which the decorated component draws, and we output the resulting image.
>         // The naive way to create the buffer is
>         //     new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
>         // but that doesn't respect transparency.  Hence the following more complicated method.
>         
>         if (bufferedImage == null
>                 || bufferedImage.getWidth() < width
>                 || bufferedImage.getHeight() < height) {
>             ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>             ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false,
>                                                         Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
>             WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
>             bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
>         }
>         bufferedImageGraphics = bufferedImage.createGraphics();
>         bufferedImageGraphics.setClip(graphics.getClip());
>         return bufferedImageGraphics;
>     }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Resolved] (PIVOT-723) Better GrayscaleDecorator

Posted by "Noel Grandin (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/PIVOT-723?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Noel Grandin resolved PIVOT-723.
--------------------------------

       Resolution: Fixed
    Fix Version/s:     (was: 2.1)
                   2.0.1
         Assignee: Noel Grandin

Fixed in rev 1150611

> Better GrayscaleDecorator
> -------------------------
>
>                 Key: PIVOT-723
>                 URL: https://issues.apache.org/jira/browse/PIVOT-723
>             Project: Pivot
>          Issue Type: Improvement
>          Components: wtk-effects
>    Affects Versions: 2.0
>            Reporter: Bill van Melle
>            Assignee: Noel Grandin
>            Priority: Minor
>             Fix For: 2.0.1
>
>         Attachments: error-round.png
>
>
> The current GrayscaleDecorator doesn't work properly when the decorated component uses transparency -- it turns the transparent pixels black, which seems unlikely to be what you want.   Here's a new version of GrayscaleDecorator#prepare that creates the BufferedImage in a transparency-aware way:
>     public Graphics2D prepare(Component component, Graphics2D graphics) {
>         this.graphics = graphics;
>         int width = component.getWidth();
>         int height = component.getHeight();
>         
>         // To convert to gray, we create a BufferedImage in the grayscale color space
>         // into which the decorated component draws, and we output the resulting image.
>         // The naive way to create the buffer is
>         //     new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
>         // but that doesn't respect transparency.  Hence the following more complicated method.
>         
>         if (bufferedImage == null
>                 || bufferedImage.getWidth() < width
>                 || bufferedImage.getHeight() < height) {
>             ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>             ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false,
>                                                         Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
>             WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
>             bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
>         }
>         bufferedImageGraphics = bufferedImage.createGraphics();
>         bufferedImageGraphics.setClip(graphics.getClip());
>         return bufferedImageGraphics;
>     }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Updated] (PIVOT-723) Better GrayscaleDecorator

Posted by "Sandro Martini (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/PIVOT-723?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Sandro Martini updated PIVOT-723:
---------------------------------

    Fix Version/s: 2.1

> Better GrayscaleDecorator
> -------------------------
>
>                 Key: PIVOT-723
>                 URL: https://issues.apache.org/jira/browse/PIVOT-723
>             Project: Pivot
>          Issue Type: Improvement
>          Components: wtk-effects
>    Affects Versions: 2.0
>            Reporter: Bill van Melle
>            Priority: Minor
>             Fix For: 2.1
>
>         Attachments: error-round.png
>
>
> The current GrayscaleDecorator doesn't work properly when the decorated component uses transparency -- it turns the transparent pixels black, which seems unlikely to be what you want.   Here's a new version of GrayscaleDecorator#prepare that creates the BufferedImage in a transparency-aware way:
>     public Graphics2D prepare(Component component, Graphics2D graphics) {
>         this.graphics = graphics;
>         int width = component.getWidth();
>         int height = component.getHeight();
>         
>         // To convert to gray, we create a BufferedImage in the grayscale color space
>         // into which the decorated component draws, and we output the resulting image.
>         // The naive way to create the buffer is
>         //     new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
>         // but that doesn't respect transparency.  Hence the following more complicated method.
>         
>         if (bufferedImage == null
>                 || bufferedImage.getWidth() < width
>                 || bufferedImage.getHeight() < height) {
>             ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>             ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false,
>                                                         Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
>             WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
>             bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
>         }
>         bufferedImageGraphics = bufferedImage.createGraphics();
>         bufferedImageGraphics.setClip(graphics.getClip());
>         return bufferedImageGraphics;
>     }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (PIVOT-723) Better GrayscaleDecorator

Posted by "Bill van Melle (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/PIVOT-723?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13011448#comment-13011448 ] 

Bill van Melle commented on PIVOT-723:
--------------------------------------

Here is a test app using the attached image:

<Window
  xmlns:effects="org.apache.pivot.wtk.effects"
  xmlns="org.apache.pivot.wtk">
  <BoxPane styles="{backgroundColor:'white'}">
    <ImageView image="@error-round.png" styles="{backgroundColor:null}" />
    <ImageView image="@error-round.png" styles="{backgroundColor:null}">
      <decorators>
        <effects:GrayscaleDecorator />
      </decorators>
    </ImageView>
  </BoxPane>
</Window>

> Better GrayscaleDecorator
> -------------------------
>
>                 Key: PIVOT-723
>                 URL: https://issues.apache.org/jira/browse/PIVOT-723
>             Project: Pivot
>          Issue Type: Improvement
>          Components: wtk-effects
>    Affects Versions: 2.0
>            Reporter: Bill van Melle
>            Priority: Minor
>         Attachments: error-round.png
>
>
> The current GrayscaleDecorator doesn't work properly when the decorated component uses transparency -- it turns the transparent pixels black, which seems unlikely to be what you want.   Here's a new version of GrayscaleDecorator#prepare that creates the BufferedImage in a transparency-aware way:
>     public Graphics2D prepare(Component component, Graphics2D graphics) {
>         this.graphics = graphics;
>         int width = component.getWidth();
>         int height = component.getHeight();
>         
>         // To convert to gray, we create a BufferedImage in the grayscale color space
>         // into which the decorated component draws, and we output the resulting image.
>         // The naive way to create the buffer is
>         //     new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
>         // but that doesn't respect transparency.  Hence the following more complicated method.
>         
>         if (bufferedImage == null
>                 || bufferedImage.getWidth() < width
>                 || bufferedImage.getHeight() < height) {
>             ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>             ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false,
>                                                         Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
>             WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
>             bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
>         }
>         bufferedImageGraphics = bufferedImage.createGraphics();
>         bufferedImageGraphics.setClip(graphics.getClip());
>         return bufferedImageGraphics;
>     }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira