Add HTML generating code

This commit is contained in:
Arvinder Bhathal 2017-07-08 02:26:59 -04:00
parent 31d84500e8
commit 78e74cf349
9 changed files with 496 additions and 505 deletions

View File

@ -0,0 +1,5 @@
Compile with current ft folder:
gcc main.c bitmap.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -ldl -lpng -lm -o main
Run
./main <base libfreetype.so> <test libfreetype.so> <size> <font file>

View File

@ -1,269 +1,282 @@
#include "bitmap.h"
HASH_128 * Generate_Hash_x64_128(FT_Bitmap * bitmap, HASH_128 * murmur)
HASH_128 * Generate_Hash_x64_128( FT_Bitmap * bitmap,
HASH_128 * murmur)
{
int seed = 99; // Dont change
int seed = 99; // Dont change
MurmurHash3_x64_128(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash);
MurmurHash3_x64_128(bitmap->buffer,
(bitmap->pitch * bitmap->rows),
seed,
murmur->hash);
return murmur;
return murmur;
}
HASH_128 * Generate_Hash_x86_128(FT_Bitmap * bitmap, HASH_128 * murmur)
HASH_128 * Generate_Hash_x86_128( FT_Bitmap * bitmap,
HASH_128 * murmur)
{
int seed = 99; // Dont change
int seed = 99; // Dont change
MurmurHash3_x86_128(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash);
MurmurHash3_x86_128(bitmap->buffer,
(bitmap->pitch * bitmap->rows),
seed,
murmur->hash);
return murmur;
return murmur;
}
HASH_32 * Generate_Hash_x86_32(FT_Bitmap * bitmap, HASH_32 * murmur)
HASH_32 * Generate_Hash_x86_32( FT_Bitmap * bitmap,
HASH_32 * murmur)
{
int seed = 99; // Dont change
int seed = 99; // Dont change
MurmurHash3_x86_32(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash);
MurmurHash3_x86_32( bitmap->buffer,
(bitmap->pitch * bitmap->rows),
seed,
murmur->hash);
return murmur;
return murmur;
}
int Get_Padding( FT_Bitmap* bitmap ){
int rem;
if (bitmap->pixel_mode == 6)
{
rem = ( 3 * bitmap->width ) % 4;
}else{
rem = ( bitmap->width ) % 4;
}
if (!rem )
{
return rem;
}
return (4 - rem);
PIXEL * Pixel_At (IMAGE * bitmap, int x, int y)
{
return bitmap->pixels + bitmap->width * y + x;
}
int Get_Bits_Per_Pixel ( unsigned char PIXEL_MODE) {
if (PIXEL_MODE < 5)
{
return BITS_PER_PIXEL_GRAY;
}else{
return BITS_PER_PIXEL_LCD;
}
}
void Write_Bitmap_Header (FT_Bitmap * bitmap, char * fname ) {
FILE *fp = fopen(fname,"w"); // Bitmap File
HEADER *header = ( HEADER* ) calloc( 1 , sizeof( HEADER ));
int Generate_PNG (IMAGE *bitmap,
const char *path,
int render_mode)
{
FILE * fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
unsigned char pixel_mode = ( bitmap->pixel_mode );
FT_UInt image_width;
FT_UInt image_rows;
FT_UInt color_table_size = 0;
size_t x, y;
png_byte ** row_pointers = NULL;
switch(pixel_mode){
int status = -1;
case 5 : image_width = bitmap->width / 3; // LCD
image_rows = bitmap->rows;
break;
int pixel_size = 4;
int depth = 8;
fp = fopen (path, "wb");
if (! fp) {
goto fopen_failed;
}
case 6 : image_width = bitmap->width; // LCD_V
image_rows = bitmap->rows / 3;
break;
png_ptr = png_create_write_struct ( PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (png_ptr == NULL) {
goto png_create_write_struct_failed;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
goto png_create_info_struct_failed;
}
default : image_width = bitmap->width; // MONO and GRAY
image_rows = bitmap->rows;
color_table_size = 4* 256;
break;
if (setjmp (png_jmpbuf (png_ptr))) {
goto png_failure;
}
png_set_IHDR (png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
depth,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
row_pointers = png_malloc ( png_ptr,
bitmap->height * sizeof (png_byte *));
for (y = 0; y < bitmap->height; y++) {
png_byte *row = png_malloc (png_ptr,
sizeof (uint8_t) * bitmap->width * pixel_size);
row_pointers[y] = row;
for (x = 0; x < bitmap->width; x++) {
PIXEL * pixel = Pixel_At (bitmap, x, y);
if (render_mode == 3 || render_mode == 5)
{
*row++ = pixel->blue;
*row++ = pixel->green;
*row++ = pixel->red;
*row++ = pixel->alpha;
continue;
}
*row++ = pixel->red;
*row++ = pixel->green;
*row++ = pixel->blue;
*row++ = pixel->alpha;
}
}
FT_Int image_size;
image_size = (image_rows * image_width );
png_init_io ( png_ptr,
fp);
header->file_header.type = 0x4D42;
header->file_header.file_size = sizeof(HEADER) + color_table_size + image_size;
header->file_header.file_offset = sizeof(HEADER) + color_table_size;
png_set_rows (png_ptr,
info_ptr,
row_pointers);
header->info_header.info_header_size = sizeof(BMP_INFO_HEADER);
header->info_header.width = image_width ;
header->info_header.height = - image_rows;
header->info_header.planes = PLANES;
header->info_header.bits_per_pixel = Get_Bits_Per_Pixel( bitmap->pixel_mode );
header->info_header.compression = COMPRESSION;
header->info_header.image_size = image_size;
header->info_header.y_pixels_per_meter = Y_PIXELS_PER_METER ;
header->info_header.x_pixels_per_meter = X_PIXELS_PER_METER ;
header->info_header.used_colors = USED_COLORS;
header->info_header.important_colors = IMPORTANT_COLORS;
png_write_png ( png_ptr,
info_ptr,
PNG_TRANSFORM_IDENTITY,
NULL);
fwrite (header, 1, sizeof(HEADER),fp);
free(header);
fclose(fp);
status = 0;
for (y = 0; y < bitmap->height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
png_create_write_struct_failed:
fclose (fp);
fopen_failed:
return status;
}
void Write_Bitmap_Data_LCD_RGB( FT_Bitmap * bitmap, char * fname ){
void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
char value;
int i,j,k;
IMAGE fruit;
int x;
int y;
FILE *fp = fopen(fname,"a");
unsigned char value;
int p;
for (i = 0; i < bitmap->rows ; ++i)
{
for ( j = 2; j < bitmap->width; j = j+3)
{
for ( k = 0; k < 3; ++k)
{
value = 0xff - bitmap->buffer[( i * bitmap->pitch) + j - k];
fwrite (&value, 1, 1,fp);
}
}
for ( k = 0; k < Get_Padding(bitmap); ++k)
{
value = 0xff;
fwrite (&value, 1, 1,fp);
}
}
switch(render_mode){
fclose(fp);
}
case 0 : fruit.width = bitmap->width; // MONO and GRAY
fruit.height = bitmap->rows;
void Write_Bitmap_Data_LCD_BGR( FT_Bitmap * bitmap, char * fname ){
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
char value;
int i,j,k;
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
FILE *fp = fopen(fname,"a");
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = (y * bitmap->pitch ) + x;
for ( i = 0; i < bitmap->rows ; ++i)
{
for ( j = 0; j < bitmap->width; j++)
{
value = 0xff - bitmap->buffer[( i * bitmap->pitch) + j];
fwrite (&value, 1, 1,fp);
}
for ( k = 0; k < Get_Padding(bitmap); ++k)
{
value = 0xff;
fwrite (&value, 1, 1,fp);
}
}
value = bitmap->buffer[p];
if ( value != 0x00 ){
value = 0xff;
}else{
value = 0x00;
}
fclose(fp);
}
pixel->red = 255- value;
pixel->green = 255- value;
pixel->blue = 255- value;
pixel->alpha = 255;
}
}
break;
case 1 : fruit.width = bitmap->width; // MONO and GRAY
fruit.height = bitmap->rows;
void Write_Bitmap_Data_LCD_V_BGR (FT_Bitmap * bitmap, char * fname) {
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
FILE *fp = fopen(fname,"a");
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
int i,j,k,step;
char value;
step = 0;
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = (y * bitmap->pitch ) + x;
while ( step < bitmap->rows ){
value = bitmap->buffer[p];
pixel->red = 255- value;
pixel->green = 255- value;
pixel->blue = 255- value;
pixel->alpha = 255;
}
}
break;
for (i = 0; i < bitmap->width; i++)
{
for (j = step ; j < step + 3; ++j)
{
value = 0xff - bitmap->buffer[(j * bitmap->pitch) + i];
fwrite (&value, 1, 1,fp);
}
}
for (k = 0; k < Get_Padding(bitmap); ++k)
{
value = 0xff;
fwrite (&value, 1, 1,fp);
}
step = step + 3; // Jumping 3 rows up
}
case 2 :
case 3 : fruit.width = bitmap->width / 3; // LCD
fruit.height = bitmap->rows;
fclose(fp);
}
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
void Write_Bitmap_Data_LCD_V_RGB (FT_Bitmap * bitmap, char * fname) {
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
FILE *fp = fopen(fname,"a");
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = (y * bitmap->pitch ) + (x)*3;
int i,j,k,step;
char value;
step = 0;
value = bitmap->buffer[p];
pixel->red = 255- value;
p++;
while ( step < bitmap->rows ){
value = bitmap->buffer[p];
pixel->green = 255- value;
p++;
for (i = 0; i < bitmap->width; i++)
{
for (j = step + 2 ; j >= step; --j)
{
value = 0xff - bitmap->buffer[(j * bitmap->pitch) + i];
fwrite (&value, 1, 1,fp);
}
}
for (k = 0; k < Get_Padding(bitmap); ++k)
{
value = 0xff;
fwrite (&value, 1, 1,fp);
}
step = step + 3; // Jumping 3 rows up
}
value = bitmap->buffer[p];
pixel->blue = 255- value;
fclose(fp);
}
pixel->alpha = 255;
}
}
break;
void Write_Bitmap_Data_MONO (FT_Bitmap * bitmap, char * fname) {
case 4 :
case 5 : fruit.width = bitmap->width; // LCD_V
fruit.height = bitmap->rows / 3;
FILE *fp = fopen(fname,"a");
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
int i;
unsigned char value;
for ( i = 0; i < 256; ++i)
{
value = i;
fwrite (&value,1,1,fp);
fwrite (&value,1,1,fp);
fwrite (&value,1,1,fp);
value = 0;
fwrite (&value,1,1,fp);
}
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = ((y*3) * bitmap->pitch ) + x;
for (int i = 0; i < bitmap->pitch * bitmap->rows; ++i)
{
if ( bitmap->buffer[i] != 0x00 ){
value = 0x00; // remember taking reverse
fwrite (&value, 1, 1,fp);
}else{
value = 0xff;
fwrite (&value, 1, 1,fp);
}
}
value = bitmap->buffer[p];
pixel->red = 255- value;
p += bitmap->pitch;
fclose(fp);
}
value = bitmap->buffer[p];
pixel->green = 255- value;
p += bitmap->pitch;
void Write_Bitmap_Data_GRAY(FT_Bitmap * bitmap, char * fname) {
value = bitmap->buffer[p];
pixel->blue = 255- value;
FILE *fp = fopen(fname,"a");
int i;
pixel->alpha = 255;
}
}
break;
unsigned char value;
for ( i = 0; i < 256; ++i)
{
value = i;
fwrite (&value,1,1,fp);
fwrite (&value,1,1,fp);
fwrite (&value,1,1,fp);
value = 0;
fwrite (&value,1,1,fp);
}
default : fruit.width = bitmap->width;
fruit.height = bitmap->rows;
break;
}
for (int i = 0; i < bitmap->pitch * bitmap->rows; ++i)
{
value = 255 - bitmap->buffer[i];
fwrite(&value,1,1,fp);
}
char * file_name = ( char * )calloc(150,sizeof(char));
fclose(fp);
sprintf(file_name, "%s_%d.png", name, i);
Generate_PNG (& fruit, file_name, render_mode);
free (fruit.pixels);
}

