OpenCores
URL https://opencores.org/ocsvn/orsoc_graphics_accelerator/orsoc_graphics_accelerator/trunk

Subversion Repositories orsoc_graphics_accelerator

[/] [orsoc_graphics_accelerator/] [trunk/] [sw/] [utils/] [bitfontmaker.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 maiden
/*
2
Per Lenander 2012
3
 
4
Bitmap font tool:
5
-----------------
6
 
7
The tool utilize SDL/SDL_image for graphics conversion
8
*/
9
 
10
#include <SDL/SDL.h>
11
#include <SDL/SDL_image.h>
12
 
13
#define SCREEN_WIDTH 640
14
#define SCREEN_HEIGHT 480
15
#define SCREEN_DEPTH 32
16
 
17
#include <sstream>
18
#include <iostream>
19
#include <fstream>
20
#include <cstdlib>
21
 
22
using namespace std;
23
 
24
SDL_Surface *screen;
25
 
26
int spriteW = 0;
27
int spriteH = 0;
28
 
29
/* Ordinary inits */
30
int InitSDL(void)
31
{
32
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
33
    {
34
        cerr << "Unable to init SDL: " << SDL_GetError() << endl;
35
        return 1;
36
    }
37
 
38
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);
39
 
40
    if (!screen)
41
    {
42
        cerr << "Unable to set video: " << SDL_GetError() << endl;
43
        return 1;
44
    }
45
    // make sure SDL cleans up before exit
46
    atexit(SDL_Quit);
47
 
48
    return 0;
49
}
50
 
51
/* Loads the spritesheet (or any image, but this is the only thing it is used for) */
52
SDL_Surface * LoadImage(std::string s)
53
{
54
    SDL_Surface *loaded = NULL;
55
 
56
    loaded = IMG_Load(s.c_str());
57
 
58
    if(!loaded)         return NULL;
59
 
60
    SDL_Surface *optimized = NULL;
61
 
62
    optimized = SDL_DisplayFormatAlpha(loaded);
63
 
64
    SDL_FreeSurface(loaded);
65
 
66
    if(!optimized)              return NULL;
67
 
68
    return optimized;
69
}
70
 
71
SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y )
72
{
73
  SDL_Color color ;
74
  Uint32 col = 0 ;
75
 
76
  //determine position
77
  char* pPosition = ( char* ) pSurface->pixels ;
78
 
79
  //offset by y
80
  pPosition += ( pSurface->pitch * y ) ;
81
 
82
  //offset by x
83
  pPosition += ( pSurface->format->BytesPerPixel * x ) ;
84
 
85
  //copy pixel data
86
  memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;
87
 
88
  //convert color
89
  SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;
90
  return ( color ) ;
91
}
92
 
