Search in sources :

Example 61 with WritableRaster

use of java.awt.image.WritableRaster in project libgdx by libgdx.

the class BMFontUtil method save.

public void save(File outputBMFontFile) throws IOException {
    File outputDir = outputBMFontFile.getParentFile();
    String outputName = outputBMFontFile.getName();
    if (outputName.endsWith(".fnt"))
        outputName = outputName.substring(0, outputName.length() - 4);
    // Always include space and the missing gyph.
    getGlyph(' ');
    getGlyph('');
    unicodeFont.loadGlyphs();
    PrintStream out = new PrintStream(new FileOutputStream(new File(outputDir, outputName + ".fnt")));
    Font font = unicodeFont.getFont();
    int pageWidth = unicodeFont.getGlyphPageWidth();
    int pageHeight = unicodeFont.getGlyphPageHeight();
    out.println("info face=\"" + font.getFontName() + "\" size=" + font.getSize() + " bold=" + (font.isBold() ? 1 : 0) + " italic=" + (font.isItalic() ? 1 : 0) + " charset=\"\" unicode=0 stretchH=100 smooth=1 aa=1 padding=" + unicodeFont.getPaddingTop() + "," + unicodeFont.getPaddingRight() + "," + unicodeFont.getPaddingBottom() + "," + unicodeFont.getPaddingLeft() + " spacing=" + unicodeFont.getPaddingAdvanceX() + "," + unicodeFont.getPaddingAdvanceY());
    out.println("common lineHeight=" + unicodeFont.getLineHeight() + " base=" + unicodeFont.getAscent() + " scaleW=" + pageWidth + " scaleH=" + pageHeight + " pages=" + unicodeFont.getGlyphPages().size() + " packed=0");
    int pageIndex = 0, glyphCount = 0;
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        String fileName;
        if (pageIndex == 0 && !pageIter.hasNext())
            fileName = outputName + ".png";
        else
            fileName = outputName + (pageIndex + 1) + ".png";
        out.println("page id=" + pageIndex + " file=\"" + fileName + "\"");
        glyphCount += page.getGlyphs().size();
        pageIndex++;
    }
    out.println("chars count=" + glyphCount);
    pageIndex = 0;
    List allGlyphs = new ArrayList(512);
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        List<Glyph> glyphs = page.getGlyphs();
        Collections.sort(glyphs, new Comparator<Glyph>() {

            public int compare(Glyph o1, Glyph o2) {
                return o1.getCodePoint() - o2.getCodePoint();
            }
        });
        for (Iterator glyphIter = page.getGlyphs().iterator(); glyphIter.hasNext(); ) {
            Glyph glyph = (Glyph) glyphIter.next();
            writeGlyph(out, pageWidth, pageHeight, pageIndex, glyph);
        }
        allGlyphs.addAll(page.getGlyphs());
        pageIndex++;
    }
    String ttfFileRef = unicodeFont.getFontFile();
    if (ttfFileRef == null)
        System.out.println("Kerning information could not be output because a TTF font file was not specified.");
    else {
        Kerning kerning = new Kerning();
        try {
            kerning.load(Gdx.files.internal(ttfFileRef).read(), font.getSize());
        } catch (IOException ex) {
            System.out.println("Unable to read kerning information from font: " + ttfFileRef);
            ex.printStackTrace();
        }
        IntIntMap glyphCodeToCodePoint = new IntIntMap();
        for (Iterator iter = allGlyphs.iterator(); iter.hasNext(); ) {
            Glyph glyph = (Glyph) iter.next();
            glyphCodeToCodePoint.put(new Integer(getGlyphCode(font, glyph.getCodePoint())), new Integer(glyph.getCodePoint()));
        }
        List kernings = new ArrayList(256);
        class KerningPair {

            public int firstCodePoint, secondCodePoint, offset;
        }
        for (IntIntMap.Entry entry : kerning.getKernings()) {
            int firstGlyphCode = entry.key >> 16;
            int secondGlyphCode = entry.key & 0xffff;
            int offset = entry.value;
            int firstCodePoint = glyphCodeToCodePoint.get(firstGlyphCode, -1);
            int secondCodePoint = glyphCodeToCodePoint.get(secondGlyphCode, -1);
            if (firstCodePoint == -1 || secondCodePoint == -1 || offset == 0) {
                // We are not outputting one or both of these glyphs, or the offset is zero anyway.
                continue;
            }
            KerningPair pair = new KerningPair();
            pair.firstCodePoint = firstCodePoint;
            pair.secondCodePoint = secondCodePoint;
            pair.offset = offset;
            kernings.add(pair);
        }
        out.println("kernings count=" + kernings.size());
        for (Iterator iter = kernings.iterator(); iter.hasNext(); ) {
            KerningPair pair = (KerningPair) iter.next();
            out.println("kerning first=" + pair.firstCodePoint + " second=" + pair.secondCodePoint + " amount=" + pair.offset);
        }
    }
    out.close();
    int width = unicodeFont.getGlyphPageWidth();
    int height = unicodeFont.getGlyphPageHeight();
    IntBuffer buffer = BufferUtils.createIntBuffer(width * height);
    BufferedImage pageImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    int[] row = new int[width];
    pageIndex = 0;
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        String fileName;
        if (pageIndex == 0 && !pageIter.hasNext())
            fileName = outputName + ".png";
        else
            fileName = outputName + (pageIndex + 1) + ".png";
        page.getTexture().bind();
        buffer.clear();
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buffer);
        WritableRaster raster = pageImage.getRaster();
        for (int y = 0; y < height; y++) {
            buffer.get(row);
            raster.setDataElements(0, y, width, 1, row);
        }
        File imageOutputFile = new File(outputDir, fileName);
        ImageIO.write(pageImage, "png", imageOutputFile);
        pageIndex++;
    }
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Font(java.awt.Font) UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) BufferedImage(java.awt.image.BufferedImage) IntIntMap(com.badlogic.gdx.utils.IntIntMap) GlyphPage(com.badlogic.gdx.tools.hiero.unicodefont.GlyphPage) WritableRaster(java.awt.image.WritableRaster) FileOutputStream(java.io.FileOutputStream) Glyph(com.badlogic.gdx.tools.hiero.unicodefont.Glyph) IntBuffer(java.nio.IntBuffer) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 62 with WritableRaster

