Created
November 9, 2017 12:34
-
-
Save Petrakeas/c47a406538cc9bd25fd4a2d19647539a to your computer and use it in GitHub Desktop.
Methods for downscaling a bitmap to powers of 2 sizes ( https://medium.com/@petrakeas/alias-free-resize-with-renderscript-5bf15a86ce3 )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class resizeUtils { | |
public static Bitmap successiveResize(Bitmap src, int resizeNum) { | |
int srcWidth = src.getWidth(); | |
int srcHeight = src.getHeight(); | |
Bitmap output = src; | |
for (int i = 0; i < resizeNum; i++) { | |
srcWidth /= 2; | |
srcHeight /= 2; | |
Bitmap temp = Bitmap.createScaledBitmap(output, srcWidth, srcHeight, true); | |
if (i != 0) { // don't recycle the src bitmap | |
output.recycle(); | |
} | |
output = temp; | |
} | |
return output; | |
} | |
public static Bitmap successiveResizeRS(RenderScript rs, Bitmap src, int resizeNum) { | |
int srcWidth = src.getWidth(); | |
int srcHeight = src.getHeight(); | |
Bitmap.Config config = src.getConfig(); | |
Allocation srcAllocation = Allocation.createFromBitmap(rs, src); | |
ScriptIntrinsicResize resizeScript = ScriptIntrinsicResize.create(rs); | |
Allocation outAllocation = null; | |
for (int i = 0; i < resizeNum; i++) { | |
srcWidth /= 2; | |
srcHeight /= 2; | |
Type t = Type.createXY(rs, srcAllocation.getElement(), srcWidth, srcHeight); | |
outAllocation = Allocation.createTyped(rs, t); | |
resizeScript.setInput(srcAllocation); | |
resizeScript.forEach_bicubic(outAllocation); | |
srcAllocation.destroy(); | |
srcAllocation = outAllocation; | |
} | |
Bitmap output = Bitmap.createBitmap(srcWidth, srcHeight, config); | |
outAllocation.copyTo(output); | |
resizeScript.destroy(); | |
outAllocation.destroy(); | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment