Search in sources :

Example 1 with GvrsFile

use of org.gridfour.gvrs.GvrsFile in project gridfour by gwlucastrig.

the class ExperimentalImageStorage method main.

/**
 * Process the specified file storing it in different image formats
 * as a way of testing data processing concepts for GVRS.
 *
 * @param args the command line arguments, the first of which give the path
 * to the input file
 * @throws java.io.IOException in the event of an unhandled IO exception
 * @throws org.apache.commons.imaging.ImageReadException in the event of an
 * unhandled exception reading an image
 */
public static void main(String[] args) throws IOException, ImageReadException {
    File input = new File(args[0]);
    Date date = new Date();
    SimpleDateFormat sdFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
    sdFormat.setTimeZone(new SimpleTimeZone(0, "UTC"));
    System.out.format("Processing image from %s%n", input.getName());
    System.out.format("Date/time of test: %s (UTC)%n", sdFormat.format(date));
    System.out.println("");
    long time0, time1;
    // -------------------------------------------------------------
    // Load the specified file to obtain sample data for processing.
    time0 = System.currentTimeMillis();
    BufferedImage bImage = Imaging.getBufferedImage(input);
    time1 = System.currentTimeMillis();
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    int nPixels = width * height;
    // GVRS API uses row, column as grid coordinates
    int nRows = height;
    int nCols = width;
    System.out.println("Image loaded");
    System.out.format("   Width:                 %7d%n", width);
    System.out.format("   Height:                %7d%n", height);
    report(time0, time1, input, nPixels);
    int[] argb = new int[width * height];
    bImage.getRGB(0, 0, width, height, argb, 0, width);
    bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    bImage.setRGB(0, 0, width, height, argb, 0, width);
    // ---------------------------------------------------------------
    // As a basis for comparison, store the image as a PNG
    // and report the time required to do so.
    File refPNG = new File("ReferenceImage.png");
    if (refPNG.exists()) {
        refPNG.delete();
    }
    time0 = System.currentTimeMillis();
    ImageIO.write(bImage, "PNG", refPNG);
    time1 = System.currentTimeMillis();
    System.out.println("ImageIO writing PNG");
    report(time0, time1, refPNG, nPixels);
    // ---------------------------------------------------------------
    File refJPEG = new File("ReferenceImage.jpg");
    if (refJPEG.exists()) {
        refJPEG.delete();
    }
    time0 = System.currentTimeMillis();
    ImageIO.write(bImage, "JPEG", refJPEG);
    time1 = System.currentTimeMillis();
    System.out.println("ImageIO writing JPEG");
    report(time0, time1, refJPEG, nPixels);
    // Note:
    // In the following code blocks, there are references to the GvrsFile
    // summarize() method.  These are commented out because they would
    // interfere with timing measurements and also because they produce
    // considerable output text which would clutter the report.
    // Note also that calls to flush() are not normally required since
    // GvrsFile performs a flush as part of its close() operation.
    // ---------------------------------------------------------------
    GvrsFileSpecification gvrsFileSpec;
    // The first test stores the specified data in an uncompressed format.
    // This is the fastest option for processing pixel data and is recommended
    // for high-performance processing.
    System.out.println("Storing pixels as integers in uncompressed GVRS file");
    gvrsFileSpec = new GvrsFileSpecification(nRows, nCols, 200, 200);
    GvrsElementSpecification pSpec = new GvrsElementSpecificationInt("pixel");
    gvrsFileSpec.addElementSpecification(pSpec);
    File output0 = new File("IntPixelNoComp.gvrs");
    time0 = System.currentTimeMillis();
    try (GvrsFile gvrs = new GvrsFile(output0, gvrsFileSpec)) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        GvrsElement pixel = gvrs.getElement("pixel");
        for (int iRow = 0; iRow < nRows; iRow++) {
            for (int iCol = 0; iCol < nCols; iCol++) {
                int index = iRow * nCols + iCol;
                pixel.writeValueInt(iRow, iCol, argb[index]);
            }
        }
    }
    time1 = System.currentTimeMillis();
    report(time0, time1, output0, nPixels);
    // ---------------------------------------------------------------
    // Store the pixels in compressed format, but do not make any special
    // processing to improve the results. In most cases, compression using
    // this approach will not yield a substantial saving in storage.
    System.out.println("Storing pixels as integers in compressed GVRS file");
    File output1 = new File("IntPixelComp.gvrs");
    gvrsFileSpec.setDataCompressionEnabled(true);
    LsCodecUtility.addLsopToSpecification(gvrsFileSpec, true);
    time0 = System.currentTimeMillis();
    try (GvrsFile gvrs = new GvrsFile(output1, gvrsFileSpec)) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        GvrsElement pixel = gvrs.getElement("pixel");
        for (int iRow = 0; iRow < nRows; iRow++) {
            for (int iCol = 0; iCol < nCols; iCol++) {
                int index = iRow * nCols + iCol;
                pixel.writeValueInt(iRow, iCol, argb[index]);
            }
        }
    // gvrs.flush();
    // gvrs.summarize(System.out, true);
    }
    time1 = System.currentTimeMillis();
    report(time0, time1, output1, nPixels);
    // ---------------------------------------------------------------
    // Separate the pixels into separate RGB components, store each component
    // in a separate GVRS Element. This approach should improve compression
    // ratios.
    System.out.println("Storing RGB components in compressed GVRS file");
    gvrsFileSpec = new GvrsFileSpecification(nRows, nCols, 200, 200);
    gvrsFileSpec.setDataCompressionEnabled(true);
    LsCodecUtility.addLsopToSpecification(gvrsFileSpec, true);
    gvrsFileSpec.addElementSpecification(new GvrsElementSpecificationInt("r"));
    gvrsFileSpec.addElementSpecification(new GvrsElementSpecificationInt("g"));
    gvrsFileSpec.addElementSpecification(new GvrsElementSpecificationInt("b"));
    File output2 = new File("PixelsCompRGB.gvrs");
    gvrsFileSpec.setDataCompressionEnabled(true);
    time0 = System.currentTimeMillis();
    try (GvrsFile gvrs = new GvrsFile(output2, gvrsFileSpec)) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        GvrsElement rElem = gvrs.getElement("r");
        GvrsElement gElem = gvrs.getElement("g");
        GvrsElement bElem = gvrs.getElement("b");
        for (int iRow = 0; iRow < nRows; iRow++) {
            for (int iCol = 0; iCol < nCols; iCol++) {
                int rgb = argb[iRow * nCols + iCol];
                int r = (rgb >> 16) & 0xff;
                int g = (rgb >> 8) & 0xff;
                int b = rgb & 0xff;
                rElem.writeValueInt(iRow, iCol, r);
                gElem.writeValueInt(iRow, iCol, g);
                bElem.writeValueInt(iRow, iCol, b);
            }
        }
    // gvrs.flush();
    // gvrs.summarize(System.out, true);
    }
    time1 = System.currentTimeMillis();
    report(time0, time1, output2, nPixels);
    // ---------------------------------------------------------------
    // Convert RGB color values to the YCoCg-R color space before storage.
    // For photographic images, this approach should further reduce storage
    // size.  For charts, graphs, line drawings, and other such graphic art
    // this approach will usually not produce a gain and sometimes degrades
    // compression.
    System.out.println("Storing YCoCg-R components in compressed GVRS file");
    gvrsFileSpec = new GvrsFileSpecification(nRows, nCols, 200, 200);
    gvrsFileSpec.setDataCompressionEnabled(true);
    LsCodecUtility.addLsopToSpecification(gvrsFileSpec, true);
    gvrsFileSpec.addElementSpecification(new GvrsElementSpecificationInt("Y"));
    gvrsFileSpec.addElementSpecification(new GvrsElementSpecificationInt("Co"));
    gvrsFileSpec.addElementSpecification(new GvrsElementSpecificationInt("Cg"));
    File output3 = new File("PixelsCompYCoCg.gvrs");
    time0 = System.currentTimeMillis();
    try (GvrsFile gvrs = new GvrsFile(output3, gvrsFileSpec)) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        GvrsElement YElem = gvrs.getElement("Y");
        GvrsElement CoElem = gvrs.getElement("Co");
        GvrsElement CgElem = gvrs.getElement("Cg");
        for (int iRow = 0; iRow < nRows; iRow++) {
            for (int iCol = 0; iCol < nCols; iCol++) {
                int rgb = argb[iRow * nCols + iCol];
                int r = (rgb >> 16) & 0xff;
                int g = (rgb >> 8) & 0xff;
                int b = rgb & 0xff;
                int Co = r - b;
                // Co>>1 is equivalent to Co/2
                int tmp = b + (Co >> 1);
                int Cg = g - tmp;
                int Y = tmp + (Cg >> 1);
                YElem.writeValueInt(iRow, iCol, Y);
                CoElem.writeValueInt(iRow, iCol, Co);
                CgElem.writeValueInt(iRow, iCol, Cg);
            }
        }
    // gvrs.flush();
    // gvrs.summarize(System.out, true);
    }
    time1 = System.currentTimeMillis();
    report(time0, time1, output3, nPixels);
    // ---------------------------------------------------------------
    // Finally, test the time required to load a YCoCg image
    // Then write the results to a JPEG file for inspection.
    // This test code also illustrates how the YCoCg values may be
    // mapped back to RGB.
    System.out.println("Testing time to read the YCoCg compressed file");
    time0 = System.currentTimeMillis();
    try (GvrsFile gvrs = new GvrsFile(output3, "r")) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        GvrsElement YElem = gvrs.getElement("Y");
        GvrsElement CoElem = gvrs.getElement("Co");
        GvrsElement CgElem = gvrs.getElement("Cg");
        for (int iRow = 0; iRow < nRows; iRow++) {
            for (int iCol = 0; iCol < nCols; iCol++) {
                int Y = YElem.readValueInt(iRow, iCol);
                int Co = CoElem.readValueInt(iRow, iCol);
                int Cg = CgElem.readValueInt(iRow, iCol);
                int tmp = Y - (Cg >> 1);
                int g = Cg + tmp;
                int b = tmp - (Co >> 1);
                int r = b + Co;
                argb[iRow * nCols + iCol] = (((0xff00 | r) << 8 | g) << 8) | b;
            }
        }
    }
    time1 = System.currentTimeMillis();
    report(time0, time1, output3, nPixels);
    File resultsJPEG = new File("ResultsForInspection.jpg");
    if (resultsJPEG.exists()) {
        resultsJPEG.delete();
    }
    bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    bImage.setRGB(0, 0, width, height, argb, 0, width);
    time0 = System.currentTimeMillis();
    ImageIO.write(bImage, "JPEG", resultsJPEG);
    time1 = System.currentTimeMillis();
    System.out.println("ImageIO writing JPEG");
}
Also used : GvrsElementSpecificationInt(org.gridfour.gvrs.GvrsElementSpecificationInt) GvrsElement(org.gridfour.gvrs.GvrsElement) Date(java.util.Date) BufferedImage(java.awt.image.BufferedImage) SimpleTimeZone(java.util.SimpleTimeZone) GvrsFileSpecification(org.gridfour.gvrs.GvrsFileSpecification) GvrsElementSpecification(org.gridfour.gvrs.GvrsElementSpecification) File(java.io.File) GvrsFile(org.gridfour.gvrs.GvrsFile) SimpleDateFormat(java.text.SimpleDateFormat) GvrsFile(org.gridfour.gvrs.GvrsFile)