use of java.awt.image.WritableRaster in project libgdx by libgdx.

the class ImageProcessor method hash.

private static String hash(BufferedImage image) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        // Ensure image is the correct format.
        int width = image.getWidth();
        int height = image.getHeight();
        if (image.getType() != BufferedImage.TYPE_INT_ARGB) {
            BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            newImage.getGraphics().drawImage(image, 0, 0, null);
            image = newImage;
        }
        WritableRaster raster = image.getRaster();
        int[] pixels = new int[width];
        for (int y = 0; y < height; y++) {
            raster.getDataElements(0, y, width, 1, pixels);
            for (int x = 0; x < width; x++) hash(digest, pixels[x]);
        }
        hash(digest, width);
        hash(digest, height);
        return new BigInteger(1, digest.digest()).toString(16);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) BufferedImage(java.awt.image.BufferedImage)

Example 63 with WritableRaster

use of java.awt.image.WritableRaster in project libgdx by libgdx.

the class ImageProcessor method getSplits.

/** Returns the splits, or null if the image had no splits or the splits were only a single region. Splits are an int[4] that
	 * has left, right, top, bottom. */
private int[] getSplits(BufferedImage image, String name) {
    WritableRaster raster = image.getRaster();
    int startX = getSplitPoint(raster, name, 1, 0, true, true);
    int endX = getSplitPoint(raster, name, startX, 0, false, true);
    int startY = getSplitPoint(raster, name, 0, 1, true, false);
    int endY = getSplitPoint(raster, name, 0, startY, false, false);
    // Ensure pixels after the end are not invalid.
    getSplitPoint(raster, name, endX + 1, 0, true, true);
    getSplitPoint(raster, name, 0, endY + 1, true, false);
    // No splits, or all splits.
    if (startX == 0 && endX == 0 && startY == 0 && endY == 0)
        return null;
    // Subtraction here is because the coordinates were computed before the 1px border was stripped.
    if (startX != 0) {
        startX--;
        endX = raster.getWidth() - 2 - (endX - 1);
    } else {
        // If no start point was ever found, we assume full stretch.
        endX = raster.getWidth() - 2;
    }
    if (startY != 0) {
        startY--;
        endY = raster.getHeight() - 2 - (endY - 1);
    } else {
        // If no start point was ever found, we assume full stretch.
        endY = raster.getHeight() - 2;
    }
    if (scale != 1) {
        startX = (int) Math.round(startX * scale);
        endX = (int) Math.round(endX * scale);
        startY = (int) Math.round(startY * scale);
        endY = (int) Math.round(endY * scale);
    }
    return new int[] { startX, endX, startY, endY };
}
Also used : WritableRaster(java.awt.image.WritableRaster)

Example 64 with WritableRaster

use of java.awt.image.WritableRaster in project libgdx by libgdx.