View File

@ -1,86 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ft2build.h>
#include <string.h>
#include "murmur3.h" // MurmurHash3_x64_128 header file
#include <png.h>
#include FT_FREETYPE_H
#include FT_MODULE_H
#include FT_LCD_FILTER_H
#include FT_BITMAP_H
#define BITS_PER_PIXEL_MONO 1 // Constants for the Bitmap Header
#define BITS_PER_PIXEL_GRAY 8
#define BITS_PER_PIXEL_LCD 24
#define BITS_PER_PIXEL_RGBA 32
#define PLANES 1 // Constants for the Bitmap Header
#define COMPRESSION 0
#define X_PIXELS_PER_METER 0
#define Y_PIXELS_PER_METER 0
#define USED_COLORS 0
#define IMPORTANT_COLORS 0
//-------------------------------------------------------------------------------
#pragma pack(push,1)
typedef struct{ // Bitmap FILE Header
FT_UInt16 type;
FT_UInt32 file_size;
FT_UInt32 reserved;
FT_UInt32 file_offset;
} BMP_FILE_HEADER;
typedef struct{ // Bitmap INFO Header
FT_UInt32 info_header_size;
FT_UInt32 width;
FT_Int height;
FT_UInt16 planes;
FT_UInt16 bits_per_pixel;
FT_UInt32 compression;
FT_UInt32 image_size;
FT_UInt32 y_pixels_per_meter;
FT_UInt32 x_pixels_per_meter;
FT_UInt32 used_colors;
FT_UInt32 important_colors;
} BMP_INFO_HEADER;
typedef struct { // Bitmap Header
BMP_FILE_HEADER file_header;
BMP_INFO_HEADER info_header;
} HEADER;
//------------------------------------------------------------------------------
typedef struct { // To store 32bit Hash
FT_UInt32 hash[1];
FT_UInt32 hash[1];
}HASH_32;
typedef struct { // To store 128bit Hash
FT_UInt32 hash[4];
FT_UInt32 hash[4];
}HASH_128;
#pragma pack(pop)
typedef struct {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} PIXEL;
//-------------------------------------------------------------------------------
typedef struct {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} PIXEL_BGRA;
/* A picture. */
typedef struct {
PIXEL *pixels;
size_t width;
size_t height;
} IMAGE;
//------------------------------------------------------------------------------
HASH_32 * Generate_Hash_x86_32(FT_Bitmap * bitmap, HASH_32 * murmur);
HASH_128 * Generate_Hash_x86_128(FT_Bitmap * bitmap, HASH_128 * murmur);
HASH_128 * Generate_Hash_x64_128(FT_Bitmap * bitmap, HASH_128 * murmur);
//-------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int Get_Padding (FT_Bitmap * bitmap);
PIXEL * Pixel_At (IMAGE * bitmap, int x, int y); // Returns a pointer to pixel
// at (x,y) co-ordinate
// buffer to image
void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode);
// Image to file
int Generate_PNG (IMAGE *bitmap, const char *path,int render_mode);
int Get_Bits_Per_Pixel ( unsigned char PIXEL_MODE );
void Write_Bitmap_Header (FT_Bitmap * bitmap, char * fname);
void Write_Bitmap_Data_MONO (FT_Bitmap * bitmap, char * fname);
void Write_Bitmap_Data_GRAY (FT_Bitmap * bitmap, char * fname);
void Write_Bitmap_Data_LCD_BGR (FT_Bitmap * bitmap, char * fname);
void Write_Bitmap_Data_LCD_RGB (FT_Bitmap * bitmap, char * fname);
void Write_Bitmap_Data_LCD_V_RGB (FT_Bitmap * bitmap, char * fname);
void Write_Bitmap_Data_LCD_V_BGR (FT_Bitmap * bitmap, char * fname);

