Changeset 446


Ignore:
Timestamp:
Jan 10, 2005, 2:21:50 PM (18 years ago)
Author:
Sam Hocevar
Message:
  • src/common.h: new font structure.
  • src/slashdot.c: use new font structure, removed FACTOR.
  • extras/makefont.c: we add spaces in the string ourselves.
Location:
pwntcha/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pwntcha/trunk/extras/makefont.c

    r443 r446  
    1818#include <stdlib.h>
    1919#include <stdio.h>
     20#include <string.h>
    2021
    2122#include "SDL.h"
     
    2425int main(int argc, char *argv[])
    2526{
     27    unsigned char *text;
    2628    SDL_Color bg = { 0xff, 0xff, 0xff, 0 };
    2729    SDL_Color fg = { 0x00, 0x00, 0x00, 0 };
    28     SDL_Surface *text;
     30    SDL_Surface *surface;
    2931    TTF_Font *font;
     32    int i;
    3033
    3134    if(argc != 5)
     
    3639    }
    3740
     41    /* Load font */
    3842    TTF_Init();
    3943    font = TTF_OpenFont(argv[1], atoi(argv[2]));
     
    4650    }
    4751
     52    /* Add spaces to string */
     53    text = malloc(2 * strlen(argv[3]) * sizeof(char));
     54    for(i = 0; argv[3][i]; i++)
     55    {
     56        text[i * 2] = argv[3][i];
     57        text[i * 2 + 1] = ' ';
     58    }
     59    text[i * 2 - 1] = '\0';
     60
     61    /* Render text to surface */
    4862    TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
    49     text = TTF_RenderUTF8_Shaded(font, argv[3], fg, bg);
    50     if(!text)
     63    surface = TTF_RenderUTF8_Shaded(font, argv[3], fg, bg);
     64    if(!surface)
    5165    {
    52         fprintf(stderr, "text rendering failed: %s\n", SDL_GetError());
     66        fprintf(stderr, "surface rendering failed: %s\n", SDL_GetError());
    5367        TTF_CloseFont(font);
    5468        TTF_Quit();
     
    5670    }
    5771
    58     SDL_SaveBMP(text, argv[4]);
    59     SDL_FreeSurface(text);
     72    /* Clean up surface */
     73
     74    /* Save surface and free everything */
     75    SDL_SaveBMP(surface, argv[4]);
     76    SDL_FreeSurface(surface);
    6077    TTF_CloseFont(font);
    6178    TTF_Quit();
  • pwntcha/trunk/src/common.h

    r445 r446  
    1818};
    1919
     20/* font structure */
     21struct font
     22{
     23    struct image *img;
     24    struct glyph
     25    {
     26        int xmin, xmax, ymin, ymax;
     27        int count; /* Black pixel count */
     28    } *glyphs;
     29};
     30
    2031/* global variables */
    2132extern char *argv0;
     
    3344char *decode_slashdot(struct image *img);
    3445char *decode_vbulletin(struct image *img);
     46char *decode_xanga(struct image *img);
    3547char *decode_test(struct image *img);
    3648
  • pwntcha/trunk/src/slashdot.c

    r445 r446  
    2525
    2626/* Our macros */
    27 #define FACTOR 1
    28 #define FONTNAME "font_slashdot.png" // use with FACTOR = 1
    29 //#define FONTNAME "font.png" // use with FACTOR = 2
    30 //#define FONTNAME "font_dilated.png" // use with FACTOR = 2
    31 static struct image *font = NULL;
     27#define FONTNAME "font_slashdot.png"
     28
     29struct font font;
     30struct glyph glyphs[22];
    3231
    3332/* Global stuff */
     
    169168    }
    170169
    171     tmp = image_new(img->width * FACTOR, img->height * FACTOR);
    172 
    173     for(y = 0; y < img->height * FACTOR; y++)
    174         for(x = 0; x < img->width * FACTOR; x++)
    175         {
    176             xtmp = 1.0 * (x - img->width * FACTOR / 2) / FACTOR;
    177             ytmp = 1.0 * (y - img->height * FACTOR / 2) / FACTOR;
     170    tmp = image_new(img->width, img->height);
     171
     172    for(y = 0; y < img->height; y++)
     173        for(x = 0; x < img->width; x++)
     174        {
     175            xtmp = 1.0 * (x - img->width / 2);
     176            ytmp = 1.0 * (y - img->height / 2);
    178177            xdest = xtmp * cosa - ytmp * sina + 0.5 * img->width;
    179178            ydest = ytmp * cosa + xtmp * sina + 0.5 * img->height;
     
    232231{
    233232    char all[] = "abcdefgijkmnpqrstvwxyz";
    234     struct
    235     {
    236         int xmin, xmax, ymin, ymax;
    237         int count;
    238     }
    239     glyphs[22];
    240233    struct image *tmp;
    241234    int x, y, i = 0;
     
    244237    int distmin, distx, disty, distch;
    245238
    246     if(!font)
     239    if(!font.img)
    247240    {
    248241        char fontname[BUFSIZ];
    249242        sprintf(fontname, "%s/%s", share, FONTNAME);
    250         font = image_load(fontname);
    251         if(!font)
     243        font.img = image_load(fontname);
     244        if(!font.img)
    252245        {
    253246            fprintf(stderr, "cannot load font %s\n", fontname);
    254247            exit(-1);
    255248        }
     249        font.glyphs = glyphs;
    256250    }
    257251
     
    265259        }
    266260
    267     for(x = 0; x < font->width; x++)
     261    for(x = 0; x < font.img->width; x++)
    268262    {
    269263        int found = 0;
    270         for(y = 0; y < font->height; y++)
    271         {
    272             getpixel(font, x, y, &r, &g, &b);
     264        for(y = 0; y < font.img->height; y++)
     265        {
     266            getpixel(font.img, x, y, &r, &g, &b);
    273267            if(r < 128)
    274268            {
     
    287281            xmax = x;
    288282#if 0
    289             ymin = font->height;
     283            ymin = font.img->height;
    290284            ymax = 0;
    291             for(y = 0; y < font->height; y++)
     285            for(y = 0; y < font.img->height; y++)
    292286            {
    293287                int newx;
     
    295289                for(newx = xmin; newx < xmax; newx++)
    296290                {
    297                     getpixel(font, newx, y, &r, &g, &b);
     291                    getpixel(font.img, newx, y, &r, &g, &b);
    298292                    if(r < 128)
    299293                    {
     
    310304#else
    311305            ymin = 0;
    312             ymax = font->height;
     306            ymax = font.img->height;
    313307#endif
    314             glyphs[i].xmin = xmin;
    315             glyphs[i].xmax = xmax;
    316             glyphs[i].ymin = ymin;
    317             glyphs[i].ymax = ymax;
    318             glyphs[i].count = count;
     308            font.glyphs[i].xmin = xmin;
     309            font.glyphs[i].xmax = xmax;
     310            font.glyphs[i].ymin = ymin;
     311            font.glyphs[i].ymax = ymax;
     312            font.glyphs[i].count = count;
    319313            count = 0;
    320314            i++;
     
    336330            int localmin = INT_MAX, localx, localy;
    337331//if(all[i] == 'i') continue;
    338             xmin = glyphs[i].xmin;
    339             ymin = glyphs[i].ymin;
    340             xmax = glyphs[i].xmax;
    341             ymax = glyphs[i].ymax;
     332            xmin = font.glyphs[i].xmin;
     333            ymin = font.glyphs[i].ymin;
     334            xmax = font.glyphs[i].xmax;
     335            ymax = font.glyphs[i].ymax;
    342336            //printf("trying to find %c (%i×%i) - ", all[i], xmax - xmin, ymax - ymin);
    343             for(y = -5 * FACTOR; y < 5 * FACTOR; y++)
    344                 for(x = startx - 5 * FACTOR; x < startx + 5 * FACTOR; x++)
     337            for(y = -5; y < 5; y++)
     338                for(x = startx - 5; x < startx + 5; x++)
    345339                {
    346340                    int z, t, dist;
     
    350344                        {
    351345                            int r2;
    352                             getgray(font, xmin + z, ymin + t, &r);
     346                            getgray(font.img, xmin + z, ymin + t, &r);
    353347                            getgray(img, x + z, y + t, &r2);
    354348                            dist += abs(r - r2);
     
    356350    //                printf("%i %i -> %i\n", x, y, dist);
    357351                    //dist /= sqrt(xmax - xmin);
    358                     dist = dist * 128 / glyphs[i].count;
     352                    dist = dist * 128 / font.glyphs[i].count;
    359353                    if(dist < localmin)
    360354                    {
     
    378372
    379373        /* Print min glyph */
    380         xmin = glyphs[distch].xmin;
    381         ymin = glyphs[distch].ymin;
    382         xmax = glyphs[distch].xmax;
    383         ymax = glyphs[distch].ymax;
     374        xmin = font.glyphs[distch].xmin;
     375        ymin = font.glyphs[distch].ymin;
     376        xmax = font.glyphs[distch].xmax;
     377        ymax = font.glyphs[distch].ymax;
    384378        for(y = 0; y < ymax - ymin; y++)
    385379            for(x = 0; x < xmax - xmin; x++)
    386380            {
    387                 getpixel(font, xmin + x, ymin + y, &r, &g, &b);
     381                getpixel(font.img, xmin + x, ymin + y, &r, &g, &b);
    388382                if(r > 128) continue;
    389383                setpixel(tmp, distx + x, disty + y, r, g, b);
Note: See TracChangeset for help on using the changeset viewer.