the class ImageProcessor method stripWhitespace.

/** Strips whitespace and returns the rect, or null if the image should be ignored. */
private Rect stripWhitespace(BufferedImage source) {
    WritableRaster alphaRaster = source.getAlphaRaster();
    if (alphaRaster == null || (!settings.stripWhitespaceX && !settings.stripWhitespaceY))
        return new Rect(source, 0, 0, source.getWidth(), source.getHeight(), false);
    final byte[] a = new byte[1];
    int top = 0;
    int bottom = source.getHeight();
    if (settings.stripWhitespaceX) {
        outer: for (int y = 0; y < source.getHeight(); y++) {
            for (int x = 0; x < source.getWidth(); x++) {
                alphaRaster.getDataElements(x, y, a);
                int alpha = a[0];
                if (alpha < 0)
                    alpha += 256;
                if (alpha > settings.alphaThreshold)
                    break outer;
            }
            top++;
        }
        outer: for (int y = source.getHeight(); --y >= top; ) {
            for (int x = 0; x < source.getWidth(); x++) {
                alphaRaster.getDataElements(x, y, a);
                int alpha = a[0];
                if (alpha < 0)
                    alpha += 256;
                if (alpha > settings.alphaThreshold)
                    break outer;
            }
            bottom--;
        }
        // Leave 1px so nothing is copied into padding.
        if (settings.duplicatePadding) {
            if (top > 0)
                top--;
            if (bottom < source.getHeight())
                bottom++;
        }
    }
    int left = 0;
    int right = source.getWidth();
    if (settings.stripWhitespaceY) {
        outer: for (int x = 0; x < source.getWidth(); x++) {
            for (int y = top; y < bottom; y++) {
                alphaRaster.getDataElements(x, y, a);
                int alpha = a[0];
                if (alpha < 0)
                    alpha += 256;
                if (alpha > settings.alphaThreshold)
                    break outer;
            }
            left++;
        }
        outer: for (int x = source.getWidth(); --x >= left; ) {
            for (int y = top; y < bottom; y++) {
                alphaRaster.getDataElements(x, y, a);
                int alpha = a[0];
                if (alpha < 0)
                    alpha += 256;
                if (alpha > settings.alphaThreshold)
                    break outer;
            }
            right--;
        }
        // Leave 1px so nothing is copied into padding.
        if (settings.duplicatePadding) {
            if (left > 0)
                left--;
            if (right < source.getWidth())
                right++;
        }
    }
    int newWidth = right - left;
    int newHeight = bottom - top;
    if (newWidth <= 0 || newHeight <= 0) {
        if (settings.ignoreBlankImages)
            return null;
        else
            return new Rect(emptyImage, 0, 0, 1, 1, false);
    }
    return new Rect(source, left, top, newWidth, newHeight, false);
}
Also used : Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) WritableRaster(java.awt.image.WritableRaster)

Example 65 with WritableRaster

use of java.awt.image.WritableRaster in project deeplearning4j by deeplearning4j.

the class DrawReconstruction method draw.

public void draw() {
    WritableRaster r = img.getRaster();
    int[] equiv = new int[data.length()];
    INDArray dataLinear = data.linearView();
    for (int i = 0; i < equiv.length; i++) equiv[i] = Math.round(dataLinear.getInt(i));
    r.setDataElements(0, 0, width, height, equiv);
    frame = new JFrame(title);
    frame.setVisible(true);
    start();
    frame.add(new JLabel(new ImageIcon(getImage())));
    frame.pack();
    // Better to DISPOSE than EXIT
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WritableRaster(java.awt.image.WritableRaster)

Aggregations

WritableRaster (java.awt.image.WritableRaster)110 BufferedImage (java.awt.image.BufferedImage)74 ColorModel (java.awt.image.ColorModel)33 DirectColorModel (java.awt.image.DirectColorModel)20 IndexColorModel (java.awt.image.IndexColorModel)19 DataBufferByte (java.awt.image.DataBufferByte)17 Point (java.awt.Point)15 Raster (java.awt.image.Raster)15 Rectangle (java.awt.Rectangle)13 Graphics2D (java.awt.Graphics2D)12 ComponentColorModel (java.awt.image.ComponentColorModel)12 DataBufferInt (java.awt.image.DataBufferInt)10 SampleModel (java.awt.image.SampleModel)9 DataBuffer (java.awt.image.DataBuffer)8 IOException (java.io.IOException)8 ColorSpace (java.awt.color.ColorSpace)7 Color (java.awt.Color)6 Iterator (java.util.Iterator)6 AffineTransform (java.awt.geom.AffineTransform)5 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)5