The fastest way to load sketch pixel values ​​in Java

I need to be able to load RGB pixel values ​​with a specific resolution in Java. This resolution is small (~ 300x300).

I am currently downloading them as follows:

File file = new File("...path...");     
BufferedImage imsrc = ImageIO.read(file);
int width = imsrc.getWidth();
int height = imsrc.getHeight();     
int[] data = new int[width * height];       
imsrc.getRGB(0,0, width, height, data, 0, width);

and then he will cut it.

Sam asked for the size reduction code, so here it is:

/**
 * DownSize an image. 
 * This is NOT precise, and is noisy. 
 * However, this is fast and better than NearestNeighbor
 * @param pixels - _RGB pixel values for the original image
 * @param width - width of the original image
 * @param newWidth - width of the new image
 * @param newHeight - height of the new image
 * @return - _RGB pixel values of the resized image
 */
public static int[] downSize(int[] pixels, int width, int newWidth, int newHeight) {
    int height = pixels.length / width;
    if (newWidth == width && height == newHeight) return pixels;
    int[] resized = new int[newWidth * newHeight];
    float x_ratio = (float) width / newWidth;
    float y_ratio = (float) height / newHeight;
    float xhr = x_ratio / 2;
    float yhr = y_ratio / 2;
    int i, j, k, l, m;
    for (int x = 0; x < newWidth; x ++)
        for (int y = 0; y < newHeight; y ++) {              
            i = (int) (x * x_ratio);
            j = (int) (y * y_ratio);
            k = (int) (x * x_ratio + xhr);
            l = (int) (y * y_ratio + yhr);
            for (int p = 0; p < 3; p ++) {
                m = 0xFF << (p * 8);
                resized[x + y * newWidth] |= (
                        (pixels[i + j * width] & m) +
                        (pixels[k + j * width] & m) +
                        (pixels[i + l * width] & m) + 
                        (pixels[k + l * width] & m) >> 2) & m;
            }
        }
    return resized;
}

I recently realized that I can reduce the size with ImageMagick 'convert', and then load the smaller version this way. This saves an additional 33%.

I was wondering if there is any better way.

EDIT: , , , . , , , (, 640x480, .getRGB() ), , ( ), , .

+3
1

Java :

http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

/.

    Graphics2D g2 = (Graphics2D)g;
    int newW = (int)(originalImage.getWidth() * xScaleFactor);
    int newH = (int)(originalImage.getHeight() * yScaleFactor);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g2.drawImage(originalImage, 0, 0, newW, newH, null);
+3

Source: https://habr.com/ru/post/1781071/


All Articles