View File

@ -1,103 +0,0 @@
#include "bitmap.h"
int main(int argc, char const * argv[])
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Bitmap * bitmap;
FT_UInt32 size;
const char * filename;
int i, j;
char mmhash[40];
unsigned int lines;
int line_len = 40;
char ** hashes;
char fname[20];
FILE * fp = fopen("old-hashes.txt", "r");
filename = argv[1];
size = atoi(argv[2]);
if (FT_Init_FreeType(&library))
{
printf("Error: library init.\n");
}
if (FT_New_Face(library, filename, 0, &face))
{
printf("Error: loading face.\n");
}
lines = face->num_glyphs;
hashes = (char **)malloc(sizeof(char*)*lines);
for (i = 0; i < lines; i++)
{
hashes[i] = malloc(line_len);
if (hashes[i]==NULL)
{
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(hashes[i],line_len-1,fp)==NULL)
break;
/* Get rid of CR or LF at end of line */
for (j=strlen(hashes[i])-1;j>=0 && (hashes[i][j]=='\n' || hashes[i][j]=='\r');j--)
;
hashes[i][j+1]='\0';
}
fclose(fp);
if (FT_Set_Char_Size(face, size * 64, 0, 96, 0))
{
printf("Error: setting char size.\n");
}
slot = face->glyph;
for (i = 0; i < face->num_glyphs; ++i)
{
if (FT_Load_Glyph(face, i, FT_LOAD_DEFAULT))
{
printf("Error: loading glyph.\n");
}
if (FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL))
{
printf("Error: rendering glyph.\n");
}
bitmap = &slot->bitmap;
HASH_128 * murmur = (HASH_128 *)malloc(sizeof(HASH_128));
murmur = Generate_Hash_x64_128(bitmap,murmur);
sprintf(mmhash, "%08x%08x%08x%08x", murmur->hash[0], murmur->hash[1], murmur->hash[2], murmur->hash[3]);
printf("%s\n", hashes[i]);
printf("%s\n", mmhash);
if (strcmp(mmhash, hashes[i]) == 0)
{
if (bitmap->width != 0 && bitmap->rows !=0)
{
printf("Glyph %d differs.\n", i);
sprintf(fname, "%d-new.bmp", i);
Write_Bitmap_Header(bitmap, fname);
Write_Bitmap_Data_GRAY(bitmap, fname);
}
}
}
FT_Done_Face(face);
FT_Done_FreeType(library);
return 0;
}

