12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.javafx.iio.common;
27
28 import com.sun.javafx.geom.Point2D;
29 import com.sun.javafx.geom.Rectangle;
30 import com.sun.javafx.iio.ImageFrame;
31 import com.sun.javafx.iio.ImageMetadata;
32 import com.sun.javafx.iio.ImageStorage.ImageType;
33 import java.io.EOFException;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.net.URL;
39 import java.nio.Buffer;
40 import java.nio.ByteBuffer;
41
42 /**
43 * A set of format-independent convenience methods useful in image loading
44 * and saving.
45 */
46 public class ImageTools {
47
48 /**
49 * The percentage increment between progress report updates.
50 */
51 public static final int PROGRESS_INTERVAL = 5;
675 finalHeight = sourceHeight;
676 }
677 if (finalWidth == 0) {
678 finalWidth = sourceWidth;
679 }
680 }
681
682
683 // clamp dimensions to positive values
684 if (finalWidth == 0) {
685 finalWidth = 1;
686 }
687 if (finalHeight == 0) {
688 finalHeight = 1;
689 }
690 }
691
692
693 return new int[]{finalWidth, finalHeight};
694 }
695 // public static final java.awt.image.BufferedImage getAsBufferedImage(Image prismImage) {
696 // java.awt.image.BufferedImage image = null;
697 //
698 // int width = prismImage.getWidth();
699 // int height = prismImage.getHeight();
700 // int scanlineStride = prismImage.getScanlineStride();
701 // byte[] pixels = ((java.nio.ByteBuffer) prismImage.getPixelBuffer()).array();
702 // switch (prismImage.getPixelFormat()) {
703 // case BYTE_GRAY: {
704 // image = new java.awt.image.BufferedImage(width, height,
705 // java.awt.image.BufferedImage.TYPE_BYTE_GRAY);
706 // java.awt.image.DataBufferByte db =
707 // (java.awt.image.DataBufferByte) image.getRaster().getDataBuffer();
708 // byte[] data = db.getData();
709 // System.arraycopy(pixels, 0, data, 0, width * height);
710 // }
711 // break;
712 // case BYTE_RGB: {
713 // image = new java.awt.image.BufferedImage(width, height,
714 // java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.javafx.iio.common;
27
28 import com.sun.javafx.geom.Point2D;
29 import com.sun.javafx.geom.Rectangle;
30 import com.sun.javafx.iio.ImageFrame;
31 import com.sun.javafx.iio.ImageMetadata;
32 import com.sun.javafx.iio.ImageStorage;
33 import com.sun.javafx.iio.ImageStorage.ImageType;
34 import java.io.EOFException;
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.URL;
40 import java.nio.Buffer;
41 import java.nio.ByteBuffer;
42
43 /**
44 * A set of format-independent convenience methods useful in image loading
45 * and saving.
46 */
47 public class ImageTools {
48
49 /**
50 * The percentage increment between progress report updates.
51 */
52 public static final int PROGRESS_INTERVAL = 5;
676 finalHeight = sourceHeight;
677 }
678 if (finalWidth == 0) {
679 finalWidth = sourceWidth;
680 }
681 }
682
683
684 // clamp dimensions to positive values
685 if (finalWidth == 0) {
686 finalWidth = 1;
687 }
688 if (finalHeight == 0) {
689 finalHeight = 1;
690 }
691 }
692
693
694 return new int[]{finalWidth, finalHeight};
695 }
696
697 public static ImageFrame scaleImageFrame(ImageFrame src,
698 int destWidth, int destHeight, boolean isSmooth)
699 {
700 int numBands = ImageStorage.getNumBands(src.getImageType());
701 ByteBuffer dst = scaleImage((ByteBuffer) src.getImageData(),
702 src.getWidth(), src.getHeight(), numBands,
703 destWidth, destHeight, isSmooth);
704 return new ImageFrame(src.getImageType(), dst,
705 destWidth, destHeight, destWidth * numBands, null, src.getMetadata());
706 }
707
708 public static ByteBuffer scaleImage(ByteBuffer src,
709 int sourceWidth, int sourceHeight, int numBands,
710 int destWidth, int destHeight, boolean isSmooth)
711 {
712 PushbroomScaler scaler = ScalerFactory.createScaler(
713 sourceWidth, sourceHeight, numBands,
714 destWidth, destHeight, isSmooth);
715
716 int stride = sourceWidth * numBands;
717 if (src.hasArray()) {
718 byte image[] = src.array();
719 for (int y = 0; y != sourceHeight; ++y) {
720 scaler.putSourceScanline(image, y * stride);
721 }
722 } else {
723 byte scanline[] = new byte[stride];
724 for (int y = 0; y != sourceHeight; ++y) {
725 src.get(scanline);
726 scaler.putSourceScanline(scanline, 0);
727 }
728 }
729
730 return scaler.getDestination();
731 }
732 // public static final java.awt.image.BufferedImage getAsBufferedImage(Image prismImage) {
733 // java.awt.image.BufferedImage image = null;
734 //
735 // int width = prismImage.getWidth();
736 // int height = prismImage.getHeight();
737 // int scanlineStride = prismImage.getScanlineStride();
738 // byte[] pixels = ((java.nio.ByteBuffer) prismImage.getPixelBuffer()).array();
739 // switch (prismImage.getPixelFormat()) {
740 // case BYTE_GRAY: {
741 // image = new java.awt.image.BufferedImage(width, height,
742 // java.awt.image.BufferedImage.TYPE_BYTE_GRAY);
743 // java.awt.image.DataBufferByte db =
744 // (java.awt.image.DataBufferByte) image.getRaster().getDataBuffer();
745 // byte[] data = db.getData();
746 // System.arraycopy(pixels, 0, data, 0, width * height);
747 // }
748 // break;
749 // case BYTE_RGB: {
750 // image = new java.awt.image.BufferedImage(width, height,
751 // java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
|