93
int main ( int argc, char** argv )
94
{
95
    if( argc < 2 )
96
    {
97
        cout << "Usage: " << argv[0] << " filename.<jpg/png/bmp> [bpp=16] [glyphsize=32]" << endl;
98
        return 1;
99
    }
100
 
101
    if( InitSDL() )
102
        return 1;
103
    cout << "SDL successfully initialized." << endl;
104
 
105
    SDL_Surface *spritesheet = LoadImage( argv[1] );
106
    if( spritesheet == NULL )
107
    {
108
        cout << "Couldn't load " << argv[1] << "!" << endl;
109
        return 1;
110
    }
111
 
112
    int outputBpp = 16;
113
    if(argc >= 3)
114
    {
115
        stringstream ss(argv[2]);
116
        ss >> outputBpp;
117
 
118
        if(outputBpp != 8 && outputBpp != 16 && outputBpp != 24 && outputBpp != 32)
119
        {
120
            cout << "Mode: '" << argv[2] << "' not supported, choose between 8, 16, 24, and 32" << endl;
121
            return 1;
122
        }
123
        else if(outputBpp == 8 && spritesheet->w % 4)
124
        {
125
            cout << "Mode: 8bpp requires the width to be divisible by 4!" << endl;
126
            return 1;
127
        }
128
        else if(outputBpp == 16 && spritesheet->w % 2)
129
        {
130
            cout << "Mode: 16bpp requires the width to be divisible by 2!" << endl;
131
            return 1;
132
        }
133
    }
134
 
135
    int stride = 1;
136
    if(outputBpp == 8)
137
        stride = 4;
138
    else if(outputBpp == 16)
139
        stride = 2;
140
 
141
    int glyphsize = 32;
142
    if(argc >= 4)
143
    {
144
        stringstream ss(argv[3]);
145
        ss >> glyphsize;
146
 
147
        if(glyphsize <= 0)
148
        {
149
            cout << "Negative glyph width not supported." << endl;
150
            return 1;
151
        }
152
    }
153
 
154
    string              filename(argv[1]);
155
    filename = filename.substr(0, filename.find('.'));
156
 
157
    /* Open an output file */
158
    string offilename = filename + "_font.h";
159
    ofstream    output(offilename.c_str(),ofstream::out);
160
    if( !output )
161
    {
162
        cout << "Couldn't open output file!" << endl;
163
        return 1;
164
    }
165
 
166
    SDL_Color bg = GetPixel(spritesheet, 0, 0);
167
 
168
    output << "/* Bitmap font definition */" << endl << endl;
169
    output << "/* size: " << spritesheet->w << " * " << spritesheet->h << " at " << outputBpp << "BPP */" << endl << endl;
170
    output << "#ifndef " << filename << "_H" << endl;
171
    output << "#define " << filename << "_H" << endl;
172
 
173
    output << "// Create the font with the following command:" << endl;
174
    output << "// -------------------------------------------" << endl;
175
    output << "orgfx_bitmap_font init_" << filename << "_font(int spacing, int spacewidth);" << endl << endl;
176
    output << "// SPACING refers to the number of pixels between characters in a string." << endl;
177
    output << "// SPACEWIDTH refers to the number of pixels used for the space char." << endl << endl;
178
 
179
 
180
    output << "// Write a string using this syntax:" << endl;
181
    output << "// ---------------------------------" << endl;
182
    output << "// orgfx_put_bitmap_text(&" << filename << "_font, x*FIXEDW, y*FIXEDW, \"Some text\");" << endl << endl;
183
 
184
    output << "orgfx_sprite_rect " << filename << "_glyphs[] = {" << endl;
185
 
186
    for(int glyphid = 0; glyphid < 256; glyphid++)
187
    {
188
        int startX = (glyphid%16) * glyphsize;
189
        int endX = startX + glyphsize;
190
        int startY = (glyphid/16) * glyphsize;
191
        int endY = startY + glyphsize;
192
 
193
        bool foundPixel = false;
194
 
195
        for(int x = endX-1; x >= startX; x--)
196
        {
197
            for(int y = startY; y < endY; y++)
198
            {
199
                SDL_Color col = GetPixel(spritesheet, x, y);
200
                if(col.r != bg.r || col.g != bg.g || col.b != bg.b)
201
                {
202
                    foundPixel = true;
203
                    break;
204
                }
205
            }
206
 
207
            if(foundPixel)
208
                break;
209
 
210
            endX--;
211
        }
212
 
213
        output << "{" << startX << ", " << startY << ", " << endX+1 << ", " << endY << "}, // " << glyphid << endl;
214
    }
215
 
216
    output << "};" << endl << endl;
217
 
218
    output << "unsigned int " << filename << "_img[] = {" << endl;
219
 
220
    for(int y = 0; y < spritesheet->h; y++)
221
    {
222
        for(int x = 0; x < spritesheet->w; x+=stride)
223
        {
224
            unsigned int pixel = 0;
225
            for(int s = 0; s < stride; s++)
226
            {
227
                SDL_Color c = GetPixel(spritesheet, x+s, y);
228
 
229
                if(outputBpp == 8)
230
                {
231
                    pixel += (unsigned char)(0.3 * c.r + 0.59 * c.g + 0.11 * c.b);
232
                    if(s != 3) pixel = pixel << 8;
233
                }
234
                else if(outputBpp == 16)
235
                {
236
                    c.r = c.r >> 3;
237
                    c.g = c.g >> 2;
238
                    c.b = c.b >> 3;
239
                    pixel += (c.r << 11) | (c.g << 5) | c.b;
240
                    if(s != 1) pixel = pixel << 16;
241
                }
242
                else
243
                    pixel = (c.r << 16) | (c.g << 8) | c.b;
244
            }
245
            output << pixel << "u, ";
246
        }
247
        output << endl;
248
    }
249
 
250
    output << "};" << endl << endl;
251
 
252
    output << "orgfx_tileset " << filename << "_ts;" << endl << endl;
253
 
254
    output << "orgfx_bitmap_font init_" << filename << "_font(int spacing, int spacewidth)" << endl;
255
    output << "{" << endl;
256
    output << "  int " << filename << "_surface = orgfxplus_init_surface(" << spritesheet->w << ", " << spritesheet->h << ", " << filename << "_img" << ");" << endl;
257
    output << "  " << filename << "_ts = orgfx_make_tileset(" << filename << "_surface, " << filename << "_glyphs, 256);" << endl;
258
    output << "  return orgfx_make_bitmap_font(&" << filename << "_ts, spacing, spacewidth);" << endl << endl;
259
    output << "}" << endl;
260
 
261
    output << "#endif // " << filename << "_H" << endl;
262
 
263
    output.close();
264
    SDL_FreeSurface( spritesheet );
265
 
266
    return 0;
267
}
268
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.