Example 2 with GvrsFile

use of org.gridfour.gvrs.GvrsFile in project gridfour by gwlucastrig.

the class GvrsReadPerformance method testTileLoadTime.

void testTileLoadTime(PrintStream ps) throws IOException {
    try (GvrsFile gvrs = new GvrsFile(inputFile, "r")) {
        gvrs.setTileCacheSize(GvrsCacheSize.Small);
        List<GvrsElement> elementList = gvrs.getElements();
        GvrsElement element = elementList.get(0);
        double avgValue = 0;
        long nSample = 0;
        long time0 = System.nanoTime();
        double sum = 0;
        for (int iRow = 0; iRow < nRowsOfTiles; iRow++) {
            for (int iCol = 0; iCol < nColsOfTiles; iCol++) {
                int row0 = iRow * nRowsInTile;
                int col0 = iCol * nColsInTile;
                float sample = element.readValue(row0 + 1, col0 + 1);
                if (!Float.isNaN(sample)) {
                    sum += sample;
                    nSample++;
                }
            }
        }
        if (nSample > 0) {
            avgValue = sum / (double) nSample;
        }
        long time1 = System.nanoTime();
        double deltaT = (time1 - time0) / 1.0e+9;
        report(ps, "Tile Load", deltaT, avgValue, nSample);
    }
}
Also used : GvrsElement(org.gridfour.gvrs.GvrsElement) GvrsFile(org.gridfour.gvrs.GvrsFile)

