Skip to content

Commit 4337ae4

Browse files
authored
replace getPixel with getPixels, which makes it 10 times faster (#386)
1 parent 82e06e6 commit 4337ae4

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

library/src/main/java/com/tom_roush/pdfbox/pdmodel/graphics/color/PDDeviceGray.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,23 @@ public Bitmap toRGBImage(Bitmap raster) throws IOException
8181

8282
int width = raster.getWidth();
8383
int height = raster.getHeight();
84+
int[] imgPixels = new int[width];
8485

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

8789
int gray;
8890
int rgb;
8991
for (int y = 0; y < height; y++)
9092
{
93+
raster.getPixels(imgPixels, 0, width, 0, y, width, 1);
9194
for (int x = 0; x < width; x++)
9295
{
93-
gray = Color.alpha(raster.getPixel(x, y));
96+
gray = Color.alpha(imgPixels[x]);
9497
rgb = Color.argb(255, gray, gray, gray);
95-
image.setPixel(x, y, rgb);
98+
outPixels[x] = rgb;
9699
}
100+
image.setPixels(outPixels, 0, width, 0, y, width, 1);
97101
}
98102

99103
return image;

library/src/main/java/com/tom_roush/pdfbox/pdmodel/graphics/image/CCITTFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ public static PDImageXObject createFromImage(PDDocument document, Bitmap image)
6969

7070
int height = image.getHeight();
7171
int width = image.getWidth();
72+
int[] pixels = new int[width];
7273

7374
ByteArrayOutputStream bos = new ByteArrayOutputStream();
7475
MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos);
7576

7677
for (int y = 0; y < height; ++y)
7778
{
79+
image.getPixels(pixels, 0, width, 0, y, width, 1);
7880
for (int x = 0; x < width; ++x)
7981
{
8082
// flip bit to avoid having to set /BlackIs1
81-
mcios.writeBits(~(image.getPixel(x, y) & 1), 1);
83+
mcios.writeBits(~(pixels[x] & 1), 1);
8284
}
8385
if (mcios.getBitOffset() != 0)
8486
{

library/src/main/java/com/tom_roush/pdfbox/pdmodel/graphics/image/PDImageXObject.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -564,39 +564,42 @@ else if (mask.getWidth() > width || mask.getHeight() > height)
564564

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

568569
int rgb;
569-
int rgba;
570570
int alphaPixel;
571571
int alpha;
572+
int[] imgPixels = new int[width];
573+
int[] maskPixels = new int[width];
572574
for (int y = 0; y < height; y++)
573575
{
576+
image.getPixels(imgPixels, 0, width, 0, y, width, 1);
577+
mask.getPixels(maskPixels, 0, width, 0, y, width, 1);
574578
for (int x = 0; x < width; x++)
575579
{
576-
rgb = image.getPixel(x, y);
577-
578-
alphaPixel = mask.getPixel(x, y);
580+
rgb = imgPixels[x];
581+
int r = Color.red(rgb);
582+
int g = Color.green(rgb);
583+
int b = Color.blue(rgb);
584+
alphaPixel = maskPixels[x];
579585
if (isSoft)
580586
{
581587
alpha = Color.alpha(alphaPixel);
582588
if (matte != null && Float.compare(alpha, 0) != 0)
583589
{
584-
rgb = Color.rgb(
585-
clampColor(((Color.red(rgb) / 255F - matte[0]) / (alpha / 255F) + matte[0]) * 255),
586-
clampColor(((Color.green(rgb) / 255F - matte[1]) / (alpha / 255F) + matte[1]) * 255),
587-
clampColor(((Color.blue(rgb) / 255F - matte[2]) / (alpha / 255F) + matte[2]) * 255)
588-
);
590+
r = clampColor(((r / 255F - matte[0]) / (alpha / 255F) + matte[0]) * 255);
591+
g = clampColor(((g / 255F - matte[1]) / (alpha / 255F) + matte[1]) * 255);
592+
b = clampColor(((b / 255F - matte[2]) / (alpha / 255F) + matte[2]) * 255);
589593
}
590594
}
591595
else
592596
{
593597
alpha = 255 - Color.alpha(alphaPixel);
594598
}
595-
rgba = Color.argb(alpha, Color.red(rgb), Color.green(rgb),
596-
Color.blue(rgb));
597599

598-
masked.setPixel(x, y, rgba);
600+
outPixels[x] = Color.argb(alpha, r, g, b);
599601
}
602+
masked.setPixels(outPixels, 0, width, 0, y, width, 1);
600603
}
601604

602605
return masked;

0 commit comments

Comments
 (0)