Skip to content

Commit

Permalink
replace getPixel with getPixels, which makes it 10 times faster (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anyongjin authored Mar 28, 2022
1 parent 82e06e6 commit 4337ae4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,23 @@ public Bitmap toRGBImage(Bitmap raster) throws IOException

int width = raster.getWidth();
int height = raster.getHeight();
int[] imgPixels = new int[width];

Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] outPixels = new int[width];

int gray;
int rgb;
for (int y = 0; y < height; y++)
{
raster.getPixels(imgPixels, 0, width, 0, y, width, 1);
for (int x = 0; x < width; x++)
{
gray = Color.alpha(raster.getPixel(x, y));
gray = Color.alpha(imgPixels[x]);
rgb = Color.argb(255, gray, gray, gray);
image.setPixel(x, y, rgb);
outPixels[x] = rgb;
}
image.setPixels(outPixels, 0, width, 0, y, width, 1);
}

return image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ public static PDImageXObject createFromImage(PDDocument document, Bitmap image)

int height = image.getHeight();
int width = image.getWidth();
int[] pixels = new int[width];

ByteArrayOutputStream bos = new ByteArrayOutputStream();
MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos);

for (int y = 0; y < height; ++y)
{
image.getPixels(pixels, 0, width, 0, y, width, 1);
for (int x = 0; x < width; ++x)
{
// flip bit to avoid having to set /BlackIs1
mcios.writeBits(~(image.getPixel(x, y) & 1), 1);
mcios.writeBits(~(pixels[x] & 1), 1);
}
if (mcios.getBitOffset() != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,39 +564,42 @@ else if (mask.getWidth() > width || mask.getHeight() > height)

// compose to ARGB
Bitmap masked = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] outPixels = new int[width];

int rgb;
int rgba;
int alphaPixel;
int alpha;
int[] imgPixels = new int[width];
int[] maskPixels = new int[width];
for (int y = 0; y < height; y++)
{
image.getPixels(imgPixels, 0, width, 0, y, width, 1);
mask.getPixels(maskPixels, 0, width, 0, y, width, 1);
for (int x = 0; x < width; x++)
{
rgb = image.getPixel(x, y);

alphaPixel = mask.getPixel(x, y);
rgb = imgPixels[x];
int r = Color.red(rgb);
int g = Color.green(rgb);
int b = Color.blue(rgb);
alphaPixel = maskPixels[x];
if (isSoft)
{
alpha = Color.alpha(alphaPixel);
if (matte != null && Float.compare(alpha, 0) != 0)
{
rgb = Color.rgb(
clampColor(((Color.red(rgb) / 255F - matte[0]) / (alpha / 255F) + matte[0]) * 255),
clampColor(((Color.green(rgb) / 255F - matte[1]) / (alpha / 255F) + matte[1]) * 255),
clampColor(((Color.blue(rgb) / 255F - matte[2]) / (alpha / 255F) + matte[2]) * 255)
);
r = clampColor(((r / 255F - matte[0]) / (alpha / 255F) + matte[0]) * 255);
g = clampColor(((g / 255F - matte[1]) / (alpha / 255F) + matte[1]) * 255);
b = clampColor(((b / 255F - matte[2]) / (alpha / 255F) + matte[2]) * 255);
}
}
else
{
alpha = 255 - Color.alpha(alphaPixel);
}
rgba = Color.argb(alpha, Color.red(rgb), Color.green(rgb),
Color.blue(rgb));

masked.setPixel(x, y, rgba);
outPixels[x] = Color.argb(alpha, r, g, b);
}
masked.setPixels(outPixels, 0, width, 0, y, width, 1);
}

return masked;
Expand Down

0 comments on commit 4337ae4

Please sign in to comment.