Home  >  Article  >  Backend Development  >  How do intensity-based and character fitting approaches differ in converting images to ASCII art in C ?

How do intensity-based and character fitting approaches differ in converting images to ASCII art in C ?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 18:38:30675browse

How do intensity-based and character fitting approaches differ in converting images to ASCII art in C  ?

Image to ASCII Art Conversion in C

Introduction

Converting an image to ASCII art involves converting each pixel in the image to a character that approximates its intensity or shape. This article explores two approaches for this conversion using C :

Approach 1: Intensity-Based Conversion

  • Pixel/Area Intensity Based (Shading):

    • Divides the image into pixels or rectangular areas (dots)
    • Computes the average gray scale intensity of each dot
    • Replaces each dot with a character whose intensity is closest to the computed intensity
  • Character Map:

    • Precomputed list of characters with their corresponding intensities
    • Characters can be distributed linearly or arbitrarily

Code Example:

<code class="c++">AnsiString m = ".,:;ox%#@&";
for (int y = 0; y < bmp->Height; y++) {
    for (int x = 0; x < bmp->Width; x++) {
        int i = (p[x + x + x + 0] + p[x + x + x + 1] + p[x + x + x + 2]) / 3;
        i = (i * m.Length()) / 768;
        s += m[m.Length() - i];
    }
    s += endl;
}</code>

Approach 2: Character Fitting (Hybrid Approach)

  • Tries to match image areas with characters of similar intensity and shape
  • Step-by-Step Process:

    • Divide image into rectangular areas
    • Compute intensity of each area in multiple zones (left, right, up, down, center)
    • Create a character map with similar intensity zones
    • Find the closest matching character in the map and replace the image area

Code Example:

<code class="c++">int xs, ys, xf, yf, x, xx, y, yy;
int i, i0, d, d0;
DWORD **p = NULL, **q = NULL;

for (;;) { // Dynamic allocation error handling
    // Font Properties
    xf = font->Size;   if (xf < 0) xf = - xf;
    yf = font->Height; if (yf < 0) yf = - yf;

    // Character Map
    for (x = 0, d = 32; d < 128; d++, x++) {
        map[x].c = char(DWORD(d));
        tmp->Canvas->TextOutA(0, 0, map[x].c);
        map[x].compute(q, xf, yf, 0, 0);
    }

    // Main Loop
    xf -= xf / 3;
    xs -= xs % xf;
    ys -= ys % yf;
    for (y = 0; y < ys; y += yf, txt += eol)
        for (x = 0; x < xs; x += xf) {
            gfx.compute(p, xf, yf, x, y);
            i0 = 0; d0 = -1;
            for (i = 0; map[i].c; i++) {
                d = abs(map[i].il - gfx.il) +
                    abs(map[i].ir - gfx.ir) +
                    abs(map[i].iu - gfx.iu) +
                    abs(map[i].id - gfx.id) +
                    abs(map[i].ic - gfx.ic);
                if ((d0 < 0) || (d0 > d)) {
                    d0 = d; i0 = i;
                }
            }
            txt += map[i0].c;
        }
    break;
}</code>

Comparison and Example:

Approach Advantage Disadvantage
Intensity-Based Simple, Fast Limited Visual Appeal
Character Fitting Higher Quality Output Slower Processing

Intensity exampleCharacter fitting example

The above is the detailed content of How do intensity-based and character fitting approaches differ in converting images to ASCII art in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn