6 #include "SDL/SDL_ttf.h"
7 #include "SDL/SDL_image.h"
9 void warn(char *fmt, ...)
14 vfprintf(stderr, fmt, list);
16 fputs(strerror(e), stderr);
20 void warnx(char *fmt, ...)
24 vfprintf(stderr, fmt, list);
28 void err(int ex, char *fmt, ...)
33 vfprintf(stderr, fmt, list);
35 fputs(strerror(e), stderr);
40 void errx(int ex, char *fmt, ...)
44 vfprintf(stderr, fmt, list);
49 void Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
52 unsigned char *buffer, *out;
53 const unsigned char *in, *end;
56 buffer = (unsigned char *)malloc(width*height*4 + 18);
58 memset (buffer, 0, 18);
59 buffer[2] = 2; // uncompressed type
60 buffer[12] = (width >> 0) & 0xFF;
61 buffer[13] = (width >> 8) & 0xFF;
62 buffer[14] = (height >> 0) & 0xFF;
63 buffer[15] = (height >> 8) & 0xFF;
65 for (y = 3;y < width*height*4;y += 4)
69 if (y < width*height*4)
71 // save the alpha channel
72 buffer[16] = 32; // pixel size
73 buffer[17] = 8; // 8 bits of alpha
77 for (y = height - 1;y >= 0;y--)
79 memcpy(out, data + y * width * 4, width * 4);
85 // save only the color channels
86 buffer[16] = 24; // pixel size
87 buffer[17] = 0; // 8 bits of alpha
89 // truncate bgra to bgr and flip upside down
91 for (y = height - 1;y >= 0;y--)
93 in = data + y * width * 4;
95 for (;in < end;in += 4)
104 f = fopen(filename, "wb");
107 if(fwrite(buffer, out - buffer, 1, f) != 1)
116 * Return the pixel value at (x, y)
117 * NOTE: The surface must be locked before calling this!
119 Uint32 getpixel(SDL_Surface *surface, int x, int y)
121 int bpp = surface->format->BytesPerPixel;
122 /* Here p is the address to the pixel we want to retrieve */
123 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
133 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
134 return p[0] << 16 | p[1] << 8 | p[2];
136 return p[0] | p[1] << 8 | p[2] << 16;
142 return 0; /* shouldn't happen, but avoids warnings */
147 * Set the pixel at (x, y) to the given value
148 * NOTE: The surface must be locked before calling this!
150 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
152 int bpp = surface->format->BytesPerPixel;
153 /* Here p is the address to the pixel we want to set */
154 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
162 *(Uint16 *)p = pixel;
166 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
167 p[0] = (pixel >> 16) & 0xff;
168 p[1] = (pixel >> 8) & 0xff;
172 p[1] = (pixel >> 8) & 0xff;
173 p[2] = (pixel >> 16) & 0xff;
178 *(Uint32 *)p = pixel;
183 #define MIN(a,b) (((a)<(b))?(a):(b))
184 #define MAX(a,b) (((a)>(b))?(a):(b))
185 #define BOUND(a,b,c) MAX(a,MIN(b,c))
186 #define BLURFUNC(d,A,B) A-B*(d)
187 #define BLURFUNCIMAX(A,B) ceil(sqrt((A)/(B)))
189 Uint32 getpixelfilter(SDL_Surface *src, SDL_PixelFormat *fmt, int x, int y, double A, double B, double C)
191 double r, g, b, a, f;
192 Uint8 pr, pg, pb, pa;
194 int imax = (int) BLURFUNCIMAX(A,B);
196 // 1. calculate blackened blurred image
198 for(i=-imax; i<=imax; ++i)
199 if(y+i >= 0 && y+i < src->h)
200 for(j=-imax; j<=imax; ++j)
201 if(x+j >= 0 && x+j < src->w)
203 SDL_GetRGBA(getpixel(src, x+j, y+i), src->format, &pr, &pg, &pb, &pa);
204 f = BLURFUNC(i*i+j*j, A, B);
216 // 2. overlap it with the actual image again
217 if(y >= 0 && y < src->h && x >= 0 && x < src->w)
219 SDL_GetRGBA(getpixel(src, x, y), src->format, &pr, &pg, &pb, &pa);
221 f = a + pa - (a * pa) / 255L;
238 r = g = b = MAX(0, 255 - C * (255 - a));
240 r = g = b = MAX(0, 255 + C * a);
242 return SDL_MapRGBA(fmt, (unsigned char) r, (unsigned char) g, (unsigned char) b, (unsigned char) a);
245 void blitfilter(SDL_Surface *src, SDL_Surface *dest, int x0, int y0, double A, double B, double C)
247 // note: x0, y0 is the origin of the UNFILTERED image; it is "transparently" expanded by a BLURFUNCIMAX.
248 int x, y, d, xa, ya, xb, yb;
250 d = (int) BLURFUNCIMAX(A,B);
251 SDL_LockSurface(src);
252 SDL_LockSurface(dest);
256 xb = x0 + src->w + d;
257 yb = y0 + src->h + d;
261 if(xa >= dest->w) xa = dest->w - 1;
262 if(ya >= dest->h) ya = dest->h - 1;
263 for(y = ya; y <= yb; ++y)
264 for(x = xa; x <= xb; ++x)
265 putpixel(dest, x, y, getpixelfilter(src, dest->format, x - x0, y - y0, A, B, C));
266 SDL_UnlockSurface(dest);
267 SDL_UnlockSurface(src);
270 int mapFont(int d, char *c_)
272 unsigned char *c = (unsigned char *) c_;
274 return ((*c >= 0x20 && *c <= 0x7E) || (*c >= 0xA0 && *c <= 0xFE)) ? 0 : -1;
275 if(*c >= 0x20 && *c <= 0x7E)
277 if(*c >= 0xA0 && *c <= 0xAF)
282 if(*c >= 0xB0 && *c <= 0xB9)
287 if(*c >= 0xBA && *c <= 0xDF)
292 if(*c >= 0xE0 && *c <= 0xFE)
301 * @brief Blit a surface onto another and stretch it.
302 * With a 4.2 gcc you can use -fopenmp :)
303 * You might want to add some linear fading for scaling up?
305 * @param dst Destination surface
306 * @param src Source surface, if NULL, the destination surface is used
307 * @param drec The target area
308 * @param srec The source area, if NULL, then you suck :P
310 void StretchBlit(SDL_Surface *dst, SDL_Surface *src, SDL_Rect *drec, SDL_Rect *srec)
315 unsigned int freeSource = 0;
317 // To avoid copying copied pixels, that would suck :)
318 src = SDL_ConvertSurface(dst, dst->format, dst->flags);
323 drec = &dst->clip_rect;
325 srec = &src->clip_rect;
327 SDL_LockSurface(dst);
328 SDL_LockSurface(src);
330 double scaleX = (double)srec->w / (double)drec->w;
331 double scaleY = (double)srec->h / (double)drec->h;
335 for(y = drec->y; y < (drec->y + drec->h); ++y)
339 int dy = y - drec->y;
340 for(x = drec->x; x < (drec->x + drec->w); ++x)
344 // dx, dy relative to the drec start
345 int dx = x - drec->x;
347 double dfromX, dfromY, dtoX, dtoY;
348 int fromX, fromY, toX, toY;
349 // Get the pixel range which represents the current pixel
350 // When scaling down this should be a rectangle :)
351 // Otherwise it's just 1 pixel anyway, from==to then
352 dfromX = dx * scaleX;
353 dfromY = dy * scaleY;
354 dtoX = (dx+1) * scaleX;
355 dtoY = (dy+1) * scaleY;
356 // The first and last one usually aren't 100% within this space
357 fromX = (int)dfromX; dfromX = 1.0 - (dfromX - fromX); // invert the from percentage
358 fromY = (int)dfromY; dfromY = 1.0 - (dfromY - fromY);
359 toX = (int)dtoX; dtoX -= toX; // this one is ok
360 toY = (int)dtoY; dtoY -= toY;
362 /* Short explanation:
363 * FROM is where to START, so when it's 5.7, then 30% of the 5th pixel is to be used
364 * TO is where it ENDS, so if it's 8.4, then 40% of the 9th pixel is to be used!
367 // Now get all the pixels and merge them together...
369 unsigned int r, g, b, a, ar, ag, ab;
370 unsigned int count = 0;
371 r = g = b = a = ar = ag = ab = 0;
373 printf("%i %i - %f %f\n", fromX, toX, dfromX, dtoX);*/
375 // when going from one to the next there's usually one
376 // situation where the left pixel has a value of 0.1something and
377 // the right one of 0
378 // so adjust the values here
379 // otherwise we get lines in the image with the original color
381 if(toX - fromX == 1 && drec->w > srec->w) {
396 if(toY - fromY == 1 && drec->h > srec->h) {
411 for(j = fromY; j <= toY; ++j)
415 if((j+srec->y) >= src->h)
417 for(i = fromX; i <= toX; ++i)
419 Uint8 pr, pg, pb, pa;
420 Uint16 par, pag, pab;
424 if((i+srec->x) >= src->w)
427 SDL_GetRGBA(getpixel(src, i + srec->x, j + srec->y), src->format, &pr, &pg, &pb, &pa);
428 par = pa * (unsigned int)pr;
429 pag = pa * (unsigned int)pg;
430 pab = pa * (unsigned int)pb;
441 int iinc = (int) (inc * 256);
454 //printf("COLOR VALUE: %i, %i, %i, %i \t COUNT: %f\n", r, g, b, a, count);
470 putpixel(dst, x, y, SDL_MapRGBA(dst->format, (Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a));
474 SDL_UnlockSurface(dst);
475 SDL_UnlockSurface(src);
478 SDL_FreeSurface(src);
481 void StretchDown(SDL_Surface *srfc, int x, int y, int w, int h, int wtarget)
483 // @"#$ SDL has no StretchBlit
484 // this one is slow, but at least I know how it works
486 unsigned int stretchedline[8 * wtarget]; // ra ga ba r g b a n
487 SDL_LockSurface(srfc);
489 for(r = y; r < y + h; ++r)
491 // each input pixel is wtarget pixels "worth"
492 //memset(stretchedline, sizeof(stretchedline), 0);
493 memset(stretchedline, 0, sizeof(stretchedline));
494 for(c = 0; c < w * wtarget; ++c)
496 Uint8 pr, pg, pb, pa;
497 unsigned int *p = &stretchedline[8 * (c / w)];
498 SDL_GetRGBA(getpixel(srfc, x + c / wtarget, r), srfc->format, &pr, &pg, &pb, &pa);
499 p[0] += (unsigned int) pr * (unsigned int) pa;
500 p[1] += (unsigned int) pg * (unsigned int) pa;
501 p[2] += (unsigned int) pb * (unsigned int) pa;
502 p[3] += (unsigned int) pr;
503 p[4] += (unsigned int) pg;
504 p[5] += (unsigned int) pb;
505 p[6] += (unsigned int) pa;
508 for(c = 0; c < wtarget; ++c)
510 unsigned int *p = &stretchedline[8 * c];
512 putpixel(srfc, x + c, r, SDL_MapRGBA(srfc->format, p[0] / p[6], p[1] / p[6], p[2] / p[6], p[6] / p[7]));
514 putpixel(srfc, x + c, r, SDL_MapRGBA(srfc->format, p[3] / p[7], p[4] / p[7], p[5] / p[7], p[6] / p[7]));
516 for(c = wtarget; c < w; ++c)
517 putpixel(srfc, x + c, r, SDL_MapRGBA(srfc->format, 0, 0, 0, 0));
520 SDL_UnlockSurface(srfc);
523 int GetBoundingBox(SDL_Surface *surf, const SDL_Rect *inbox, SDL_Rect *outbox)
525 int bx = -1, by = -1; // start
526 //int bw = 0, bh = 0;
527 int ex = -1, ey = -1; // end
529 for(cx = inbox->x; cx < inbox->x + inbox->w; ++cx)
531 for(cy = inbox->y; cy < inbox->y + inbox->h; ++cy)
533 Uint8 pr, pg, pb, pa;
534 SDL_GetRGBA(getpixel(surf, cx, cy), surf->format, &pr, &pg, &pb, &pa);
535 // include colors, or only care about pa?
545 if(cx < bx) // a pixel more on the left
547 /*if(cy < by) // a pixel more above... wait... this cannot happen actually
549 if(cx > ex) // a pixel on the right
551 if(cy > ey) // a pixel on the bottom :)
561 outbox->w = (ex - bx + 1);
562 outbox->h = (ey - by + 1);
566 int main(int argc, char **argv)
569 SDL_Surface *conchars, *conchars0;
572 SDL_Color white = {255, 255, 255, 255};
574 int maxAscent, maxDescent, maxWidth;
580 errx(1, "Usage: %s infile.tga topref bottomref cellheight outfile.tga font.ttf fontCOOL.ttf fontLCD.ttf blurA blurB blurColors\n", argv[0]);
582 const char *infilename = argv[1];
583 int referenceTop = atoi(argv[2]);
584 int referenceBottom = atoi(argv[3]);
585 int cell = atoi(argv[4]);
586 const char *outfilename = argv[5];
587 const char *font0 = argv[6];
588 const char *font1 = argv[7];
589 const char *font2 = argv[8];
590 double A = atof(argv[9]);
591 double B = atof(argv[10]);
592 double C = atof(argv[11]);
595 char widthfilename[512];
596 snprintf(widthfilename, sizeof(widthfilename), "%.*s.width", (int)strlen(outfilename) - 4, outfilename);
598 int border=(int) BLURFUNCIMAX(A, B);
600 fprintf(stderr, "Using %d border pixels\n", border);
603 errx(1, "SDL_Init failed");
606 errx(1, "TTF_Init failed: %s", TTF_GetError());
608 conchars0 = IMG_Load(infilename);
610 errx(1, "IMG_Load failed: %s", IMG_GetError());
612 if(conchars0->w != conchars0->h)
613 errx(1, "conchars aren't square");
614 if(conchars0->w % 16)
615 errx(1, "conchars have bad width");
617 conchars = SDL_CreateRGBSurface(SDL_SWSURFACE, cell * 16, cell * 16, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
618 in.x = in.y = out.x = out.y = 0;
619 in.w = in.h = conchars0->w;
620 out.w = out.h = cell * 16;
621 StretchBlit(conchars, conchars0, &out, &in);
622 SDL_FreeSurface(conchars0);
624 for(currentSize = cell * 2; currentSize; --currentSize)
626 fonts[0] = TTF_OpenFont(font0, currentSize);
628 errx(1, "TTF_OpenFont %s failed: %s", font0, TTF_GetError());
630 if(strcmp(font0, font1) || strcmp(font0, font2))
634 fonts[1] = TTF_OpenFont(font1, currentSize);
636 warnx("TTF_OpenFont %s failed: %s", font1, TTF_GetError());
643 fonts[2] = TTF_OpenFont(font2, currentSize);
645 warnx("TTF_OpenFont %s failed: %s", font2, TTF_GetError());
654 fonts[1] = fonts[2] = fonts[0];
658 //maxAscent = MAX(MAX(TTF_FontAscent(fonts[0]), fonts[1] ? TTF_FontAscent(fonts[1]) : 0), fonts[2] ? TTF_FontAscent(fonts[2]) : 0);
659 //maxDescent = -MIN(MIN(TTF_FontDescent(fonts[0]), fonts[1] ? TTF_FontDescent(fonts[1]) : 0), fonts[2] ? TTF_FontDescent(fonts[2]) : 0);
663 for(i = 0; i < 256; ++i)
665 char str[2]; str[0] = i; str[1] = 0;
666 int fntid = mapFont(differentFonts, &str[0]);
671 glyph = TTF_RenderText_Blended(fonts[fntid], str, white);
673 errx(1, "TTF_RenderText_Blended %d failed: %s", i, TTF_GetError());
675 maxWidth = MAX(maxWidth, glyph->w);
681 if(GetBoundingBox(glyph, &in, &out))
683 int baseline = TTF_FontAscent(fonts[fntid]);
684 int asc = baseline - out.y;
685 int desc = (out.y + out.h - 1) - baseline;
688 if(desc > maxDescent)
692 SDL_FreeSurface(glyph);
695 if(border + maxAscent + 1 + maxDescent + border <= cell)
696 if(border + maxWidth + border <= cell)
702 TTF_CloseFont(fonts[2]);
704 TTF_CloseFont(fonts[1]);
706 TTF_CloseFont(fonts[0]);
709 errx(1, "Sorry, no suitable size found.");
710 fprintf(stderr, "Using font size %d (%d + 1 + %d)\n", currentSize, maxAscent, maxDescent);
712 isfixed = TTF_FontFaceIsFixedWidth(fonts[0]);
713 if(getenv("FORCE_FIXED"))
716 // TODO convert conchars to BGRA (so the TGA writer can reliably use it)
718 transparent = SDL_MapRGBA(conchars->format, 255, 0, 255, 0);
720 FILE *widthfile = fopen(widthfilename, "w");
722 err(1, "fopen widthfile");
723 fprintf(widthfile, "extraspacing %f ", 0.0);
725 for(i = 0; i < 256; ++i)
730 char str[2]; str[0] = i; str[1] = 0;
733 fprintf(widthfile, "\n");
735 fntid = mapFont(differentFonts, &str[0]);
736 if(fntid < 0 || !fonts[fntid])
739 src.x = cell * (i % 16);
740 src.y = cell * (i / 16);
747 glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, cell, cell, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
748 SDL_FillRect(glyph, &src2, transparent);
751 // referenceTop -> (cell - (maxAscent + 1 + maxDescent)) / 2
752 // referenceBottom -> (cell - (maxAscent + 1 + maxDescent)) / 2 + maxAscent
754 int destTop = (cell - (maxAscent + 1 + maxDescent)) / 2;
755 int destBottom = (cell - (maxAscent + 1 + maxDescent)) / 2 + maxAscent;
758 // x' = x / cell * h + y
760 // destTop = referenceTop / cell * h + y
761 // destBottom = referenceBottom / cell * h + y
764 dest.y = (int) ((double) (destBottom * referenceTop - destTop * referenceBottom) / (double) (referenceTop - referenceBottom));
765 dest.h = (int) (cell * (double) (destBottom - destTop) / (double) (referenceBottom - referenceTop));
771 if(dest.w > glyph->w)
773 if(dest.y + dest.h > glyph->h)
774 dest.h = glyph->h - dest.y;
778 dest.w = border + maxWidth + border;
779 StretchBlit(glyph, conchars, &dest, &src);
780 //SDL_FillRect(conchars, &src, transparent);
781 //SDL_BlitSurface(glyph, &src2, conchars, &src);
782 StretchBlit(conchars, glyph, &src, &src2);
783 SDL_FreeSurface(glyph);
784 fprintf(widthfile, "%f ", dest.w / (double) cell);
788 fprintf(stderr, "glyph %d...\n", i);
790 glyph = TTF_RenderText_Blended(fonts[fntid], str, white);
792 errx(1, "TTF_RenderText_Blended %d failed: %s", i, TTF_GetError());
794 w = border + glyph->w + border;
795 h = border + glyph->h + border;
797 warnx("sorry, this font contains a character that is too wide... output will be borked");
799 dest.x = cell * (i % 16);
800 dest.y = cell * (i / 16);
803 SDL_FillRect(conchars, &dest, transparent);
805 dest.x += border + (isfixed ? ((border + maxWidth + border - w) / 2) : 0);
806 dest.y += (cell - (maxAscent + 1 + maxDescent)) / 2 + (maxAscent - TTF_FontAscent(fonts[fntid]));
807 blitfilter(glyph, conchars, dest.x, dest.y, A, B, C);
809 SDL_FreeSurface(glyph);
811 if(isfixed && w > border + maxWidth + border)
813 StretchDown(conchars, cell * (i % 16), cell * (i / 16), w, cell, border + maxWidth + border);
814 fprintf(widthfile, "%f ", (border + maxWidth + border) / (double) cell);
817 fprintf(widthfile, "%f ", (isfixed ? border + maxWidth + border : w) / (double) cell);
820 fprintf(widthfile, "\n");
823 fprintf(stderr, "Writing...\n");
825 Image_WriteTGABGRA(outfilename, conchars->w, conchars->h, (unsigned char *) conchars->pixels);
827 SDL_FreeSurface(conchars);