|
View:
New views
12 Messages
—
Rating Filter:
Alert me
|
|
|
How to convert PNG to jpg and alpha mask pngHi all,
I'm a JAI newbie and have tried searching long and hard before posting. I have a task I am doing now with imagemagick that I have to be able to do with java (and not just a JNI around imagemagick). It seems to me like it would be a fairly common task but I'm surprised I haven't found much on it. TASK: convert PNG to jpg and alpha mask png Essentially, If png has alpha channel convert png to jpg while also resizing to a given size create a grayscale png alpha mask from that original png while also resizing else convert png to jpg while also resizing to given size So I need to know 1) how to determine if given png has an alpha channel 2) how to create the a jpg with just the RGB part of the given png 3) how to create a grayscale alpha mask from the given png If anyone can point me in the right direction I'd be very appreciative. [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] http://forums.java.net/jive/thread.jspa?messageID=274643 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngFirstly I think you should look at JAI Image I/O Tools https://jai-imageio.dev.java.net
especially if you have not written any code yet. On May 15, 2008, at 10:51 AM, jai-interest@... wrote: > Hi all, > > I'm a JAI newbie and have tried searching long and hard before > posting. I have a task I am doing now with imagemagick that I have > to be able to do with java (and not just a JNI around imagemagick). > It seems to me like it would be a fairly common task but I'm > surprised I haven't found much on it. > > TASK: convert PNG to jpg and alpha mask png > > Essentially, > If png has alpha channel > convert png to jpg while also resizing to a given size > create a grayscale png alpha mask from that original png while > also resizing > else > convert png to jpg while also resizing to given size > > So I need to know > 1) how to determine if given png has an alpha channel > 2) how to create the a jpg with just the RGB part of the given png > 3) how to create a grayscale alpha mask from the given png > > If anyone can point me in the right direction I'd be very > appreciative. > [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] > > http://forums.java.net/jive/thread.jspa?messageID=274643 > > --------------------------------------------------------------------- > To unsubscribe, e-mail: interest-unsubscribe@... > For additional commands, e-mail: interest-help@... > >^..^< >^..^< Brian Burkhalter Java Imaging and Video Sun Microsystems, Inc. This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngFirstly, is the alpha channel associated (premultiplied) or
unassociated (not premultiplied)? On May 15, 2008, at 10:51 AM, jai-interest@... wrote: > Hi all, > > I'm a JAI newbie and have tried searching long and hard before > posting. I have a task I am doing now with imagemagick that I have > to be able to do with java (and not just a JNI around imagemagick). > It seems to me like it would be a fairly common task but I'm > surprised I haven't found much on it. > > TASK: convert PNG to jpg and alpha mask png > > Essentially, > If png has alpha channel > convert png to jpg while also resizing to a given size > create a grayscale png alpha mask from that original png while > also resizing > else > convert png to jpg while also resizing to given size > > So I need to know > 1) how to determine if given png has an alpha channel > 2) how to create the a jpg with just the RGB part of the given png > 3) how to create a grayscale alpha mask from the given png > > If anyone can point me in the right direction I'd be very > appreciative. > [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] > > http://forums.java.net/jive/thread.jspa?messageID=274643 > > --------------------------------------------------------------------- > To unsubscribe, e-mail: interest-unsubscribe@... > For additional commands, e-mail: interest-help@... > >^..^< >^..^< Brian Burkhalter Java Imaging and Video Sun Microsystems, Inc. This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngBrian,
Thanks for your replies. I will not know in advance anything about the png so I need to determine with code anything I have to know before making the conversion. do you know api call to find out whether it is premultiplied or not? What would be the difference in how I code it? What's the best source of info to learn these things? I'm happy to study books/code/etc. But need pointed toward the right direction since there is just SO much. The code I need to write has to be server-side (no GUI). I'll look into the JAI Image IO Tools though. Thanks, Lynn [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] http://forums.java.net/jive/thread.jspa?messageID=274902 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask png> do you know api call to find out whether it is
> premultiplied or not? renderedImage.getColorModel().isAlphaPremultiplied() > What's the best source of info to > learn these things? I'm happy to study > books/code/etc. But need pointed toward the right > direction since there is just SO much. Here you go: http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/JAITOC.fm.html http://java.sun.com/developer/onlineTraining/javaai/jai/index.html http://java.sun.com/products/java-media/jai/utilities/jaiutils.html https://jaistuff.dev.java.net/ http://archives.java.sun.com/cgi-bin/wa?A0=JAI-INTEREST (this was a mailinglist, closed but still full of useful information) [Message sent by forum member 'jonasfleer' (jonasfleer)] http://forums.java.net/jive/thread.jspa?messageID=274984 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngThanks jonasfleer for the hint on isAlphaPremultiplied() and the other general links.
After looking at the PNG spec, it seems to me that all PNG's are non-associated (not premultiplied). Am I wrong in that Brian? ( http://www.w3.org/TR/PNG-Rationale.html section 12.8 ) Anyhow, I'd still love any more specific guidance on how to take just the RGB values of a png that has ColorModel's getTransparency() == Transparency.TRANSLUCENT and make a jpg out of them. And then take just the alpha channel and create a 1 band, 8bit per pixel, opaque grayscale png that can serve as a mask in a program that understands how to use a mask. Thanks in advance to all who can help! Lynn [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] http://forums.java.net/jive/thread.jspa?messageID=275369 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngSo, i'm also rather new to JAI, but this is how i extract alpha from an image ...
final public static int ALPHA_BAND_INDEX = 3; public RenderedOp createRenderedOp(final RenderedImage source) { RenderedOp alpha; if (source.getColorModel() instanceof IndexColorModel) { alpha = AndConstDescriptor.create(source, new int[] { ((IndexColorModel)source.getColorModel()).getTransparentPixel()}, renderingHints); } else { if (source.getColorModel().getNumComponents()==4) { // Alpha channel is on band ALPHA_BAND_INDEX ParameterBlock params = new ParameterBlock(); params.addSource (source); params.add(new int[] {ALPHA_BAND_INDEX}); alpha = JAI.create("bandSelect", params, renderingHints); } else { // No alpha channel present, create a full opaque one alpha = ConstantDescriptor.create((float)source.getWidth(), (float)source.getHeight(), new Byte[]{Byte.valueOf((byte)0xFF)}, renderingHints); } } return alpha; } So, after that you've a 1-banded image. I've not worked with grayscale images yet, but loading and storing is just simple with the ImageIO class: http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html E.g. ImageIO.write(renderedImage, "jpg", new File("/tmp/result.jpg")); [Message sent by forum member 'jonasfleer' (jonasfleer)] http://forums.java.net/jive/thread.jspa?messageID=275439 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngThanks again Jonasfleer.
I'll play around with this some. More thoughts: From some messing around I did with bandselect to try to create the color jpg part I need, I realized that since the png's are not premultiplied but for it to have the correct colors when used with an alpha mask, the jpg I'm creating does need to be premultiplied. So I guess somehow that will be a part of what I have to do to create the jpg. But I haven't seen how to do that yet. bandcombine seems to only let you multiply a given band by a constant, not by a value from another band. I'm sure there is some way to do this that I'm just not seeing yet. Thanks, Lynn [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] http://forums.java.net/jive/thread.jspa?messageID=275530 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngMaybe this is what you are looking for:
http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html#coerceData(boolean) HTH, -James [Message sent by forum member 'jxc' (jxc)] http://forums.java.net/jive/thread.jspa?messageID=275537 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngThanks James. I did stumble upon the coerceData method before seeing your post and had some trouble where I thought it wasn't the answer, but it turned out I was forgetting to do something else that was the problem. Indeed coerceData was necessary and your post encouraged me to keep experimenting with it.
I'll paste some code (in a separate post) that I ended up using that is working in case someone else needs to do this sometime. I'm leaving this question as unanswered for now only because I have a couple more cases I am not handling that I'd love to hear some advice on from the guru's out there! :) The case (with two sub-cases) that I'm not yet handling is: If the png I am given has only 1 band (indexed color) but has either Transparency.BITMASK or Transparency.TRANSLUCENT ? I'd like to still create a jpg and alpha mask that work together. Thanks to all who helped so far. Lynn [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] http://forums.java.net/jive/thread.jspa?messageID=276131 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngrelevant snippets from a class I'm using ...
// assumes quality is a set as a member of the class // and File srcFile, String destJpgName and String destMaskName // are member variables also // so I'd create an instance of the class, set the quality then // call this create(File src) passing a file that is a png. public void create(File src) throws IOException { this.srcFile = src; String ext = FilenameUtils.getExtension(src.getAbsolutePath()); this.destJpgName = src.getAbsolutePath().replaceFirst("\\." +ext + "$", ".jpg"); this.destMaskName = src.getAbsolutePath().replaceFirst("\\." +ext + "$", "Mask.png"); if(has4BandsWithAlpha(src)) { BufferedImage inputBufferedImage = ImageIO.read(srcFile); createJpgForMask(inputBufferedImage); createMask(inputBufferedImage); } else { createNormalJpg(); } } /* * for now this should only be called with ones we already know meet the * 4 bands and alpha channel test! */ private void createJpgForMask(BufferedImage input) throws IOException { // all png's with alpha channel should by definition be non premultiplied // so to create the jpg we need to coerce it to be premultiplied input.coerceData(true); int[] bandIndices; bandIndices = new int[3]; bandIndices[0] = 0; bandIndices[1] = 1; bandIndices[2] = 2; ParameterBlock pb = new ParameterBlock(); pb.addSource(input); pb.add(bandIndices); RenderedImage dst = (RenderedImage)JAI.create("bandSelect",pb); compressAndWrite(dst); } private void createNormalJpg() throws IOException { // when used BufferedImage inputBufferedImage = ImageIO.read(srcFile); to read in, // it didn't come out in correct colors but with JAI.create it did. ??? RenderedImage dst = JAI.create("fileload", srcFile.getAbsolutePath()); compressAndWrite(dst); } private void compressAndWrite(RenderedImage img) throws IOException { // would use if didn't want to compress first // JAI.create("filestore", dst, destJpgName, "JPEG"); // next code is longer but saves writing to file and reading back in again ImageOutputStream ios = null; ImageWriter writer = null; try { writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpg").next(); // we assume one is present ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault()); iwparam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT) ; iwparam.setCompressionQuality(quality); ios = ImageIO.createImageOutputStream(new File(destJpgName)); writer.setOutput(ios); writer.write(null, new IIOImage(img, null, null), iwparam); } finally { try { ios.flush(); writer.dispose(); ios.close(); }catch(Exception e) {} } } /* * for now this should only be called with ones we already know meet the * 4 bands and alpha channel test! */ private void createMask(BufferedImage input) throws IOException { int[] bandIndices; bandIndices = new int[1]; bandIndices[0] = 3; // index of alpha band ParameterBlock pb = new ParameterBlock(); pb.addSource(input); pb.add(bandIndices); RenderedImage dst = (RenderedImage)JAI.create("bandSelect",pb); JAI.create("filestore", dst, destMaskName, "PNG"); } public static boolean has4BandsWithAlpha(File src) throws IOException { PlanarImage pi = JAI.create("fileload", src.getAbsolutePath()); SampleModel sm = pi.getSampleModel(); if( sm.getNumBands() == 4 && pi.getColorModel().hasAlpha() ) return true; else return false; } [Message sent by forum member 'lynn_javagirl' (lynn_javagirl)] http://forums.java.net/jive/thread.jspa?messageID=276133 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
|
|
Re: How to convert PNG to jpg and alpha mask pngSo has this issue been resolved at this point?
Brian > relevant snippets from a class I'm using ... > > > // assumes quality is a set as a member of the > class > // and File srcFile, String destJpgName and > String destMaskName > // are member variables also > // so I'd create an instance of the class, set the > quality then > // call this create(File src) passing a file > that is a png. > > ublic void create(File src) throws IOException { > > this.srcFile = src; > String ext = > FilenameUtils.getExtension(src.getAbsolutePath()); > this.destJpgName = > src.getAbsolutePath().replaceFirst("\\." +ext + "$", > ".jpg"); > this.destMaskName = > src.getAbsolutePath().replaceFirst("\\." +ext + "$", > "Mask.png"); > > if(has4BandsWithAlpha(src)) { > BufferedImage inputBufferedImage = > ImageIO.read(srcFile); > createJpgForMask(inputBufferedImage); > createMask(inputBufferedImage); > } > else { > createNormalJpg(); > } > > > /* > * for now this should only be called with ones > we already know meet the > * 4 bands and alpha channel test! > */ > private void createJpgForMask(BufferedImage > input) throws IOException { > > // all png's with alpha channel should by > definition be non premultiplied > // so to create the jpg we need to coerce it > to be premultiplied > input.coerceData(true); > > int[] bandIndices; > bandIndices = new int[3]; > bandIndices[0] = 0; > bandIndices[1] = 1; > bandIndices[2] = 2; > ParameterBlock pb = new ParameterBlock(); > pb.addSource(input); > pb.add(bandIndices); > RenderedImage dst = > (RenderedImage)JAI.create("bandSelect",pb); > compressAndWrite(dst); > > > private void createNormalJpg() throws IOException > { > // when used BufferedImage inputBufferedImage > = ImageIO.read(srcFile); to read in, > // it didn't come out in correct colors but > with JAI.create it did. ??? > RenderedImage dst = JAI.create("fileload", > srcFile.getAbsolutePath()); > compressAndWrite(dst); > > > private void compressAndWrite(RenderedImage img) > throws IOException { > > // would use if didn't want to compress > first > // JAI.create("filestore", dst, destJpgName, > "JPEG"); > // next code is longer but saves writing to > file and reading back in again > > ImageOutputStream ios = null; > ImageWriter writer = null; > try { > writer = (ImageWriter) > ImageIO.getImageWritersByFormatName("jpg").next(); > // we assume one is present > ImageWriteParam iwparam = new > JPEGImageWriteParam(Locale.getDefault()); > > wparam.setCompressionMode(JPEGImageWriteParam.MODE_EXP > LICIT) ; > iwparam.setCompressionQuality(quality); > ios = ImageIO.createImageOutputStream(new > File(destJpgName)); > writer.setOutput(ios); > writer.write(null, new IIOImage(img, null, null), > iwparam); > > } finally { > try { > ios.flush(); > writer.dispose(); > ios.close(); > }catch(Exception e) {} > } > > > /* > * for now this should only be called with ones we > already know meet the > * 4 bands and alpha channel test! > */ > private void createMask(BufferedImage input) > throws IOException { > int[] bandIndices; > bandIndices = new int[1]; > bandIndices[0] = 3; // index of alpha band > ParameterBlock pb = new ParameterBlock(); > pb.addSource(input); > pb.add(bandIndices); > RenderedImage dst = > (RenderedImage)JAI.create("bandSelect",pb); > JAI.create("filestore", dst, destMaskName, > "PNG"); > } > > > public static boolean has4BandsWithAlpha(File src) > throws IOException { > PlanarImage pi = JAI.create("fileload", > src.getAbsolutePath()); > SampleModel sm = pi.getSampleModel(); > if( sm.getNumBands() == 4 && > pi.getColorModel().hasAlpha() ) > return true; > e > return false; http://forums.java.net/jive/thread.jspa?messageID=277097 --------------------------------------------------------------------- To unsubscribe, e-mail: interest-unsubscribe@... For additional commands, e-mail: interest-help@... |
| Free Forum Powered by Nabble | Forum Help |