Enlarge image

There is a very large image that cannot be loaded into memory once. Because it can cause an exception in memory. I need to enlarge the image to a small size. So what should I do?

A simple thought is opened by the input stream and processes the buffer size at a time. But the scaling algorithm?

+4
source share
2 answers

There are many different resizing algorithms that offer different levels of quality, and trading is processor time.

I believe that with any of them you should be able to process a massive file in chunks relatively easily, however you should probably try existing tools to see if they can already process a massive file.

The Gd graphics library allows you to determine how much working memory it can use. I believe that she obviously already has the logic for processing files in pieces.

+1
source

If you can access the drawing line by line (for example, a bitmap ), the simplest thing you could do is simply downsample , for example, to read only every nth pixel of every nth line.

// n is an integer that is the downsampling factor // width, height are the width and height of the original image, in pixels // down is a new image that is (height/n * width/n) pixels in size for (y = 0; y < height; y += n) { row = ... // read row y from original image into a buffer for (x = 0; x < width; x += n) { down[y/n, x/n] = row[x]; // image[row,col] -- shorthand for accessing a pixel } } 

This is a quick and dirty way that allows you to quickly and cheaply change the original image without loading it all into memory. Unfortunately, it also introduces anti-aliasing of the output image (down). Working with aliasing will require interpolation - it is still possible to use the above phased approach, but it is a bit more active.

If you cannot easily access a sequential image, for example. this is JPEG, which encodes data in 8x8 blocks, you can still do something similar to the approach described above. You would just read a line of blocks instead of a line of pixels - the rest of the algorithm would work the same. In addition, if you reduce disqualification by 8 times, then with JPEG - very simple - you just take the DC coefficient for each block . Using this approach, downsampling by multiples of 8 is also possible.

I masked many other details (such as color channels, pixel pitch, etc.), but that should be enough for you to get started.

+3
source

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


All Articles