com.badlogic.gdx.utils.ByteArray Java Examples
The following examples show how to use
com.badlogic.gdx.utils.ByteArray.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PaletteReducer.java From gdx-texture-packer-gui with Apache License 2.0 | 4 votes |
/** * Modifies the given Pixmap so it only uses colors present in this PaletteReducer, dithering when it can. * If you want to reduce the colors in a Pixmap based on what it currently contains, call * {@link #analyze(Pixmap)} with {@code pixmap} as its argument, then call this method with the same * Pixmap. You may instead want to use a known palette instead of one computed from a Pixmap; * {@link #exact(int[])} is the tool for that job. * <br> * This method is not incredibly fast because of the extra calculations it has to do for dithering, but if you can * compute the PaletteReducer once and reuse it, that will save some time. * @param pixmap a Pixmap that will be modified in place * @return the given Pixmap, for chaining */ public Pixmap reduce (Pixmap pixmap) { boolean hasTransparent = (paletteArray[0] == 0); final int lineLen = pixmap.getWidth(), h = pixmap.getHeight(); byte[] curErrorRed, nextErrorRed, curErrorGreen, nextErrorGreen, curErrorBlue, nextErrorBlue; if (curErrorRedBytes == null) { curErrorRed = (curErrorRedBytes = new ByteArray(lineLen)).items; nextErrorRed = (nextErrorRedBytes = new ByteArray(lineLen)).items; curErrorGreen = (curErrorGreenBytes = new ByteArray(lineLen)).items; nextErrorGreen = (nextErrorGreenBytes = new ByteArray(lineLen)).items; curErrorBlue = (curErrorBlueBytes = new ByteArray(lineLen)).items; nextErrorBlue = (nextErrorBlueBytes = new ByteArray(lineLen)).items; } else { curErrorRed = curErrorRedBytes.ensureCapacity(lineLen); nextErrorRed = nextErrorRedBytes.ensureCapacity(lineLen); curErrorGreen = curErrorGreenBytes.ensureCapacity(lineLen); nextErrorGreen = nextErrorGreenBytes.ensureCapacity(lineLen); curErrorBlue = curErrorBlueBytes.ensureCapacity(lineLen); nextErrorBlue = nextErrorBlueBytes.ensureCapacity(lineLen); for (int i = 0; i < lineLen; i++) { nextErrorRed[i] = 0; nextErrorGreen[i] = 0; nextErrorBlue[i] = 0; } } Pixmap.Blending blending = pixmap.getBlending(); pixmap.setBlending(Pixmap.Blending.None); int color, used, rdiff, gdiff, bdiff; byte er, eg, eb, paletteIndex; for (int y = 0; y < h; y++) { int ny = y + 1; for (int i = 0; i < lineLen; i++) { curErrorRed[i] = nextErrorRed[i]; curErrorGreen[i] = nextErrorGreen[i]; curErrorBlue[i] = nextErrorBlue[i]; nextErrorRed[i] = 0; nextErrorGreen[i] = 0; nextErrorBlue[i] = 0; } for (int px = 0; px < lineLen; px++) { color = pixmap.getPixel(px, y) & 0xF8F8F880; if ((color & 0x80) == 0 && hasTransparent) pixmap.drawPixel(px, y, 0); else { er = curErrorRed[px]; eg = curErrorGreen[px]; eb = curErrorBlue[px]; color |= (color >>> 5 & 0x07070700) | 0xFE; int rr = MathUtils.clamp(((color >>> 24) ) + (er), 0, 0xFF); int gg = MathUtils.clamp(((color >>> 16) & 0xFF) + (eg), 0, 0xFF); int bb = MathUtils.clamp(((color >>> 8) & 0xFF) + (eb), 0, 0xFF); paletteIndex = paletteMapping[((rr << 7) & 0x7C00) | ((gg << 2) & 0x3E0) | ((bb >>> 3))]; used = paletteArray[paletteIndex & 0xFF]; pixmap.drawPixel(px, y, used); rdiff = (color>>>24)- (used>>>24); gdiff = (color>>>16&255)-(used>>>16&255); bdiff = (color>>>8&255)- (used>>>8&255); if(px < lineLen - 1) { curErrorRed[px+1] += rdiff * ditherStrength; curErrorGreen[px+1] += gdiff * ditherStrength; curErrorBlue[px+1] += bdiff * ditherStrength; } if(ny < h) { if(px > 0) { nextErrorRed[px-1] += rdiff * halfDitherStrength; nextErrorGreen[px-1] += gdiff * halfDitherStrength; nextErrorBlue[px-1] += bdiff * halfDitherStrength; } nextErrorRed[px] += rdiff * halfDitherStrength; nextErrorGreen[px] += gdiff * halfDitherStrength; nextErrorBlue[px] += bdiff * halfDitherStrength; } } } } pixmap.setBlending(blending); return pixmap; }