1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 package org.jemmy.image;
  26 
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.IOException;
  30 import org.jemmy.Dimension;
  31 import org.jemmy.image.pixel.PNGLoader;
  32 import org.jemmy.image.pixel.PNGSaver;
  33 import org.jemmy.image.pixel.Raster;
  34 import org.jemmy.image.pixel.WriteableRaster;
  35 import org.testng.annotations.AfterClass;
  36 import org.testng.annotations.AfterMethod;
  37 import org.testng.annotations.BeforeClass;
  38 import org.testng.annotations.BeforeMethod;
  39 import org.testng.annotations.Test;
  40 
  41 import static org.testng.Assert.fail;
  42 
  43 
  44 /**
  45  *
  46  * @author shura
  47  */
  48 public class SaveLoadImageTest {
  49 
  50     public SaveLoadImageTest() {
  51     }
  52 
  53     @BeforeClass
  54     public static void setUpClass() throws Exception {
  55     }
  56 
  57     @AfterClass
  58     public static void tearDownClass() throws Exception {
  59     }
  60 
  61     @BeforeMethod
  62     public void setUp() {
  63     }
  64 
  65     @AfterMethod
  66     public void tearDown() {
  67     }
  68     @Test
  69     public void hello() throws IOException {
  70         int [][][] data = new int[99][99][3];
  71         for (int i = 0; i < data.length; i++) {
  72             for (int j = 0; j < data[i].length; j++) {
  73                 for (int k = 0; k < data[i][j].length; k++) {
  74                     data[i][j][k] = 0;
  75                 }
  76                 data[i][j][i%3] = (int)((((double)j)/data[i].length)*0xFF);
  77             }
  78         }
  79         Raster img = new RasterImpl(data.length, data[0].length, data);
  80         File out = new File(System.getProperty("user.dir") + File.separator + "out.png");
  81         new PNGSaver(out).encode(img);
  82         Raster res = new PNGLoader(new FileInputStream(out)) {
  83 
  84             @Override
  85             protected WriteableRaster createRaster(int width, int height) {
  86                 return new RasterImpl(width, height);
  87             }
  88         }.decode();
  89         out = new File(System.getProperty("user.dir") + File.separator + "out2.png");
  90         new PNGSaver(out).encode(img); //jusr for a reference
  91         double[] oColors = new double[3];
  92         double[] rColors = new double[3];
  93         for (int i = 0; i < data.length; i++) {
  94             for (int j = 0; j < data[i].length; j++) {
  95                 img.getColors(i, j, oColors);
  96                 res.getColors(i, j, rColors);
  97                 for (int k = 0; k < oColors.length; k++) {
  98                     if(oColors[k] != rColors[k]) {
  99                         System.out.println("Different as (" + i + "," + j + "),"+k+":");
 100                         System.out.println(oColors[k] +" != "+ rColors[k]);
 101                         fail();
 102                     }
 103                 }
 104             }
 105         }
 106     }
 107 
 108     private class RasterImpl implements WriteableRaster {
 109 
 110 
 111         Component[] RGB = new Component[] {Component.RED, Component.GREEN, Component.BLUE};
 112         Dimension size;
 113         int[][][] data;
 114 
 115         public RasterImpl(int w, int h) {
 116             size = new Dimension(w, h);
 117             data = new int[w][h][RGB.length];
 118         }
 119 
 120         public RasterImpl(int w, int h, int[][][] data) {
 121             size = new Dimension(w, h);
 122             this.data = data;
 123         }
 124 
 125         public Dimension getSize() {
 126             return size;
 127         }
 128 
 129         public void getColors(int x, int y, double[] colors) {
 130             for (int i = 0; i < colors.length; i++) {
 131                 colors[i] = (double)data[x][y][i]/0xFF;
 132             }
 133         }
 134 
 135         public Component[] getSupported() {
 136             return RGB;
 137         }
 138 
 139         public void setColors(int x, int y, double[] values) {
 140             for (int i = 0; i < values.length; i++) {
 141                 data[x][y][i] = (int)(values[i] * 0xFF);
 142             }
 143         }
 144     }
 145 }