Example 3 with GvrsFile

use of org.gridfour.gvrs.GvrsFile in project gridfour by gwlucastrig.

the class GvrsReadPerformance method testColumnMajorScan.

void testColumnMajorScan(PrintStream ps) throws IOException {
    try (GvrsFile gvrs = new GvrsFile(inputFile, "r")) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        List<GvrsElement> elementList = gvrs.getElements();
        GvrsElement element = elementList.get(0);
        GvrsElementType dType = element.getDataType();
        double avgValue = 0;
        long nSample = 0;
        long time0 = System.nanoTime();
        if (dType == GvrsElementType.INTEGER) {
            long sum = 0;
            for (int iCol = 0; iCol < nColsInRaster; iCol++) {
                for (int iRow = 0; iRow < nRowsInRaster; iRow++) {
                    int sample = element.readValueInt(iRow, iCol);
                    if (sample != Integer.MIN_VALUE) {
                        sum += sample;
                        nSample++;
                    }
                }
            }
            if (nSample > 0) {
                avgValue = (double) sum / (double) nSample;
            }
        } else {
            double sum = 0;
            for (int iCol = 0; iCol < nColsInRaster; iCol++) {
                for (int iRow = 0; iRow < nRowsInRaster; iRow++) {
                    float sample = element.readValue(iRow, iCol);
                    if (!Float.isNaN(sample)) {
                        sum += sample;
                        nSample++;
                    }
                }
            }
            if (nSample > 0) {
                avgValue = sum / (double) nSample;
            }
        }
        long time1 = System.nanoTime();
        double deltaT = (time1 - time0) / 1.0e+9;
        report(ps, "Column Major", deltaT, avgValue, nSample);
    }
}
Also used : GvrsElementType(org.gridfour.gvrs.GvrsElementType) GvrsElement(org.gridfour.gvrs.GvrsElement) GvrsFile(org.gridfour.gvrs.GvrsFile)