View File

@ -1,14 +0,0 @@
// consolidate code into this file
#include "bitmap.h"
#include <dlfcn.h>
int main(int argc, char const *argv[])
{
if(argc != 5)
{
printf("Usage: %s <base ft.so dir> <test ft.so dir> <font dir> <char size>", argv[0]);
}
return 0;
}

View File

@ -1,64 +0,0 @@
#include "bitmap.h"
int main(int argc, char const * argv[])
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Bitmap * bitmap;
FT_UInt32 size;
const char * filename;
int i;
FILE * fp = fopen("old-hashes.txt", "w");
filename = argv[1];
size = atoi(argv[2]);
if (FT_Init_FreeType(&library))
{
printf("Error: library init.\n");
}
if (FT_New_Face(library, filename, 0, &face))
{
printf("Error: loading face.\n");
}
if (FT_Set_Char_Size(face, size * 64, 0, 96, 0))
{
printf("Error: setting char size.\n");
}
slot = face->glyph;
for (i = 0; i < face->num_glyphs; ++i)
{
if (FT_Load_Glyph(face, i, FT_LOAD_DEFAULT))
{
printf("Error: loading glyph.\n");
}
if (FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL))
{
printf("Error: rendering glyph.\n");
}
bitmap = &slot->bitmap;
HASH_128 * murmur = (HASH_128 *)malloc(sizeof(HASH_128));
murmur = Generate_Hash_x64_128(bitmap, murmur);
fprintf(fp, "%08x%08x%08x%08x\n", murmur->hash[0], murmur->hash[1], murmur->hash[2], murmur->hash[3]);
}
fclose(fp);
FT_Done_Face(face);
FT_Done_FreeType(library);
return 0;
}

