Algorithms for scaling raster fonts

This is a continuation of this issue .

I am working on a low-level C application where I need to draw text. I decided to save the font that I want to use as an array (black and white, each char 128x256, maybe), then I would reduce it to the sizes I need with some algorithm (in grayscale, so I can do some rough smoothing font).

Note: this is a toy project, please ignore things like doing calculations at runtime or not.

Question: which algorithm?

I looked 2xSaI, but it's pretty complicated. I would like for me to be able to read the description and develop the code myself (I start and code in C / C ++ a little less than a year).

Suggestions, anyone?

Thank you for your time!

Edit: Please note that the B&W input, the output should be smoothed in shades of gray

+2
source share
5 answers

Display the rectangle in the source image that will correspond to the destination pixel. For example, if the source image is 50x100 and your destination is 20x40, the top left pixel at the destination corresponds to a rectangle from (0,0) to (2.2,2.2.2) in the source image. Now make an average size for these pixels:

  • 2,2 * 2,2 = 4,84. 1/4.84.
  • (0,0), (0,1), (1,0) (1,1) 1 .
  • (0,2), (1,2), (2,0) (2,1) 0,2 ( 20%).
  • (2,2) 0,04 ( 4%).
  • , , 4 * 1 + 4 * 0,2 + 0,04 = 4,84.

, , . , 4 /4 .

, . ( , , 1/2), , , - , , .

+4

, N * M BW. char Letter[N][M], 0 1. , unsigned char letter[n][m]. , letter :

char Letter[N][M];
unsigned char letter[n][m];
int rect_sz_X = N / n; // the size of rectangle that will map to a single pixel
int rect_sz_Y = M / m; // in the downscaled image
int i, j, x, y;
for (i = 0; i < n; i++) for (j = 0; j < m; j++){
    int sum = 0;
    for (x = 0; x < rect_sz_X; x++) for (y = 0; y < rect_sz_Y; y++)
        sum += Letter[i*rect_sz_X + x][j*rect_sz_Y + y];
    letter[n][m] = ( sum * 255) / (rect_sz_X * rect_sz_Y);
};

, , , ( , ). , .

+3

- , . , , . - , , . ( ):

+2

. , , , . , , , 16 . "" 4x4.

, , , .

+1

2 (50%, 25%, 12,5% ..), , . , 50% : , ; , . ( ), , tiebreaking.

0

All Articles