Example 4 with GvrsFile

use of org.gridfour.gvrs.GvrsFile in project gridfour by gwlucastrig.

the class GvrsReadPerformance method testRowMajorScan.

void testRowMajorScan(PrintStream ps) throws IOException {
    try (GvrsFile gvrs = new GvrsFile(inputFile, "r")) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        List<GvrsElement> elementList = gvrs.getElements();
        GvrsElement element = elementList.get(0);
        GvrsElementType dType = element.getDataType();
        double avgValue = 0;
        long nSample = 0;
        long time0 = System.nanoTime();
        if (dType == GvrsElementType.INTEGER) {
            long sum = 0;
            for (int iRow = 0; iRow < nRowsInRaster; iRow++) {
                for (int iCol = 0; iCol < nColsInRaster; iCol++) {
                    int sample = element.readValueInt(iRow, iCol);
                    if (sample != Integer.MIN_VALUE) {
                        sum += sample;
                        nSample++;
                    }
                }
            }
            if (nSample > 0) {
                avgValue = (double) sum / (double) nSample;
            }
        } else {
            double sum = 0;
            for (int iRow = 0; iRow < nRowsInRaster; iRow++) {
                for (int iCol = 0; iCol < nColsInRaster; iCol++) {
                    float sample = element.readValue(iRow, iCol);
                    if (!Float.isNaN(sample)) {
                        sum += sample;
                        nSample++;
                    }
                }
            }
            if (nSample > 0) {
                avgValue = sum / (double) nSample;
            }
        }
        long time1 = System.nanoTime();
        double deltaT = (time1 - time0) / 1.0e+9;
        report(ps, "Row Major", deltaT, avgValue, nSample);
    }
}
Also used : GvrsElementType(org.gridfour.gvrs.GvrsElementType) GvrsElement(org.gridfour.gvrs.GvrsElement) GvrsFile(org.gridfour.gvrs.GvrsFile)