View File

@ -1,48 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>tests/web_interface-test</title>
<style>
img {
display: none;
}
table {
margin: 0 auto;
}
</style>
</head>
<body onload="draw();">
<table>
<tr>
<td><img src="65"></td>
<td><img src="65-2"></td>
</tr>
<tr>
<td><img src="66"></td>
<td><img src="66-2"></td>
</tr>
</table>
<script>
function draw() {
for (var i = 0; i < document.images.length; i++) {
canvas = document.createElement('canvas');
canvas.setAttribute('width', 300);
canvas.setAttribute('height', 300);
document.images[i].parentNode.insertBefore(canvas,document.images[i]);
ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
ctx.drawImage(document.images[i], 0, 0, 60, 60);
}
}
</script>
</body>
</html>

230
tests/web_interface/main.c Normal file
View File

@ -0,0 +1,230 @@
#include "bitmap.h"
#include <dlfcn.h>
#include <math.h>
struct entry
{
int code;
char base_img[256];
char test_img[256];
char base_hash[33];
char test_hash[33];
double base_value;
double test_value;
double difference;
};
void render(const char*, const char*, FT_UInt32, int, struct entry (*entries)[], int*, int*);
int compare(const void* e1, const void* e2);
void make_html(struct entry (*entries)[], int* num, const char*);
int main(int argc, char const *argv[])
{
const char* base_ft;
const char* test_ft;
FT_UInt32 size;
const char* font;
int num = 0;
int max = 0;
int mode;
struct entry entries[2000];
for (int i = 0; i < 2000; ++i)
{
entries[i].difference = 0.0;
}
if(argc != 5)
{
printf("Usage: %s <base ft.so> <test ft.so> <char size> <font>\n", argv[0]);
return 0;
}
base_ft = argv[1];
test_ft = argv[2];
size = atoi(argv[3]);
font = argv[4];
mode = 0; // base hashes
render(base_ft, font, size, mode, &entries, &num, &max);
mode = 1; // test hashes
render(test_ft, font, size, mode, &entries, &num, &max);
mode = 2; // base images for differing glyphs
render(test_ft, font, size, mode, &entries, &num, &max);
mode = 3; // test images for differing glyphs
render(test_ft, font, size, mode, &entries, &num, &max);
qsort(entries, 2000, sizeof(struct entry), compare);
make_html(&entries, &num, font);
}
void make_html(struct entry (*entries)[], int* num, const char* font)
{
FILE *fp = fopen("index.html", "w");
if (fp == NULL)
{
printf("Error opening file.\n");
exit(1);
}
fprintf(fp, "<!DOCTYPE html>\n<html>\n<head>\n<style>\nimg{image-rendering: optimizeSpeed;image-rendering: -moz-crisp-edges;image-rendering: -o-crisp-edges;image-rendering: -webkit-optimize-contrast;image-rendering: pixelated;image-rendering: optimize-contrast;-ms-interpolation-mode: nearest-neighbor;min-width:10%%}\ntable, th, td{\nborder: 1px solid black;\n}\n</style>\n</head>\n\n<body>\n");
fprintf(fp, "<p>%s</p>\n<table style=\"width:100%%\">", font);
fprintf(fp, "<tr><th>ID</th><th>Difference</th><th>Base glyph | Test glyph</th></tr>\n");
for (int i = 0; i < *num; ++i)
{
fprintf(fp, "<tr><td>%d</td><td>%.2f</td><td><img src=\"%s\"</img> <img src=\"%s\"</img></td></tr>\n", (*entries)[i].code, (*entries)[i].difference, (*entries)[i].base_img, (*entries)[i].test_img);
}
fprintf(fp, "</table>\n</body>\n</html>");
fclose(fp);
}
int compare (const void* e1, const void* e2)
{
struct entry *s1 = (struct entry *)e1;
struct entry *s2 = (struct entry *)e2;
int comp = (int)(s1->difference) - (int)(s2->difference);
return -comp;
}
// mode 0: hash base
// mode 1: hash test
// mode 2: image base
// mode 3: image test
void render(const char* ft_dir, const char* font, FT_UInt32 size, int mode, struct entry (*entries)[], int* num, int* max)
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Bitmap *bitmap;
FT_Error error;
int i;
FT_Error (*ft_init_fun)(FT_Library*);
FT_Error (*ft_newface_fun)(FT_Library, const char*, FT_Long, FT_Face*);
FT_Error (*ft_setcharsize_fun)(FT_Face, FT_F26Dot6, FT_F26Dot6, FT_UInt,
FT_UInt);
FT_Error (*ft_loadglyph_fun)(FT_Face, FT_UInt, FT_Int32);
FT_Error (*ft_renderglyph_fun)(FT_GlyphSlot, FT_Render_Mode);
FT_Error (*ft_doneface_fun)(FT_Face);
FT_Error (*ft_donefreetype_fun)(FT_Library);
void (*ft_bitmapinit_fun)(FT_Bitmap*);
FT_Error (*ft_bitmapconvert_fun)(FT_Library, const FT_Bitmap*, FT_Bitmap,
FT_Int);
void* handle = dlopen(ft_dir, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
if (!handle) {
fputs(dlerror(), stderr);
exit(1);
}
dlerror();
*(void**)(&ft_init_fun) = dlsym(handle,"FT_Init_FreeType");
*(void**)(&ft_newface_fun) = dlsym(handle,"FT_New_Face");
*(void**)(&ft_setcharsize_fun) = dlsym(handle,"FT_Set_Char_Size");
*(void**)(&ft_loadglyph_fun) = dlsym(handle,"FT_Load_Glyph");
*(void**)(&ft_renderglyph_fun) = dlsym(handle,"FT_Render_Glyph");
*(void**)(&ft_doneface_fun) = dlsym(handle,"FT_Done_Face");
*(void**)(&ft_donefreetype_fun) = dlsym(handle,"FT_Done_FreeType");
*(void**)(&ft_bitmapinit_fun) = dlsym(handle,"FT_Bitmap_Init");
*(void**)(&ft_bitmapconvert_fun) = dlsym(handle,"FT_Bitmap_Convert");
dlerror();
error = ft_init_fun(&library);
if (error)
{
printf("Error: library init");
}
error = ft_newface_fun(library, font, 0, &face);
if (error)
{
printf("Error: loading face");
}
error = ft_setcharsize_fun(face, size * 64, 0, 96, 0);
if (error)
{
printf("Error: setting char size");
}
slot = face->glyph;
for (i = 0; i < face->num_glyphs; ++i)
{
(*entries)[i].code = i;
if (((mode == 2) || (mode == 3)) &&
(strcmp((*entries)[i].base_hash, (*entries)[i].test_hash) == 0))
{
continue;
}
error = ft_loadglyph_fun(face, i, FT_LOAD_DEFAULT);
if (error)
{
printf("Error: loading glyph");
}
error = ft_renderglyph_fun(slot, FT_RENDER_MODE_NORMAL);
if (error)
{
printf("Error: rendering glyph");
}
bitmap = &slot->bitmap;
if ((mode == 0) || (mode == 1))
{
HASH_128 * murmur = (HASH_128 *)malloc(sizeof(HASH_128));
murmur = Generate_Hash_x64_128(bitmap, murmur);
if (mode == 0)
{
sprintf((*entries)[i].base_hash, "%08x%08x%08x%08x", murmur->hash[0], murmur->hash[1],
murmur->hash[2], murmur->hash[3]);
}
else
{
sprintf((*entries)[i].test_hash, "%08x%08x%08x%08x", murmur->hash[0], murmur->hash[1],
murmur->hash[2], murmur->hash[3]);
}
} else {
*max = i;
if (bitmap->width == 0 || bitmap->rows == 0)
{
continue;
}
if (mode == 2)
{
*num = *num + 1;
Make_PNG(bitmap, "base", i, 1);
sprintf((*entries)[i].base_img, "base_%d.png", i);
(*entries)[i].base_value = (double)(rand() % 1000);
} else if (mode == 3){
Make_PNG(bitmap, "test", i, 1);
sprintf((*entries)[i].test_img, "test_%d.png", i);
(*entries)[i].test_value = (double)(rand() % 1000);
(*entries)[i].difference = fabs((*entries)[i].base_value- (*entries)[i].test_value);
}
}
}
error = ft_doneface_fun(face);
if (error)
{
printf("Error: freeing face");
}
error = ft_donefreetype_fun(library);
if (error)
{
printf("Error: freeing library");
}
dlclose(handle);
}

View File

@ -1,9 +0,0 @@
Compile with old:
gcc generate-old-hashes.c bitmap.c murmur3.c -I <old ft dir>/include -L <old ft dir>/objs -lfreetype -o generate-old-hashes
Compile with new:
gcc compare-new-to-old.c bitmap.c murmur3.c -I <new ft dir>/include -L <new ft dir>/objs -lfreetype -o compare-new-to-old
For consolidated gen.c:
gcc gen.c bitmap.c murmur3.c -I/usr/local/include/freetype2/ -lfreetype -o gen
./genc <base ft.so dir> <test ft.so dir> <font dir> <char size>