public void setResourceIds(int[] resourceIds) { ByteBuffer[] mipmapChain = new ByteBuffer[resourceIds.length]; Resources resources = TextureManager.getInstance().getContext().getResources(); int mip_0_width = 1, mip_0_height = 1; try { for (int i = 0, length = resourceIds.length; i < length; i++) { ETC1Util.ETC1Texture texture = ETC1Util.createTexture(resources.openRawResource(resourceIds[i])); mipmapChain[i] = texture.getData(); if (i == 0) { mip_0_width = texture.getWidth(); mip_0_height = texture.getHeight(); } } setWidth(mip_0_width); setHeight(mip_0_height); setCompressionFormat(ETC1.ETC1_RGB8_OES); } catch (IOException e) { RajLog.e(e.getMessage()); e.printStackTrace(); } mByteBuffers = mipmapChain; }
public void setBitmap(Bitmap bitmap) { mBitmap = bitmap; int imageSize = bitmap.getRowBytes() * bitmap.getHeight(); ByteBuffer uncompressedBuffer = ByteBuffer.allocateDirect(imageSize); bitmap.copyPixelsToBuffer(uncompressedBuffer); uncompressedBuffer.position(0); ByteBuffer compressedBuffer = ByteBuffer.allocateDirect( ETC1.getEncodedDataSize(bitmap.getWidth(), bitmap.getHeight())).order(ByteOrder.nativeOrder()); ETC1.encodeImage(uncompressedBuffer, bitmap.getWidth(), bitmap.getHeight(), 2, 2 * bitmap.getWidth(), compressedBuffer); mByteBuffers = new ByteBuffer[]{compressedBuffer}; setWidth(bitmap.getWidth()); setHeight(bitmap.getHeight()); }
private void setBitmap(Bitmap bitmap) { mBitmap = bitmap; int imageSize = bitmap.getRowBytes() * bitmap.getHeight(); ByteBuffer uncompressedBuffer = ByteBuffer.allocateDirect(imageSize); bitmap.copyPixelsToBuffer(uncompressedBuffer); uncompressedBuffer.position(0); ByteBuffer compressedBuffer = ByteBuffer.allocateDirect( ETC1.getEncodedDataSize(bitmap.getWidth(), bitmap.getHeight())).order(ByteOrder.nativeOrder()); ETC1.encodeImage(uncompressedBuffer, bitmap.getWidth(), bitmap.getHeight(), 2, 2 * bitmap.getWidth(), compressedBuffer); setCompressionFormat(ETC1.ETC1_RGB8_OES); mByteBuffers = new ByteBuffer[]{compressedBuffer}; setWidth(bitmap.getWidth()); setHeight(bitmap.getHeight()); }
public ETC1Texture(final TextureManager pTextureManager, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IOException { super(pTextureManager, PixelFormat.RGB_565, pTextureOptions, pTextureStateListener); InputStream inputStream = null; try { inputStream = this.getInputStream(); this.mETC1TextureHeader = new ETC1TextureHeader(StreamUtils.streamToBytes(inputStream, ETC1.ETC_PKM_HEADER_SIZE)); if (BuildConfig.DEBUG) { if (!(MathUtils.isPowerOfTwo(this.mETC1TextureHeader.mWidth) && MathUtils.isPowerOfTwo(this.mETC1TextureHeader.mHeight))) { Debug.w("ETC1 textures with NPOT sizes can cause a crash on PowerVR GPUs!"); } } } finally { StreamUtils.close(inputStream); } }
public ETC1TextureHeader(final byte[] pData) { if (pData.length != ETC1.ETC_PKM_HEADER_SIZE) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mDataByteBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE).order(ByteOrder.nativeOrder()); this.mDataByteBuffer.put(pData, 0, ETC1.ETC_PKM_HEADER_SIZE); this.mDataByteBuffer.position(0); if (!ETC1.isValid(this.mDataByteBuffer)) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mWidth = ETC1.getWidth(this.mDataByteBuffer); this.mHeight = ETC1.getHeight(this.mDataByteBuffer); }
public ETC1TextureHeader(final byte[] pData) { if(pData.length != ETC1.ETC_PKM_HEADER_SIZE) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mDataByteBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE).order(ByteOrder.nativeOrder()); this.mDataByteBuffer.put(pData, 0, ETC1.ETC_PKM_HEADER_SIZE); this.mDataByteBuffer.position(0); if (!ETC1.isValid(this.mDataByteBuffer)) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mWidth = ETC1.getWidth(this.mDataByteBuffer); this.mHeight = ETC1.getHeight(this.mDataByteBuffer); }
/** * Add mipmap-chained ETC1 texture. */ public TextureInfo addEtc1Texture(int[] resourceIds, TextureType textureType, boolean isExistingTexture, WrapType wrapType, FilterType filterType) { if (resourceIds == null) return null; ByteBuffer[] mipmapChain = new ByteBuffer[resourceIds.length]; int mip_0_width = 1, mip_0_height = 1; try { for (int i = 0, length = resourceIds.length; i < length; i++) { ETC1Util.ETC1Texture texture = ETC1Util.createTexture(mContext.getResources().openRawResource(resourceIds[i])); mipmapChain[i] = texture.getData(); if (i == 0) { mip_0_width = texture.getWidth(); mip_0_height = texture.getHeight(); } } } catch (IOException e) { RajLog.e(e.getMessage()); e.printStackTrace(); } return addTexture(mipmapChain, null, mip_0_width, mip_0_height, textureType, null, true, false, isExistingTexture, wrapType, filterType, CompressionType.ETC1, ETC1.ETC1_RGB8_OES); }
public static byte[] testSDKETC1BlockCompressor() { // Test android class block (reference) byte[] in1 = { 6, 5, 7, 7, 6, 5, 9, 2, 1, 20, 5, 80, 75, 24, 96, 64, 27, 43, 45, 78, 21, 2, 85, 32, 9, 5, 7, 7, 6, 5, 9, 2, 1, 85, 5, 80, 75, 3, 96, 64, 4, 43, 45, 78, 21, 2, 7, 32 }; ByteBuffer inb = ByteBuffer.allocateDirect(48).order( ByteOrder.nativeOrder()); inb.put(in1); ByteBuffer out = ByteBuffer.allocateDirect(8).order( ByteOrder.nativeOrder()); inb.rewind(); ETC1.encodeBlock(inb, mask, out); inb.rewind(); byte[] arrayOut1 = new byte[8]; out.get(arrayOut1); return arrayOut1; }
public static ETC1Texture testSDKETC1ImageCompressor() { // RGB_565 is 2 bytes per pixel ETC1.encodeImage(buffer, bitmap.getWidth(), bitmap.getHeight(), 2, 2 * bitmap.getWidth(), compressedImage); ETC1Texture texture = new ETC1Texture(bitmap.getWidth(), bitmap.getHeight(), compressedImage); buffer.rewind(); // if (texture != null) { // int estimatedMemorySize = ETC1.ETC_PKM_HEADER_SIZE // + texture.getHeight() * texture.getWidth() / 2; // File f = new // File(Environment.getExternalStorageDirectory(),"bmngpkm.pkm"); // f.delete(); // f.createNewFile(); // ETC1Util.writeTexture(texture, new FileOutputStream(f)); // System.out.println("Texture PKM created "); // } // System.out.println("Texture PKM creation failed "); return texture; }
/** * Helper function that writes an ETC1Texture to an output stream formatted as a PKM file. * @param texture the input texture. * @param output the stream to write the formatted texture data to. * @throws IOException */ public static void writeTexture(ETC1Texture texture, OutputStream output) throws IOException { ByteBuffer dataBuffer = texture.getData(); dataBuffer.rewind(); System.out.println(dataBuffer.remaining()); int originalPosition = dataBuffer.position(); try { int width = texture.getWidth(); int height = texture.getHeight(); ByteBuffer header = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE).order(ByteOrder.nativeOrder()); ETC1.formatHeader(header, width, height); header.position(0); byte[] ioBuffer = new byte[4096]; header.get(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE); output.write(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE); while (dataBuffer.remaining()>0) { int chunkSize = Math.min(ioBuffer.length, dataBuffer.remaining()); dataBuffer.get(ioBuffer, 0, chunkSize); output.write(ioBuffer, 0, chunkSize); } } finally { dataBuffer.position(originalPosition); } }
public void setBitmap(Bitmap bitmap) { mBitmap = bitmap; int imageSize = bitmap.getRowBytes() * bitmap.getHeight(); ByteBuffer uncompressedBuffer = ByteBuffer.allocateDirect(imageSize); bitmap.copyPixelsToBuffer(uncompressedBuffer); uncompressedBuffer.position(0); ByteBuffer compressedBuffer = ByteBuffer.allocateDirect( ETC1.getEncodedDataSize(bitmap.getWidth(), bitmap.getHeight())).order(ByteOrder.nativeOrder()); ETC1.encodeImage(uncompressedBuffer, bitmap.getWidth(), bitmap.getHeight(), 2, 2 * bitmap.getWidth(), compressedBuffer); mByteBuffers = new ByteBuffer[] { compressedBuffer }; setWidth(bitmap.getWidth()); setHeight(bitmap.getHeight()); }
/** * Add mipmap-chained ETC1 texture. * @param context * @param resourceIds * @return */ public TextureInfo addEtc1Texture(int[] resourceIds, TextureType textureType, boolean isExistingTexture, WrapType wrapType, FilterType filterType) { if (resourceIds == null) return null; ByteBuffer[] mipmapChain = new ByteBuffer[resourceIds.length]; int mip_0_width = 1, mip_0_height = 1; try { for (int i = 0, length = resourceIds.length; i < length; i++) { ETC1Util.ETC1Texture texture = ETC1Util.createTexture(mContext.getResources().openRawResource(resourceIds[i])); mipmapChain[i] = texture.getData(); if (i == 0) { mip_0_width = texture.getWidth(); mip_0_height = texture.getHeight(); } } } catch (IOException e) { RajLog.e(e.getMessage()); e.printStackTrace(); } return addTexture(mipmapChain, null, mip_0_width, mip_0_height, textureType, null, true, false, isExistingTexture, wrapType, filterType, CompressionType.ETC1, ETC1.ETC1_RGB8_OES); }
/** * Create a new ETC2Texture from an input stream containing a PKM formatted compressed texture. * * @param input an input stream containing a PKM formatted compressed texture. * * @return an ETC2Texture read from the input stream. * @throws IOException */ public static ETC2Texture createTexture(InputStream input) throws IOException { int width = 0; int height = 0; int format = -1; byte[] ioBuffer = new byte[4096]; // We can use the ETC1 header size as it is the same if (input.read(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE) != ETC1.ETC_PKM_HEADER_SIZE) { throw new IOException("Unable to read PKM file header."); } final ByteBuffer headerBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE) .order(ByteOrder.BIG_ENDIAN); headerBuffer.put(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE).position(0); if (!ETC2.isValid(headerBuffer)) { throw new IOException("Not a PKM file."); } width = ETC2.getWidth(headerBuffer); height = ETC2.getHeight(headerBuffer); format = ETC2.getETC2CompressionType(headerBuffer); final int encodedSize = ETC2.getEncodedDataSize(width, height); final ByteBuffer dataBuffer = ByteBuffer.allocateDirect(encodedSize).order(ByteOrder.BIG_ENDIAN); for (int i = 0; i < encodedSize; ) { int chunkSize = Math.min(ioBuffer.length, encodedSize - i); if (input.read(ioBuffer, 0, chunkSize) != chunkSize) { throw new IOException("Unable to read PKM file data."); } dataBuffer.put(ioBuffer, 0, chunkSize); i += chunkSize; } dataBuffer.position(0); return new ETC2Texture(format, width, height, dataBuffer); }
public void setResourceId(int resourceId) { mResourceId = resourceId; Resources resources = TextureManager.getInstance().getContext().getResources(); try { ETC1Util.ETC1Texture texture = ETC1Util.createTexture(resources.openRawResource(resourceId)); mByteBuffers = new ByteBuffer[]{texture.getData()}; setWidth(texture.getWidth()); setHeight(texture.getHeight()); setCompressionFormat(ETC1.ETC1_RGB8_OES); } catch (IOException e) { RajLog.e(e.getMessage()); e.printStackTrace(); } }
public ETC1Texture(final TextureManager pTextureManager, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IOException { super(pTextureManager, PixelFormat.RGB_565, pTextureOptions, pTextureStateListener); InputStream inputStream = null; try { inputStream = this.getInputStream(); this.mETC1TextureHeader = new ETC1TextureHeader(StreamUtils.streamToBytes(inputStream, ETC1.ETC_PKM_HEADER_SIZE)); } finally { StreamUtils.close(inputStream); } }
/** * Dynamically generates an ETC1 texture on-the-fly from given bitmap and automatically binds the newly generated * texture. * * @param bitmap * @param textureType * @return */ public TextureInfo addEtc1Texture(Bitmap bitmap, TextureType textureType) { int imageSize = bitmap.getRowBytes() * bitmap.getHeight(); ByteBuffer uncompressedBuffer = ByteBuffer.allocateDirect(imageSize); bitmap.copyPixelsToBuffer(uncompressedBuffer); uncompressedBuffer.position(0); ByteBuffer compressedBuffer = ByteBuffer.allocateDirect(ETC1.getEncodedDataSize(bitmap.getWidth(), bitmap.getHeight())).order(ByteOrder.nativeOrder()); ETC1.encodeImage(uncompressedBuffer, bitmap.getWidth(), bitmap.getHeight(), 2, 2 * bitmap.getWidth(), compressedBuffer); return addEtc1Texture(compressedBuffer, bitmap.getWidth(), bitmap.getHeight(), textureType); }
public void showEtc1Texture(ETC1Texture texture) { int width = texture.getWidth(); int height = texture.getHeight(); Buffer data = texture.getData(); int pixelSize = 2; int stride = pixelSize * width; ByteBuffer decodedData = ByteBuffer.allocateDirect(stride*height) .order(ByteOrder.nativeOrder()); ETC1.decodeImage((ByteBuffer) data, decodedData, width, height, pixelSize, stride); mBitmapOut.copyPixelsFromBuffer(decodedData); mDisplayView.invalidate(); }
/** * Convenience method to load an ETC1 texture whether or not the active OpenGL context * supports the ETC1 texture compression format. * @param target the texture target. * @param level the texture level * @param border the border size. Typically 0. * @param fallbackFormat the format to use if ETC1 texture compression is not supported. * Must be GL_RGB. * @param fallbackType the type to use if ETC1 texture compression is not supported. * Can be either GL_UNSIGNED_SHORT_5_6_5, which implies 16-bits-per-pixel, * or GL_UNSIGNED_BYTE, which implies 24-bits-per-pixel. * @param texture the ETC1 to load. */ public static void loadTexture(int target, int level, int border, int fallbackFormat, int fallbackType, ETC1Texture texture) { if (fallbackFormat != GLES10.GL_RGB) { throw new IllegalArgumentException("fallbackFormat must be GL_RGB"); } if (! (fallbackType == GLES10.GL_UNSIGNED_SHORT_5_6_5 || fallbackType == GLES10.GL_UNSIGNED_BYTE)) { throw new IllegalArgumentException("Unsupported fallbackType"); } int width = texture.getWidth(); int height = texture.getHeight(); Buffer data = texture.getData(); if (isETC1Supported()) { int imageSize = data.remaining(); GLES10.glCompressedTexImage2D(target, level, RsETC1.ETC1_RGB8_OES, width, height, border, imageSize, data); } else { boolean useShorts = fallbackType != GLES10.GL_UNSIGNED_BYTE; int pixelSize = useShorts ? 2 : 3; int stride = pixelSize * width; ByteBuffer decodedData = ByteBuffer.allocateDirect(stride*height) .order(ByteOrder.nativeOrder()); ETC1.decodeImage((ByteBuffer) data, decodedData, width, height, pixelSize, stride); GLES10.glTexImage2D(target, level, fallbackFormat, width, height, border, fallbackFormat, fallbackType, decodedData); } }
/** * Create a new ETC1Texture from an input stream containing a PKM formatted compressed texture. * @param input an input stream containing a PKM formatted compressed texture. * @return an ETC1Texture read from the input stream. * @throws IOException */ public static ETC1Texture createTexture(InputStream input) throws IOException { int width = 0; int height = 0; byte[] ioBuffer = new byte[4096]; { if (input.read(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE) != ETC1.ETC_PKM_HEADER_SIZE) { throw new IOException("Unable to read PKM file header."); } ByteBuffer headerBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE) .order(ByteOrder.nativeOrder()); headerBuffer.put(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE).position(0); if (!ETC1.isValid(headerBuffer)) { throw new IOException("Not a PKM file."); } width = ETC1.getWidth(headerBuffer); height = ETC1.getHeight(headerBuffer); } int encodedSize = ETC1.getEncodedDataSize(width, height); ByteBuffer dataBuffer = ByteBuffer.allocateDirect(encodedSize).order(ByteOrder.nativeOrder()); for (int i = 0; i < encodedSize; ) { int chunkSize = Math.min(ioBuffer.length, encodedSize - i); if (input.read(ioBuffer, 0, chunkSize) != chunkSize) { throw new IOException("Unable to read PKM file data."); } dataBuffer.put(ioBuffer, 0, chunkSize); i += chunkSize; } dataBuffer.position(0); return new ETC1Texture(width, height, dataBuffer); }
public void setResourceId(int resourceId) { mResourceId = resourceId; Resources resources = TextureManager.getInstance().getContext().getResources(); try { ETC1Util.ETC1Texture texture = ETC1Util.createTexture(resources.openRawResource(resourceId)); mByteBuffers = new ByteBuffer[] { texture.getData() }; setWidth(texture.getWidth()); setHeight(texture.getHeight()); setCompressionFormat(ETC1.ETC1_RGB8_OES); } catch (IOException e) { RajLog.e(e.getMessage()); e.printStackTrace(); } }
public Etc1Texture(String textureName) { super(textureName); mCompressionType = CompressionType.ETC1; mCompressionFormat = ETC1.ETC1_RGB8_OES; }
public TextureInfo addEtc1Texture(ByteBuffer buffer, int width, int height, TextureType textureType, boolean isExistingTexture, WrapType wrapType, FilterType filterType) { return addTexture(new ByteBuffer[]{buffer}, null, width, height, textureType, null, false, false, isExistingTexture, wrapType, filterType, CompressionType.ETC1, ETC1.ETC1_RGB8_OES); }
public TextureInfo addEtc1Texture(ByteBuffer[] buffer, int width, int height, TextureType textureType, boolean isExistingTexture, WrapType wrapType, FilterType filterType) { return addTexture(buffer, null, width, height, textureType, null, false, false, isExistingTexture, wrapType, filterType, CompressionType.ETC1, ETC1.ETC1_RGB8_OES); }
public static ETC1Texture encodeTextureAsETC1_Sdk(InputStream stream) throws IOException, FileNotFoundException { // stream.reset(); // stream.mark(1024); Options opts = new BitmapFactory.Options(); //opts.inPremultiplied = false; opts.inPreferredConfig = Config.RGB_565; Bitmap bitmap = BitmapFactory.decodeStream(stream, null, opts); if (bitmap != null) { ByteBuffer buffer = ByteBuffer.allocateDirect( bitmap.getRowBytes() * bitmap.getHeight()).order( ByteOrder.nativeOrder()); bitmap.copyPixelsToBuffer(buffer); buffer.position(0); System.out.println("Width : " + bitmap.getWidth()); System.out.println("Height : " + bitmap.getHeight()); System.out.println("Config : " + bitmap.getConfig()); if (bitmap.getConfig() == Bitmap.Config.ARGB_4444 || bitmap.getConfig() == Bitmap.Config.ARGB_8888) { System.out.println("Texture need aplha channel"); return null; } final int encodedImageSize = ETC1.getEncodedDataSize( bitmap.getWidth(), bitmap.getHeight()); ByteBuffer compressedImage = ByteBuffer.allocateDirect( encodedImageSize).order(ByteOrder.nativeOrder()); // RGB_565 is 2 bytes per pixel ETC1.encodeImage(buffer, bitmap.getWidth(), bitmap.getHeight(), 2, 2 * bitmap.getWidth(), compressedImage); ETC1Texture texture = new ETC1Texture(bitmap.getWidth(), bitmap.getHeight(), compressedImage); return texture; } return null; }