Example 5 with GvrsFile

use of org.gridfour.gvrs.GvrsFile in project gridfour by gwlucastrig.

the class GvrsReadPerformance method testRowBlockScan.

void testRowBlockScan(PrintStream ps) throws IOException {
    try (GvrsFile gvrs = new GvrsFile(inputFile, "r")) {
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        List<GvrsElement> elementList = gvrs.getElements();
        GvrsElement element = elementList.get(0);
        GvrsElementType dType = element.getDataType();
        double avgValue = 0;
        long nSample = 0;
        long time0 = System.nanoTime();
        if (dType == GvrsElementType.INTEGER || dType == GvrsElementType.SHORT) {
            long sum = 0;
            for (int iRow = 0; iRow < nRowsInRaster; iRow++) {
                int[] block = element.readBlockInt(iRow, 0, 1, nColsInRaster);
                for (int iCol = 0; iCol < nColsInRaster; iCol++) {
                    int sample = block[iCol];
                    sum += sample;
                    nSample++;
                }
            }
            if (nSample > 0) {
                avgValue = (double) sum / (double) nSample;
            }
        } else {
            double sum = 0;
            for (int iRow = 0; iRow < nRowsInRaster; iRow++) {
                float[] block = element.readBlock(iRow, 0, 1, nColsInRaster);
                for (int iCol = 0; iCol < nColsInRaster; iCol++) {
                    float sample = block[iCol];
                    if (!Float.isNaN(sample)) {
                        sum += sample;
                        nSample++;
                    }
                }
            }
            if (nSample > 0) {
                avgValue = sum / (double) nSample;
            }
        }
        long time1 = System.nanoTime();
        double deltaT = (time1 - time0) / 1.0e+9;
        report(ps, "Row Block", deltaT, avgValue, nSample);
    }
}
Also used : GvrsElementType(org.gridfour.gvrs.GvrsElementType) GvrsElement(org.gridfour.gvrs.GvrsElement) GvrsFile(org.gridfour.gvrs.GvrsFile)

Aggregations

GvrsElement (org.gridfour.gvrs.GvrsElement)9 GvrsFile (org.gridfour.gvrs.GvrsFile)9 GvrsElementType (org.gridfour.gvrs.GvrsElementType)6 File (java.io.File)4 GvrsFileSpecification (org.gridfour.gvrs.GvrsFileSpecification)4 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 IOException (java.io.IOException)2 Locale (java.util.Locale)2 GvrsElementSpecification (org.gridfour.gvrs.GvrsElementSpecification)2 GvrsElementSpecificationInt (org.gridfour.gvrs.GvrsElementSpecificationInt)2 GvrsMetadata (org.gridfour.gvrs.GvrsMetadata)2 BufferedImage (java.awt.image.BufferedImage)1 PrintStream (java.io.PrintStream)1 SimpleTimeZone (java.util.SimpleTimeZone)1 GvrsElementSpecificationFloat (org.gridfour.gvrs.GvrsElementSpecificationFloat)1 GvrsElementSpecificationIntCodedFloat (org.gridfour.gvrs.GvrsElementSpecificationIntCodedFloat)1 GvrsElementSpecificationShort (org.gridfour.gvrs.GvrsElementSpecificationShort)1 KahanSummation (org.gridfour.util.KahanSummation)1 Array (ucar.ma2.Array)1