From 4f5f083dc68dc6f0896e8da529590e88101c3c19 Mon Sep 17 00:00:00 2001 From: Kushal K S V S Date: Fri, 7 Jul 2017 02:23:45 +0530 Subject: Put it all in tests folder --- make_bitmap/README | 18 --- make_bitmap/bitmap.c | 269 ------------------------------------ make_bitmap/bitmap.h | 85 ------------ make_bitmap/example1.c | 98 ------------- make_bitmap/example2.c | 93 ------------- make_bitmap/murmur3.c | 312 ------------------------------------------ make_bitmap/murmur3.h | 24 ---- make_bitmap/runme.sh | 4 - make_png/README | 37 ----- make_png/bitmap.c | 282 -------------------------------------- make_png/bitmap.h | 67 --------- make_png/hash_to_file.c | 151 -------------------- make_png/make_png.c | 178 ------------------------ make_png/makefile | 28 ---- make_png/murmur3.c | 312 ------------------------------------------ make_png/murmur3.h | 24 ---- tests/make_bitmap/README | 18 +++ tests/make_bitmap/bitmap.c | 269 ++++++++++++++++++++++++++++++++++++ tests/make_bitmap/bitmap.h | 85 ++++++++++++ tests/make_bitmap/example1.c | 98 +++++++++++++ tests/make_bitmap/example2.c | 93 +++++++++++++ tests/make_bitmap/murmur3.c | 312 ++++++++++++++++++++++++++++++++++++++++++ tests/make_bitmap/murmur3.h | 24 ++++ tests/make_bitmap/runme.sh | 4 + tests/make_png/README | 37 +++++ tests/make_png/bitmap.c | 282 ++++++++++++++++++++++++++++++++++++++ tests/make_png/bitmap.h | 67 +++++++++ tests/make_png/hash_to_file.c | 151 ++++++++++++++++++++ tests/make_png/make_png.c | 178 ++++++++++++++++++++++++ tests/make_png/makefile | 28 ++++ tests/make_png/murmur3.c | 312 ++++++++++++++++++++++++++++++++++++++++++ tests/make_png/murmur3.h | 24 ++++ 32 files changed, 1982 insertions(+), 1982 deletions(-) delete mode 100644 make_bitmap/README delete mode 100644 make_bitmap/bitmap.c delete mode 100644 make_bitmap/bitmap.h delete mode 100644 make_bitmap/example1.c delete mode 100644 make_bitmap/example2.c delete mode 100644 make_bitmap/murmur3.c delete mode 100644 make_bitmap/murmur3.h delete mode 100755 make_bitmap/runme.sh delete mode 100644 make_png/README delete mode 100644 make_png/bitmap.c delete mode 100644 make_png/bitmap.h delete mode 100644 make_png/hash_to_file.c delete mode 100644 make_png/make_png.c delete mode 100644 make_png/makefile delete mode 100644 make_png/murmur3.c delete mode 100644 make_png/murmur3.h create mode 100644 tests/make_bitmap/README create mode 100644 tests/make_bitmap/bitmap.c create mode 100644 tests/make_bitmap/bitmap.h create mode 100644 tests/make_bitmap/example1.c create mode 100644 tests/make_bitmap/example2.c create mode 100644 tests/make_bitmap/murmur3.c create mode 100644 tests/make_bitmap/murmur3.h create mode 100755 tests/make_bitmap/runme.sh create mode 100644 tests/make_png/README create mode 100644 tests/make_png/bitmap.c create mode 100644 tests/make_png/bitmap.h create mode 100644 tests/make_png/hash_to_file.c create mode 100644 tests/make_png/make_png.c create mode 100644 tests/make_png/makefile create mode 100644 tests/make_png/murmur3.c create mode 100644 tests/make_png/murmur3.h diff --git a/make_bitmap/README b/make_bitmap/README deleted file mode 100644 index 98208b50a..000000000 --- a/make_bitmap/README +++ /dev/null @@ -1,18 +0,0 @@ -Use the "new" folder. - - -To compile and execute - -gcc example1.c ksvsk.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example1 -./example1 - -gcc example2.c ksvsk.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example2 -./example2 - - -NOTE - --> DONOT use Get_Padding render MONO / GRAYSCALE bitmaps. - Use FT_Bitmap_Convert to make the width of bitmap a multiple of 4 bytes and then write it to file. - - diff --git a/make_bitmap/bitmap.c b/make_bitmap/bitmap.c deleted file mode 100644 index 4217c3c16..000000000 --- a/make_bitmap/bitmap.c +++ /dev/null @@ -1,269 +0,0 @@ -#include "bitmap.h" - -HASH_128 * Generate_Hash_x64_128(FT_Bitmap * bitmap, HASH_128 * murmur) -{ - int seed = 99; // Dont change - - MurmurHash3_x64_128(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash); - - return murmur; -} - -HASH_128 * Generate_Hash_x86_128(FT_Bitmap * bitmap, HASH_128 * murmur) -{ - int seed = 99; // Dont change - - MurmurHash3_x86_128(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash); - - return murmur; -} - -HASH_32 * Generate_Hash_x86_32(FT_Bitmap * bitmap, HASH_32 * murmur) -{ - int seed = 99; // Dont change - - MurmurHash3_x86_32(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash); - - 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); -} - -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 ) { - - FILE *fp = fopen("test.bmp","w"); // Bitmap File - HEADER *header = ( HEADER* ) calloc( 1 , sizeof( HEADER )); - - unsigned char pixel_mode = ( bitmap->pixel_mode ); - FT_UInt image_width; - FT_UInt image_rows; - FT_UInt color_table_size = 0; - - switch(pixel_mode){ - - case 5 : image_width = bitmap->width / 3; // LCD - image_rows = bitmap->rows; - break; - - case 6 : image_width = bitmap->width; // LCD_V - image_rows = bitmap->rows / 3; - break; - - default : image_width = bitmap->width; // MONO and GRAY - image_rows = bitmap->rows; - color_table_size = 4* 256; - break; - } - - FT_Int image_size; - image_size = (image_rows * image_width ); - - 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; - - 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; - - fwrite (header, 1, sizeof(HEADER),fp); - free(header); - fclose(fp); -} - -void Write_Bitmap_Data_LCD_RGB( FT_Bitmap * bitmap ){ - - char value; - int i,j,k; - - FILE *fp = fopen("test.bmp","a"); - - 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); - } - } - - fclose(fp); -} - -void Write_Bitmap_Data_LCD_BGR( FT_Bitmap * bitmap ){ - - char value; - int i,j,k; - - FILE *fp = fopen("test.bmp","a"); - - 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); - } - } - - fclose(fp); -} - -void Write_Bitmap_Data_LCD_V_BGR (FT_Bitmap * bitmap) { - - FILE *fp = fopen("test.bmp","a"); - - int i,j,k,step; - char value; - step = 0; - - while ( step < bitmap->rows ){ - - 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 - } - - fclose(fp); -} - -void Write_Bitmap_Data_LCD_V_RGB (FT_Bitmap * bitmap) { - - FILE *fp = fopen("test.bmp","a"); - - int i,j,k,step; - char value; - step = 0; - - while ( step < bitmap->rows ){ - - 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 - } - - fclose(fp); -} - -void Write_Bitmap_Data_MONO (FT_Bitmap * bitmap) { - - FILE *fp = fopen("test.bmp","a"); - - 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 (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); - } - } - - fclose(fp); -} - -void Write_Bitmap_Data_GRAY(FT_Bitmap * bitmap) { - - FILE *fp = fopen("test.bmp","a"); - 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 (int i = 0; i < bitmap->pitch * bitmap->rows; ++i) - { - value = 255 - bitmap->buffer[i]; - fwrite(&value,1,1,fp); - } - - fclose(fp); -} \ No newline at end of file diff --git a/make_bitmap/bitmap.h b/make_bitmap/bitmap.h deleted file mode 100644 index f2e309bd5..000000000 --- a/make_bitmap/bitmap.h +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#include "murmur3.h" // MurmurHash3_x64_128 header file - -#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 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]; -}HASH_32; - -typedef struct { // To store 128bit Hash - FT_UInt32 hash[4]; -}HASH_128; - -#pragma pack(pop) - -//------------------------------------------------------------------------------- - -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); - -int Get_Bits_Per_Pixel ( unsigned char PIXEL_MODE ); - -void Write_Bitmap_Header (FT_Bitmap * bitmap ); - -void Write_Bitmap_Data_MONO (FT_Bitmap * bitmap); -void Write_Bitmap_Data_GRAY (FT_Bitmap * bitmap); -void Write_Bitmap_Data_LCD_BGR (FT_Bitmap * bitmap); -void Write_Bitmap_Data_LCD_RGB (FT_Bitmap * bitmap); -void Write_Bitmap_Data_LCD_V_RGB (FT_Bitmap * bitmap); -void Write_Bitmap_Data_LCD_V_BGR (FT_Bitmap * bitmap); diff --git a/make_bitmap/example1.c b/make_bitmap/example1.c deleted file mode 100644 index 9bc61216d..000000000 --- a/make_bitmap/example1.c +++ /dev/null @@ -1,98 +0,0 @@ -// This example uses the FreeType rendering library to produce -// LCD rendered bitmap (RGB) for the glyphs of a font -// Also gives the MurmurHash3 hash value of the bitmap rendered. - -#include "bitmap.h" - -int main(int argc, char const *argv[]) -{ - FT_Library library; - FT_Face face; - FT_GlyphSlot slot; - FT_Bitmap* bitmap; - FT_Error error; - - const char* font_file; // Arguments - FT_UInt32 size; - FT_UInt glyph_index; - const char* character; - - if (argc != 4) - { - printf("\nTo render a RGB bitmap of a glyph to be displayed on an \n"); - printf("LCD surface and to generate the 128bit MurmurHash3 hash \n\n"); - - printf("Filter used is FT_LCD_FILTER_DEFAULT\n\n"); - - printf("Usage ./ \n\n"); - - exit(0); - } - - font_file = argv[1]; - size = atoi(argv[2]); - character = argv[3]; - - error = FT_Init_FreeType( &library ); - if(error){ - printf("Error while initialising library\n"); - } - - error = FT_Library_SetLcdFilter( library, - FT_LCD_FILTER_DEFAULT ); - if(error){ - printf("Error while setting LCD filter\n"); - } - - error = FT_New_Face( library, - font_file, - 0, - &face ); - if(error){ - printf("Error loading new face\n"); - } - - error = FT_Set_Char_Size( face, - size * 64, - 0, - 100, - 0 ); - if(error){ - printf("Error setting character size\n"); - } - - glyph_index = FT_Get_Char_Index( face, *character ); - - slot = face->glyph; - - error = FT_Load_Glyph( face, - glyph_index, - FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_LCD ); - if(error){ - printf("Error loading glyph\n"); - } - - FT_Render_Glyph( slot, - FT_RENDER_MODE_LCD); - if(error){ - printf("Error rendering the glyph\n"); - } - - bitmap = &slot->bitmap; - - Write_Bitmap_Header(bitmap); - Write_Bitmap_Data_LCD_RGB(bitmap); - - HASH_128 * murmur = (HASH_128 *) malloc(sizeof(HASH_128)) ; - murmur = Generate_Hash_x64_128(bitmap,murmur); // using the x64_128 function of the hash - - printf("%08x %08x %08x %08x\n", murmur->hash[0], // Print hash - murmur->hash[1], - murmur->hash[2], - murmur->hash[3]); - - FT_Done_Face ( face ); - FT_Done_FreeType( library ); - - return 0; -} \ No newline at end of file diff --git a/make_bitmap/example2.c b/make_bitmap/example2.c deleted file mode 100644 index 4fa791075..000000000 --- a/make_bitmap/example2.c +++ /dev/null @@ -1,93 +0,0 @@ -// This example uses the FreeType rendering library to print -// the MurmurHash3 hash value of the 8-bit anti-aliased -// bitmaps of all the glyphs in the font. - -#include "bitmap.h" // The header file - -int main(int argc, char const *argv[]) -{ - FT_Library library; - FT_Face face; - FT_GlyphSlot slot; - FT_Bitmap* bitmap; - - FT_Error error; - - const char* filename; - FT_UInt32 size; - - if (argc != 3) - { - printf("This program prints the hashes of 8-bit grayscale\n"); - printf("anti-aliased bitmaps of all the glyphs in a font\n\n"); - - printf("Usage ./ \n\n"); - exit(0); - } - - - filename = argv[1]; - size = atoi(argv[2]); - - error = FT_Init_FreeType( &library ); - if(error){ - printf("Error while initialising library\n"); - } - - error = FT_New_Face( library, - filename, - 0, - &face ); - if(error){ - printf("Error loading new face\n"); - } - - error = FT_Set_Char_Size( face, - size * 64, - 0, - 100, - 0 ); - if(error){ - printf("Error setting character size\n"); - } - - slot = face->glyph; - - for (int i = 0; i < face->num_glyphs; ++i) // Loop - { - error = FT_Load_Glyph( face, - i, - FT_LOAD_DEFAULT); - if(error){ - printf("Error loading glyph\n"); - } - - FT_Render_Glyph( slot, - FT_RENDER_MODE_NORMAL); - if(error){ - printf("Error rendering the glyph\n"); - } - - bitmap = &slot->bitmap; - - if (bitmap->width == 0|| bitmap->rows == 0) - { - printf("%d - Empty Glyph\n",i); - continue; - } - - HASH_128 * murmur = (HASH_128 *) malloc(sizeof(HASH_128)) ; - murmur = Generate_Hash_x64_128(bitmap,murmur); - - printf("%d - %08x %08x %08x %08x\n",i, - murmur->hash[0], - murmur->hash[1], - murmur->hash[2], - murmur->hash[3]); - } - - FT_Done_Face ( face ); - FT_Done_FreeType( library ); - - return 0; -} \ No newline at end of file diff --git a/make_bitmap/murmur3.c b/make_bitmap/murmur3.c deleted file mode 100644 index 484ebfd03..000000000 --- a/make_bitmap/murmur3.c +++ /dev/null @@ -1,312 +0,0 @@ -//----------------------------------------------------------------------------- -// Note - The x86 and x64 versions do _not_ produce the same results, as the -// algorithms are optimized for their respective platforms. You can still -// compile and run any of them on any platform, but your performance with the -// non-native version will be less than optimal. - -#include "murmur3.h" - -//----------------------------------------------------------------------------- -// Platform-specific functions and macros - -#ifdef __GNUC__ -#define FORCE_INLINE __attribute__((always_inline)) inline -#else -#define FORCE_INLINE inline -#endif - -static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r ) -{ - return (x << r) | (x >> (32 - r)); -} - -static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r ) -{ - return (x << r) | (x >> (64 - r)); -} - -#define ROTL32(x,y) rotl32(x,y) -#define ROTL64(x,y) rotl64(x,y) - -#define BIG_CONSTANT(x) (x##LLU) - -//----------------------------------------------------------------------------- -// Block read - if your platform needs to do endian-swapping or can only -// handle aligned reads, do the conversion here - -#define getblock(p, i) (p[i]) - -//----------------------------------------------------------------------------- -// Finalization mix - force all bits of a hash block to avalanche - -static FORCE_INLINE uint32_t fmix32 ( uint32_t h ) -{ - h ^= h >> 16; - h *= 0x85ebca6b; - h ^= h >> 13; - h *= 0xc2b2ae35; - h ^= h >> 16; - - return h; -} - -//---------- - -static FORCE_INLINE uint64_t fmix64 ( uint64_t k ) -{ - k ^= k >> 33; - k *= BIG_CONSTANT(0xff51afd7ed558ccd); - k ^= k >> 33; - k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); - k ^= k >> 33; - - return k; -} - -//----------------------------------------------------------------------------- - -void MurmurHash3_x86_32 ( const void * key, int len, - uint32_t seed, void * out ) -{ - const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 4; - int i; - - uint32_t h1 = seed; - - uint32_t c1 = 0xcc9e2d51; - uint32_t c2 = 0x1b873593; - - //---------- - // body - - const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); - - for(i = -nblocks; i; i++) - { - uint32_t k1 = getblock(blocks,i); - - k1 *= c1; - k1 = ROTL32(k1,15); - k1 *= c2; - - h1 ^= k1; - h1 = ROTL32(h1,13); - h1 = h1*5+0xe6546b64; - } - - //---------- - // tail - - const uint8_t * tail = (const uint8_t*)(data + nblocks*4); - - uint32_t k1 = 0; - - switch(len & 3) - { - case 3: k1 ^= tail[2] << 16; - case 2: k1 ^= tail[1] << 8; - case 1: k1 ^= tail[0]; - k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; - }; - - //---------- - // finalization - - h1 ^= len; - - h1 = fmix32(h1); - - *(uint32_t*)out = h1; -} - -//----------------------------------------------------------------------------- - -void MurmurHash3_x86_128 ( const void * key, const int len, - uint32_t seed, void * out ) -{ - const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 16; - int i; - - uint32_t h1 = seed; - uint32_t h2 = seed; - uint32_t h3 = seed; - uint32_t h4 = seed; - - uint32_t c1 = 0x239b961b; - uint32_t c2 = 0xab0e9789; - uint32_t c3 = 0x38b34ae5; - uint32_t c4 = 0xa1e38b93; - - //---------- - // body - - const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); - - for(i = -nblocks; i; i++) - { - uint32_t k1 = getblock(blocks,i*4+0); - uint32_t k2 = getblock(blocks,i*4+1); - uint32_t k3 = getblock(blocks,i*4+2); - uint32_t k4 = getblock(blocks,i*4+3); - - k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; - - h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; - - k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; - - h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; - - k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; - - h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; - - k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; - - h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; - } - - //---------- - // tail - - const uint8_t * tail = (const uint8_t*)(data + nblocks*16); - - uint32_t k1 = 0; - uint32_t k2 = 0; - uint32_t k3 = 0; - uint32_t k4 = 0; - - switch(len & 15) - { - case 15: k4 ^= tail[14] << 16; - case 14: k4 ^= tail[13] << 8; - case 13: k4 ^= tail[12] << 0; - k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; - - case 12: k3 ^= tail[11] << 24; - case 11: k3 ^= tail[10] << 16; - case 10: k3 ^= tail[ 9] << 8; - case 9: k3 ^= tail[ 8] << 0; - k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; - - case 8: k2 ^= tail[ 7] << 24; - case 7: k2 ^= tail[ 6] << 16; - case 6: k2 ^= tail[ 5] << 8; - case 5: k2 ^= tail[ 4] << 0; - k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; - - case 4: k1 ^= tail[ 3] << 24; - case 3: k1 ^= tail[ 2] << 16; - case 2: k1 ^= tail[ 1] << 8; - case 1: k1 ^= tail[ 0] << 0; - k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; - }; - - //---------- - // finalization - - h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; - - h1 += h2; h1 += h3; h1 += h4; - h2 += h1; h3 += h1; h4 += h1; - - h1 = fmix32(h1); - h2 = fmix32(h2); - h3 = fmix32(h3); - h4 = fmix32(h4); - - h1 += h2; h1 += h3; h1 += h4; - h2 += h1; h3 += h1; h4 += h1; - - ((uint32_t*)out)[0] = h1; - ((uint32_t*)out)[1] = h2; - ((uint32_t*)out)[2] = h3; - ((uint32_t*)out)[3] = h4; -} - -//----------------------------------------------------------------------------- - -void MurmurHash3_x64_128 ( const void * key, const int len, - const uint32_t seed, void * out ) -{ - const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 16; - int i; - - uint64_t h1 = seed; - uint64_t h2 = seed; - - uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); - uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); - - //---------- - // body - - const uint64_t * blocks = (const uint64_t *)(data); - - for(i = 0; i < nblocks; i++) - { - uint64_t k1 = getblock(blocks,i*2+0); - uint64_t k2 = getblock(blocks,i*2+1); - - k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; - - h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; - - k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; - - h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; - } - - //---------- - // tail - - const uint8_t * tail = (const uint8_t*)(data + nblocks*16); - - uint64_t k1 = 0; - uint64_t k2 = 0; - - switch(len & 15) - { - case 15: k2 ^= (uint64_t)(tail[14]) << 48; - case 14: k2 ^= (uint64_t)(tail[13]) << 40; - case 13: k2 ^= (uint64_t)(tail[12]) << 32; - case 12: k2 ^= (uint64_t)(tail[11]) << 24; - case 11: k2 ^= (uint64_t)(tail[10]) << 16; - case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; - case 9: k2 ^= (uint64_t)(tail[ 8]) << 0; - k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; - - case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; - case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; - case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; - case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; - case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; - case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; - case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; - case 1: k1 ^= (uint64_t)(tail[ 0]) << 0; - k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; - }; - - //---------- - // finalization - - h1 ^= len; h2 ^= len; - - h1 += h2; - h2 += h1; - - h1 = fmix64(h1); - h2 = fmix64(h2); - - h1 += h2; - h2 += h1; - - ((uint64_t*)out)[0] = h1; - ((uint64_t*)out)[1] = h2; -} - -//----------------------------------------------------------------------------- - diff --git a/make_bitmap/murmur3.h b/make_bitmap/murmur3.h deleted file mode 100644 index 90878b00d..000000000 --- a/make_bitmap/murmur3.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _MURMURHASH3_H_ -#define _MURMURHASH3_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -//----------------------------------------------------------------------------- - -void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out); - -void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out); - -void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out); - -//----------------------------------------------------------------------------- - -#ifdef __cplusplus -} -#endif - -#endif // _MURMURHASH3_H_ diff --git a/make_bitmap/runme.sh b/make_bitmap/runme.sh deleted file mode 100755 index 4243ea5d7..000000000 --- a/make_bitmap/runme.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -gcc example1.c bitmap.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example1 -gcc example2.c bitmap.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example2 - diff --git a/make_png/README b/make_png/README deleted file mode 100644 index 677cf65dc..000000000 --- a/make_png/README +++ /dev/null @@ -1,37 +0,0 @@ -To generate hashes and store it in the ./hashes folder, - -1) sudo make hash - (set resoluton in DPI by passing argument - example: sudo make DPI=100, if not specified,default is 72) - -2) Usage ./hash - - Hashes will be saved in a file named - $(font)_$(pt_size)_$(render_mode).hash - - By default, hashes of 256-level gray bitmaps will be generated - - Values for render_mode 0 - monochrome - 1 - anti-aliased - 2 - lcd horizontal - 3 - lcd vertical - -To generate 32-bit RGBA PNG(s) of all glyphs in a font\n - -1) sudo make png - (set resoluton in DPI by passing argument - example: sudo make DPI=100, if not specified,default is 72) - -2.Usage ./ - - Images will be saved in a file named - $(font)_$(pt_size)_$(render_mode)_$(glyph_index).png - - By default, hashes of 256-level gray bitmaps will be generated - - Values for render_mode 0 - monochrome - 1 - anti-aliased - 2 - lcd horizontal-RGB - 3 - lcd horizontal-BGR - 4 - lcd vertical-RGB - 5 - lcd vertical-BGR \ No newline at end of file diff --git a/make_png/bitmap.c b/make_png/bitmap.c deleted file mode 100644 index 104acdc1c..000000000 --- a/make_png/bitmap.c +++ /dev/null @@ -1,282 +0,0 @@ -#include "bitmap.h" - -HASH_128 * Generate_Hash_x64_128( FT_Bitmap * bitmap, - HASH_128 * murmur) -{ - int seed = 99; // Dont change - - MurmurHash3_x64_128(bitmap->buffer, - (bitmap->pitch * bitmap->rows), - seed, - murmur->hash); - - return murmur; -} - -HASH_128 * Generate_Hash_x86_128( FT_Bitmap * bitmap, - HASH_128 * murmur) -{ - int seed = 99; // Dont change - - MurmurHash3_x86_128(bitmap->buffer, - (bitmap->pitch * bitmap->rows), - seed, - murmur->hash); - - return murmur; -} - -HASH_32 * Generate_Hash_x86_32( FT_Bitmap * bitmap, - HASH_32 * murmur) -{ - int seed = 99; // Dont change - - MurmurHash3_x86_32( bitmap->buffer, - (bitmap->pitch * bitmap->rows), - seed, - murmur->hash); - - return murmur; -} - -PIXEL * Pixel_At (IMAGE * bitmap, int x, int y) -{ - return bitmap->pixels + bitmap->width * y + x; -} - -int Generate_PNG (IMAGE *bitmap, - const char *path, - int render_mode) -{ - FILE * fp; - png_structp png_ptr = NULL; - png_infop info_ptr = NULL; - - size_t x, y; - png_byte ** row_pointers = NULL; - - int status = -1; - - int pixel_size = 4; - int depth = 8; - - fp = fopen (path, "wb"); - if (! fp) { - goto fopen_failed; - } - - 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; - } - - 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; - } - } - - png_init_io ( png_ptr, - fp); - - png_set_rows (png_ptr, - info_ptr, - row_pointers); - - png_write_png ( png_ptr, - info_ptr, - PNG_TRANSFORM_IDENTITY, - NULL); - - 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 Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){ - - IMAGE fruit; - int x; - int y; - - unsigned char value; - int p; - - switch(render_mode){ - - case 0 : fruit.width = bitmap->width; // MONO and GRAY - fruit.height = bitmap->rows; - - fruit.pixels = calloc ( fruit.width * fruit.height, - sizeof (PIXEL)); - - for (y = 0; y < fruit.height; y++) { - for (x = 0; x < fruit.width; x++) { - - PIXEL * pixel = Pixel_At (& fruit, x, y); - p = (y * bitmap->pitch ) + x; - - value = bitmap->buffer[p]; - - if ( value != 0x00 ){ - value = 0xff; - }else{ - value = 0x00; - } - - 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; - - fruit.pixels = calloc ( fruit.width * fruit.height, - sizeof (PIXEL)); - - for (y = 0; y < fruit.height; y++) { - for (x = 0; x < fruit.width; x++) { - - PIXEL * pixel = Pixel_At (& fruit, x, y); - p = (y * bitmap->pitch ) + x; - - value = bitmap->buffer[p]; - - pixel->red = 255- value; - pixel->green = 255- value; - pixel->blue = 255- value; - pixel->alpha = 255; - } - } - break; - - case 2 : - case 3 : fruit.width = bitmap->width / 3; // LCD - fruit.height = bitmap->rows; - - fruit.pixels = calloc ( fruit.width * fruit.height, - sizeof (PIXEL)); - - for (y = 0; y < fruit.height; y++) { - for (x = 0; x < fruit.width; x++) { - - PIXEL * pixel = Pixel_At (& fruit, x, y); - p = (y * bitmap->pitch ) + (x)*3; - - value = bitmap->buffer[p]; - pixel->red = 255- value; - p++; - - value = bitmap->buffer[p]; - pixel->green = 255- value; - p++; - - value = bitmap->buffer[p]; - pixel->blue = 255- value; - - pixel->alpha = 255; - } - } - break; - - case 4 : - case 5 : fruit.width = bitmap->width; // LCD_V - fruit.height = bitmap->rows / 3; - - fruit.pixels = calloc ( fruit.width * fruit.height, - sizeof (PIXEL)); - - 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; - - value = bitmap->buffer[p]; - pixel->red = 255- value; - p += bitmap->pitch; - - value = bitmap->buffer[p]; - pixel->green = 255- value; - p += bitmap->pitch; - - value = bitmap->buffer[p]; - pixel->blue = 255- value; - - pixel->alpha = 255; - } - } - break; - - default : fruit.width = bitmap->width; - fruit.height = bitmap->rows; - break; - } - - char * file_name = ( char * )calloc(150,sizeof(char)); - - sprintf(file_name, "%s_%d", name, i); - - Generate_PNG (& fruit, file_name, render_mode); - - free (fruit.pixels); -} \ No newline at end of file diff --git a/make_png/bitmap.h b/make_png/bitmap.h deleted file mode 100644 index 426d62218..000000000 --- a/make_png/bitmap.h +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -#include - -#include "murmur3.h" // MurmurHash3_x64_128 header file - -#include - - -#include FT_FREETYPE_H -#include FT_MODULE_H -#include FT_LCD_FILTER_H -#include FT_BITMAP_H - -#define BITS_PER_PIXEL_RGBA 32 - -typedef struct { // To store 32bit Hash - FT_UInt32 hash[1]; -}HASH_32; - -typedef struct { // To store 128bit Hash - FT_UInt32 hash[4]; -}HASH_128; - -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); - -//------------------------------------------------------------------------------ - -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); - diff --git a/make_png/hash_to_file.c b/make_png/hash_to_file.c deleted file mode 100644 index e3446594b..000000000 --- a/make_png/hash_to_file.c +++ /dev/null @@ -1,151 +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_Error error; - - const char* font_file; - int size; - int render_mode; // argument - - int load_flag; // FT_LOAD_XXX - int render_flag; // FT_RENDER_MODE_XXX - int target_flag; // FT_LOAD_TARGET_XXX - char* render_type; // for file_name - - char name[100]; // hashes file name - int i; // for loop - - if (argc != 4) - { - printf("\nTo generate MurmurHash3 hash values of all glyphs\n"); - printf("Hashes will be saved in a file named\n \ - $(font)_$(pt_size)_$(render_mode).hash \n\n"); - - printf("Default: hashes of 256-level gray bitmaps will be generated\n\n"); - - printf("Usage ./ \n\n"); - - printf("Values for render_mode 0 - monochrome\n"); - printf(" 1 - anti-aliased\n"); - printf(" 2 - lcd horizontal-RGB\n"); - printf(" 3 - lcd vertical-RGB\n"); - - return 1; - } - - font_file = argv[1]; - size = atof(argv[2]); - render_mode = atoi(argv[3]); - - error = FT_Init_FreeType( &library ); - if(error){ - printf("Error while initialising library\n"); - } - - if (render_mode > 1 ) - { - error = FT_Library_SetLcdFilter( library, - FT_LCD_FILTER_DEFAULT ); - if(error){ - printf("Error while setting LCD filter\n"); - } - } - - error = FT_New_Face( library, - font_file, - 0, - &face ); - if(error){ - printf("Error loading new face\n"); - } - - error = FT_Set_Char_Size( face, - size * 64, - 0, - DPI, - 0 ); - if(error){ - printf("Error setting character size\n"); - } - - switch ( render_mode ) { - case 0: render_flag = FT_RENDER_MODE_MONO; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_MONO; - render_type = "mono"; - break; - - case 1: render_flag = FT_RENDER_MODE_NORMAL; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_NORMAL; - render_type = "gray"; - break; - - case 2: render_flag = FT_RENDER_MODE_LCD; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_LCD; - render_type = "lcd_rgb"; - break; - - case 3: render_flag = FT_RENDER_MODE_LCD_V; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_LCD_V; - render_type = "lcd_ver_rgb"; - break; - - default:render_flag = FT_RENDER_MODE_NORMAL; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_NORMAL; - render_type = "gray"; - } - - slot = face->glyph; - - FILE* fp; - - sprintf(name,"./hashes/%s_%d_%s.hash",font_file, - size, - render_type); - - fp = fopen(name,"w"); - - for (i = 0; i < face->num_glyphs; ++i) - { - error = FT_Load_Glyph( face, - i, - load_flag | target_flag); - if(error){ - printf("Error loading glyph\n"); - } - - FT_Render_Glyph( slot, - render_flag); - if(error){ - printf("Error rendering the 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], // Print hash - murmur->hash[1], - murmur->hash[2], - murmur->hash[3]); - } - fclose(fp); - - FT_Done_Face ( face ); - FT_Done_FreeType( library ); - - return 0; - -} diff --git a/make_png/make_png.c b/make_png/make_png.c deleted file mode 100644 index 8b59d4455..000000000 --- a/make_png/make_png.c +++ /dev/null @@ -1,178 +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_Error error; - - const char* font_file; - int size; - int render_mode; // argument - - int load_flag; // FT_LOAD_XXX - int render_flag; // FT_RENDER_MODE_XXX - int target_flag; // FT_LOAD_TARGET_XXX - char* render_type; // for file_name - - char name[100]; // hashes file name - int i; // for loop - - if (argc != 4) - { - printf("\nTo generate 32-bit RGBA PNG(s) of all glyphs in a font\n"); - printf("Images will be saved in a file named \n\ - $(font)_$(pt_size)_$(render_mode)_$(glyph_index).png \n\n"); - - printf("Default: hashes of 256-level gray bitmaps will be generated\n\n"); - - printf("Usage ./ \n\n"); - - printf("Values for render_mode 0 - monochrome\n"); - printf(" 1 - anti-aliased\n"); - printf(" 2 - lcd horizontal-RGB\n"); - printf(" 3 - lcd horizontal-BGR\n"); - printf(" 4 - lcd vertical-RGB\n"); - printf(" 5 - lcd vertical-BGR\n"); - - return 1; - } - - font_file = argv[1]; - size = atof(argv[2]); - render_mode = atoi(argv[3]); - - error = FT_Init_FreeType( &library ); - if(error){ - printf("Error while initialising library\n"); - } - - if (render_mode > 1 ) - { - error = FT_Library_SetLcdFilter( library, - FT_LCD_FILTER_DEFAULT ); - if(error){ - printf("Error while setting LCD filter\n"); - } - } - - error = FT_New_Face( library, - font_file, - 0, - &face ); - if(error){ - printf("Error loading new face\n"); - } - - error = FT_Set_Char_Size( face, - size * 64, - 0, - DPI, - 0 ); - if(error){ - printf("Error setting character size\n"); - } - - switch ( render_mode ) { - case 0: render_flag = FT_RENDER_MODE_MONO; - load_flag = FT_LOAD_MONOCHROME; - target_flag = FT_LOAD_TARGET_MONO; - render_type = "mono"; - break; - - case 1: render_flag = FT_RENDER_MODE_NORMAL; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_NORMAL; - render_type = "gray"; - break; - - case 2: render_flag = FT_RENDER_MODE_LCD; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_LCD; - render_type = "lcd_rgb"; - break; - - case 3: render_flag = FT_RENDER_MODE_LCD; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_LCD; - render_type = "lcd_bgr"; - break; - - case 4: render_flag = FT_RENDER_MODE_LCD_V; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_LCD_V; - render_type = "lcd_ver_rgb"; - break; - - case 5: render_flag = FT_RENDER_MODE_LCD_V; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_LCD_V; - render_type = "lcd_ver_bgr"; - break; - - default:render_flag = FT_RENDER_MODE_NORMAL; - load_flag = FT_LOAD_DEFAULT; - target_flag = FT_LOAD_TARGET_NORMAL; - render_type = "gray"; - } - - slot = face->glyph; - - sprintf(name,"./images/%s_%d_%s", font_file, - size, - render_type); - - for (i = 0; i num_glyphs; ++i) - { - error = FT_Load_Glyph( face, - i, - load_flag | target_flag); - if(error){ - printf("Error loading glyph\n"); - } - - FT_Render_Glyph( slot, - render_flag); - if(error){ - printf("Error rendering the glyph\n"); - } - - bitmap = &slot->bitmap; - - if (bitmap->width == 0 || bitmap->rows == 0) - { - continue; - } - - FT_Bitmap target; - FT_Bitmap_Init( &target ); - - int alignment = 4; - error = FT_Bitmap_Convert( library, - bitmap, - &target, - alignment); - if(error){ - printf("Error converting the bitmap\n"); - } - - if (render_mode == 0) - { - Make_PNG(&target,name,i,render_mode); - - }else{ - - Make_PNG(bitmap,name,i,render_mode); - } - } - - FT_Done_Face ( face ); - FT_Done_FreeType( library ); - - return 0; - -} diff --git a/make_png/makefile b/make_png/makefile deleted file mode 100644 index 691a6fd1b..000000000 --- a/make_png/makefile +++ /dev/null @@ -1,28 +0,0 @@ -# Define required macros here - -SHELL = /bin/sh - -SRC_HASH = hash_to_file.c bitmap.c murmur3.c -SRC_PNG = make_png.c bitmap.c murmur3.c -OBJS = $(src:.c=.o) - -CFLAGS = -Wall -g -CC = gcc -INCLUDE = -I /usr/local/include/freetype2/ -LIBS = -lfreetype -lpng - -DPI = 72 - -all: png hash - -png:$(SRC_PNG) - $(CC) $(CFLAGS) $(INCLUDE) -DDPI=$(DPI) -o $@ $(SRC_PNG) $(OBJS) $(LIBS) - -hash:$(SRC_HASH) - $(CC) $(CFLAGS) $(INCLUDE) -DDPI=$(DPI) -o $@ $(SRC_HASH) $(OBJS) $(LIBS) - -.PHONY: clean -clean: - -rm -f *.o - - diff --git a/make_png/murmur3.c b/make_png/murmur3.c deleted file mode 100644 index 484ebfd03..000000000 --- a/make_png/murmur3.c +++ /dev/null @@ -1,312 +0,0 @@ -//----------------------------------------------------------------------------- -// Note - The x86 and x64 versions do _not_ produce the same results, as the -// algorithms are optimized for their respective platforms. You can still -// compile and run any of them on any platform, but your performance with the -// non-native version will be less than optimal. - -#include "murmur3.h" - -//----------------------------------------------------------------------------- -// Platform-specific functions and macros - -#ifdef __GNUC__ -#define FORCE_INLINE __attribute__((always_inline)) inline -#else -#define FORCE_INLINE inline -#endif - -static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r ) -{ - return (x << r) | (x >> (32 - r)); -} - -static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r ) -{ - return (x << r) | (x >> (64 - r)); -} - -#define ROTL32(x,y) rotl32(x,y) -#define ROTL64(x,y) rotl64(x,y) - -#define BIG_CONSTANT(x) (x##LLU) - -//----------------------------------------------------------------------------- -// Block read - if your platform needs to do endian-swapping or can only -// handle aligned reads, do the conversion here - -#define getblock(p, i) (p[i]) - -//----------------------------------------------------------------------------- -// Finalization mix - force all bits of a hash block to avalanche - -static FORCE_INLINE uint32_t fmix32 ( uint32_t h ) -{ - h ^= h >> 16; - h *= 0x85ebca6b; - h ^= h >> 13; - h *= 0xc2b2ae35; - h ^= h >> 16; - - return h; -} - -//---------- - -static FORCE_INLINE uint64_t fmix64 ( uint64_t k ) -{ - k ^= k >> 33; - k *= BIG_CONSTANT(0xff51afd7ed558ccd); - k ^= k >> 33; - k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); - k ^= k >> 33; - - return k; -} - -//----------------------------------------------------------------------------- - -void MurmurHash3_x86_32 ( const void * key, int len, - uint32_t seed, void * out ) -{ - const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 4; - int i; - - uint32_t h1 = seed; - - uint32_t c1 = 0xcc9e2d51; - uint32_t c2 = 0x1b873593; - - //---------- - // body - - const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); - - for(i = -nblocks; i; i++) - { - uint32_t k1 = getblock(blocks,i); - - k1 *= c1; - k1 = ROTL32(k1,15); - k1 *= c2; - - h1 ^= k1; - h1 = ROTL32(h1,13); - h1 = h1*5+0xe6546b64; - } - - //---------- - // tail - - const uint8_t * tail = (const uint8_t*)(data + nblocks*4); - - uint32_t k1 = 0; - - switch(len & 3) - { - case 3: k1 ^= tail[2] << 16; - case 2: k1 ^= tail[1] << 8; - case 1: k1 ^= tail[0]; - k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; - }; - - //---------- - // finalization - - h1 ^= len; - - h1 = fmix32(h1); - - *(uint32_t*)out = h1; -} - -//----------------------------------------------------------------------------- - -void MurmurHash3_x86_128 ( const void * key, const int len, - uint32_t seed, void * out ) -{ - const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 16; - int i; - - uint32_t h1 = seed; - uint32_t h2 = seed; - uint32_t h3 = seed; - uint32_t h4 = seed; - - uint32_t c1 = 0x239b961b; - uint32_t c2 = 0xab0e9789; - uint32_t c3 = 0x38b34ae5; - uint32_t c4 = 0xa1e38b93; - - //---------- - // body - - const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); - - for(i = -nblocks; i; i++) - { - uint32_t k1 = getblock(blocks,i*4+0); - uint32_t k2 = getblock(blocks,i*4+1); - uint32_t k3 = getblock(blocks,i*4+2); - uint32_t k4 = getblock(blocks,i*4+3); - - k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; - - h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; - - k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; - - h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; - - k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; - - h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; - - k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; - - h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; - } - - //---------- - // tail - - const uint8_t * tail = (const uint8_t*)(data + nblocks*16); - - uint32_t k1 = 0; - uint32_t k2 = 0; - uint32_t k3 = 0; - uint32_t k4 = 0; - - switch(len & 15) - { - case 15: k4 ^= tail[14] << 16; - case 14: k4 ^= tail[13] << 8; - case 13: k4 ^= tail[12] << 0; - k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; - - case 12: k3 ^= tail[11] << 24; - case 11: k3 ^= tail[10] << 16; - case 10: k3 ^= tail[ 9] << 8; - case 9: k3 ^= tail[ 8] << 0; - k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; - - case 8: k2 ^= tail[ 7] << 24; - case 7: k2 ^= tail[ 6] << 16; - case 6: k2 ^= tail[ 5] << 8; - case 5: k2 ^= tail[ 4] << 0; - k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; - - case 4: k1 ^= tail[ 3] << 24; - case 3: k1 ^= tail[ 2] << 16; - case 2: k1 ^= tail[ 1] << 8; - case 1: k1 ^= tail[ 0] << 0; - k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; - }; - - //---------- - // finalization - - h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; - - h1 += h2; h1 += h3; h1 += h4; - h2 += h1; h3 += h1; h4 += h1; - - h1 = fmix32(h1); - h2 = fmix32(h2); - h3 = fmix32(h3); - h4 = fmix32(h4); - - h1 += h2; h1 += h3; h1 += h4; - h2 += h1; h3 += h1; h4 += h1; - - ((uint32_t*)out)[0] = h1; - ((uint32_t*)out)[1] = h2; - ((uint32_t*)out)[2] = h3; - ((uint32_t*)out)[3] = h4; -} - -//----------------------------------------------------------------------------- - -void MurmurHash3_x64_128 ( const void * key, const int len, - const uint32_t seed, void * out ) -{ - const uint8_t * data = (const uint8_t*)key; - const int nblocks = len / 16; - int i; - - uint64_t h1 = seed; - uint64_t h2 = seed; - - uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); - uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); - - //---------- - // body - - const uint64_t * blocks = (const uint64_t *)(data); - - for(i = 0; i < nblocks; i++) - { - uint64_t k1 = getblock(blocks,i*2+0); - uint64_t k2 = getblock(blocks,i*2+1); - - k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; - - h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; - - k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; - - h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; - } - - //---------- - // tail - - const uint8_t * tail = (const uint8_t*)(data + nblocks*16); - - uint64_t k1 = 0; - uint64_t k2 = 0; - - switch(len & 15) - { - case 15: k2 ^= (uint64_t)(tail[14]) << 48; - case 14: k2 ^= (uint64_t)(tail[13]) << 40; - case 13: k2 ^= (uint64_t)(tail[12]) << 32; - case 12: k2 ^= (uint64_t)(tail[11]) << 24; - case 11: k2 ^= (uint64_t)(tail[10]) << 16; - case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; - case 9: k2 ^= (uint64_t)(tail[ 8]) << 0; - k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; - - case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; - case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; - case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; - case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; - case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; - case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; - case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; - case 1: k1 ^= (uint64_t)(tail[ 0]) << 0; - k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; - }; - - //---------- - // finalization - - h1 ^= len; h2 ^= len; - - h1 += h2; - h2 += h1; - - h1 = fmix64(h1); - h2 = fmix64(h2); - - h1 += h2; - h2 += h1; - - ((uint64_t*)out)[0] = h1; - ((uint64_t*)out)[1] = h2; -} - -//----------------------------------------------------------------------------- - diff --git a/make_png/murmur3.h b/make_png/murmur3.h deleted file mode 100644 index 90878b00d..000000000 --- a/make_png/murmur3.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _MURMURHASH3_H_ -#define _MURMURHASH3_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -//----------------------------------------------------------------------------- - -void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out); - -void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out); - -void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out); - -//----------------------------------------------------------------------------- - -#ifdef __cplusplus -} -#endif - -#endif // _MURMURHASH3_H_ diff --git a/tests/make_bitmap/README b/tests/make_bitmap/README new file mode 100644 index 000000000..98208b50a --- /dev/null +++ b/tests/make_bitmap/README @@ -0,0 +1,18 @@ +Use the "new" folder. + + +To compile and execute + +gcc example1.c ksvsk.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example1 +./example1 + +gcc example2.c ksvsk.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example2 +./example2 + + +NOTE + +-> DONOT use Get_Padding render MONO / GRAYSCALE bitmaps. + Use FT_Bitmap_Convert to make the width of bitmap a multiple of 4 bytes and then write it to file. + + diff --git a/tests/make_bitmap/bitmap.c b/tests/make_bitmap/bitmap.c new file mode 100644 index 000000000..4217c3c16 --- /dev/null +++ b/tests/make_bitmap/bitmap.c @@ -0,0 +1,269 @@ +#include "bitmap.h" + +HASH_128 * Generate_Hash_x64_128(FT_Bitmap * bitmap, HASH_128 * murmur) +{ + int seed = 99; // Dont change + + MurmurHash3_x64_128(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash); + + return murmur; +} + +HASH_128 * Generate_Hash_x86_128(FT_Bitmap * bitmap, HASH_128 * murmur) +{ + int seed = 99; // Dont change + + MurmurHash3_x86_128(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash); + + return murmur; +} + +HASH_32 * Generate_Hash_x86_32(FT_Bitmap * bitmap, HASH_32 * murmur) +{ + int seed = 99; // Dont change + + MurmurHash3_x86_32(bitmap->buffer, (bitmap->pitch * bitmap->rows), seed, murmur->hash); + + 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); +} + +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 ) { + + FILE *fp = fopen("test.bmp","w"); // Bitmap File + HEADER *header = ( HEADER* ) calloc( 1 , sizeof( HEADER )); + + unsigned char pixel_mode = ( bitmap->pixel_mode ); + FT_UInt image_width; + FT_UInt image_rows; + FT_UInt color_table_size = 0; + + switch(pixel_mode){ + + case 5 : image_width = bitmap->width / 3; // LCD + image_rows = bitmap->rows; + break; + + case 6 : image_width = bitmap->width; // LCD_V + image_rows = bitmap->rows / 3; + break; + + default : image_width = bitmap->width; // MONO and GRAY + image_rows = bitmap->rows; + color_table_size = 4* 256; + break; + } + + FT_Int image_size; + image_size = (image_rows * image_width ); + + 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; + + 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; + + fwrite (header, 1, sizeof(HEADER),fp); + free(header); + fclose(fp); +} + +void Write_Bitmap_Data_LCD_RGB( FT_Bitmap * bitmap ){ + + char value; + int i,j,k; + + FILE *fp = fopen("test.bmp","a"); + + 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); + } + } + + fclose(fp); +} + +void Write_Bitmap_Data_LCD_BGR( FT_Bitmap * bitmap ){ + + char value; + int i,j,k; + + FILE *fp = fopen("test.bmp","a"); + + 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); + } + } + + fclose(fp); +} + +void Write_Bitmap_Data_LCD_V_BGR (FT_Bitmap * bitmap) { + + FILE *fp = fopen("test.bmp","a"); + + int i,j,k,step; + char value; + step = 0; + + while ( step < bitmap->rows ){ + + 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 + } + + fclose(fp); +} + +void Write_Bitmap_Data_LCD_V_RGB (FT_Bitmap * bitmap) { + + FILE *fp = fopen("test.bmp","a"); + + int i,j,k,step; + char value; + step = 0; + + while ( step < bitmap->rows ){ + + 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 + } + + fclose(fp); +} + +void Write_Bitmap_Data_MONO (FT_Bitmap * bitmap) { + + FILE *fp = fopen("test.bmp","a"); + + 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 (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); + } + } + + fclose(fp); +} + +void Write_Bitmap_Data_GRAY(FT_Bitmap * bitmap) { + + FILE *fp = fopen("test.bmp","a"); + 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 (int i = 0; i < bitmap->pitch * bitmap->rows; ++i) + { + value = 255 - bitmap->buffer[i]; + fwrite(&value,1,1,fp); + } + + fclose(fp); +} \ No newline at end of file diff --git a/tests/make_bitmap/bitmap.h b/tests/make_bitmap/bitmap.h new file mode 100644 index 000000000..f2e309bd5 --- /dev/null +++ b/tests/make_bitmap/bitmap.h @@ -0,0 +1,85 @@ +#include +#include +#include "murmur3.h" // MurmurHash3_x64_128 header file + +#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 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]; +}HASH_32; + +typedef struct { // To store 128bit Hash + FT_UInt32 hash[4]; +}HASH_128; + +#pragma pack(pop) + +//------------------------------------------------------------------------------- + +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); + +int Get_Bits_Per_Pixel ( unsigned char PIXEL_MODE ); + +void Write_Bitmap_Header (FT_Bitmap * bitmap ); + +void Write_Bitmap_Data_MONO (FT_Bitmap * bitmap); +void Write_Bitmap_Data_GRAY (FT_Bitmap * bitmap); +void Write_Bitmap_Data_LCD_BGR (FT_Bitmap * bitmap); +void Write_Bitmap_Data_LCD_RGB (FT_Bitmap * bitmap); +void Write_Bitmap_Data_LCD_V_RGB (FT_Bitmap * bitmap); +void Write_Bitmap_Data_LCD_V_BGR (FT_Bitmap * bitmap); diff --git a/tests/make_bitmap/example1.c b/tests/make_bitmap/example1.c new file mode 100644 index 000000000..9bc61216d --- /dev/null +++ b/tests/make_bitmap/example1.c @@ -0,0 +1,98 @@ +// This example uses the FreeType rendering library to produce +// LCD rendered bitmap (RGB) for the glyphs of a font +// Also gives the MurmurHash3 hash value of the bitmap rendered. + +#include "bitmap.h" + +int main(int argc, char const *argv[]) +{ + FT_Library library; + FT_Face face; + FT_GlyphSlot slot; + FT_Bitmap* bitmap; + FT_Error error; + + const char* font_file; // Arguments + FT_UInt32 size; + FT_UInt glyph_index; + const char* character; + + if (argc != 4) + { + printf("\nTo render a RGB bitmap of a glyph to be displayed on an \n"); + printf("LCD surface and to generate the 128bit MurmurHash3 hash \n\n"); + + printf("Filter used is FT_LCD_FILTER_DEFAULT\n\n"); + + printf("Usage ./ \n\n"); + + exit(0); + } + + font_file = argv[1]; + size = atoi(argv[2]); + character = argv[3]; + + error = FT_Init_FreeType( &library ); + if(error){ + printf("Error while initialising library\n"); + } + + error = FT_Library_SetLcdFilter( library, + FT_LCD_FILTER_DEFAULT ); + if(error){ + printf("Error while setting LCD filter\n"); + } + + error = FT_New_Face( library, + font_file, + 0, + &face ); + if(error){ + printf("Error loading new face\n"); + } + + error = FT_Set_Char_Size( face, + size * 64, + 0, + 100, + 0 ); + if(error){ + printf("Error setting character size\n"); + } + + glyph_index = FT_Get_Char_Index( face, *character ); + + slot = face->glyph; + + error = FT_Load_Glyph( face, + glyph_index, + FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_LCD ); + if(error){ + printf("Error loading glyph\n"); + } + + FT_Render_Glyph( slot, + FT_RENDER_MODE_LCD); + if(error){ + printf("Error rendering the glyph\n"); + } + + bitmap = &slot->bitmap; + + Write_Bitmap_Header(bitmap); + Write_Bitmap_Data_LCD_RGB(bitmap); + + HASH_128 * murmur = (HASH_128 *) malloc(sizeof(HASH_128)) ; + murmur = Generate_Hash_x64_128(bitmap,murmur); // using the x64_128 function of the hash + + printf("%08x %08x %08x %08x\n", murmur->hash[0], // Print hash + murmur->hash[1], + murmur->hash[2], + murmur->hash[3]); + + FT_Done_Face ( face ); + FT_Done_FreeType( library ); + + return 0; +} \ No newline at end of file diff --git a/tests/make_bitmap/example2.c b/tests/make_bitmap/example2.c new file mode 100644 index 000000000..4fa791075 --- /dev/null +++ b/tests/make_bitmap/example2.c @@ -0,0 +1,93 @@ +// This example uses the FreeType rendering library to print +// the MurmurHash3 hash value of the 8-bit anti-aliased +// bitmaps of all the glyphs in the font. + +#include "bitmap.h" // The header file + +int main(int argc, char const *argv[]) +{ + FT_Library library; + FT_Face face; + FT_GlyphSlot slot; + FT_Bitmap* bitmap; + + FT_Error error; + + const char* filename; + FT_UInt32 size; + + if (argc != 3) + { + printf("This program prints the hashes of 8-bit grayscale\n"); + printf("anti-aliased bitmaps of all the glyphs in a font\n\n"); + + printf("Usage ./ \n\n"); + exit(0); + } + + + filename = argv[1]; + size = atoi(argv[2]); + + error = FT_Init_FreeType( &library ); + if(error){ + printf("Error while initialising library\n"); + } + + error = FT_New_Face( library, + filename, + 0, + &face ); + if(error){ + printf("Error loading new face\n"); + } + + error = FT_Set_Char_Size( face, + size * 64, + 0, + 100, + 0 ); + if(error){ + printf("Error setting character size\n"); + } + + slot = face->glyph; + + for (int i = 0; i < face->num_glyphs; ++i) // Loop + { + error = FT_Load_Glyph( face, + i, + FT_LOAD_DEFAULT); + if(error){ + printf("Error loading glyph\n"); + } + + FT_Render_Glyph( slot, + FT_RENDER_MODE_NORMAL); + if(error){ + printf("Error rendering the glyph\n"); + } + + bitmap = &slot->bitmap; + + if (bitmap->width == 0|| bitmap->rows == 0) + { + printf("%d - Empty Glyph\n",i); + continue; + } + + HASH_128 * murmur = (HASH_128 *) malloc(sizeof(HASH_128)) ; + murmur = Generate_Hash_x64_128(bitmap,murmur); + + printf("%d - %08x %08x %08x %08x\n",i, + murmur->hash[0], + murmur->hash[1], + murmur->hash[2], + murmur->hash[3]); + } + + FT_Done_Face ( face ); + FT_Done_FreeType( library ); + + return 0; +} \ No newline at end of file diff --git a/tests/make_bitmap/murmur3.c b/tests/make_bitmap/murmur3.c new file mode 100644 index 000000000..484ebfd03 --- /dev/null +++ b/tests/make_bitmap/murmur3.c @@ -0,0 +1,312 @@ +//----------------------------------------------------------------------------- +// Note - The x86 and x64 versions do _not_ produce the same results, as the +// algorithms are optimized for their respective platforms. You can still +// compile and run any of them on any platform, but your performance with the +// non-native version will be less than optimal. + +#include "murmur3.h" + +//----------------------------------------------------------------------------- +// Platform-specific functions and macros + +#ifdef __GNUC__ +#define FORCE_INLINE __attribute__((always_inline)) inline +#else +#define FORCE_INLINE inline +#endif + +static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r ) +{ + return (x << r) | (x >> (32 - r)); +} + +static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r ) +{ + return (x << r) | (x >> (64 - r)); +} + +#define ROTL32(x,y) rotl32(x,y) +#define ROTL64(x,y) rotl64(x,y) + +#define BIG_CONSTANT(x) (x##LLU) + +//----------------------------------------------------------------------------- +// Block read - if your platform needs to do endian-swapping or can only +// handle aligned reads, do the conversion here + +#define getblock(p, i) (p[i]) + +//----------------------------------------------------------------------------- +// Finalization mix - force all bits of a hash block to avalanche + +static FORCE_INLINE uint32_t fmix32 ( uint32_t h ) +{ + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + + return h; +} + +//---------- + +static FORCE_INLINE uint64_t fmix64 ( uint64_t k ) +{ + k ^= k >> 33; + k *= BIG_CONSTANT(0xff51afd7ed558ccd); + k ^= k >> 33; + k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); + k ^= k >> 33; + + return k; +} + +//----------------------------------------------------------------------------- + +void MurmurHash3_x86_32 ( const void * key, int len, + uint32_t seed, void * out ) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 4; + int i; + + uint32_t h1 = seed; + + uint32_t c1 = 0xcc9e2d51; + uint32_t c2 = 0x1b873593; + + //---------- + // body + + const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); + + for(i = -nblocks; i; i++) + { + uint32_t k1 = getblock(blocks,i); + + k1 *= c1; + k1 = ROTL32(k1,15); + k1 *= c2; + + h1 ^= k1; + h1 = ROTL32(h1,13); + h1 = h1*5+0xe6546b64; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*4); + + uint32_t k1 = 0; + + switch(len & 3) + { + case 3: k1 ^= tail[2] << 16; + case 2: k1 ^= tail[1] << 8; + case 1: k1 ^= tail[0]; + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; + + h1 = fmix32(h1); + + *(uint32_t*)out = h1; +} + +//----------------------------------------------------------------------------- + +void MurmurHash3_x86_128 ( const void * key, const int len, + uint32_t seed, void * out ) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 16; + int i; + + uint32_t h1 = seed; + uint32_t h2 = seed; + uint32_t h3 = seed; + uint32_t h4 = seed; + + uint32_t c1 = 0x239b961b; + uint32_t c2 = 0xab0e9789; + uint32_t c3 = 0x38b34ae5; + uint32_t c4 = 0xa1e38b93; + + //---------- + // body + + const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); + + for(i = -nblocks; i; i++) + { + uint32_t k1 = getblock(blocks,i*4+0); + uint32_t k2 = getblock(blocks,i*4+1); + uint32_t k3 = getblock(blocks,i*4+2); + uint32_t k4 = getblock(blocks,i*4+3); + + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + + h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; + + k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; + + h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; + + k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; + + h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; + + k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; + + h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*16); + + uint32_t k1 = 0; + uint32_t k2 = 0; + uint32_t k3 = 0; + uint32_t k4 = 0; + + switch(len & 15) + { + case 15: k4 ^= tail[14] << 16; + case 14: k4 ^= tail[13] << 8; + case 13: k4 ^= tail[12] << 0; + k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; + + case 12: k3 ^= tail[11] << 24; + case 11: k3 ^= tail[10] << 16; + case 10: k3 ^= tail[ 9] << 8; + case 9: k3 ^= tail[ 8] << 0; + k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; + + case 8: k2 ^= tail[ 7] << 24; + case 7: k2 ^= tail[ 6] << 16; + case 6: k2 ^= tail[ 5] << 8; + case 5: k2 ^= tail[ 4] << 0; + k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; + + case 4: k1 ^= tail[ 3] << 24; + case 3: k1 ^= tail[ 2] << 16; + case 2: k1 ^= tail[ 1] << 8; + case 1: k1 ^= tail[ 0] << 0; + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; + + h1 += h2; h1 += h3; h1 += h4; + h2 += h1; h3 += h1; h4 += h1; + + h1 = fmix32(h1); + h2 = fmix32(h2); + h3 = fmix32(h3); + h4 = fmix32(h4); + + h1 += h2; h1 += h3; h1 += h4; + h2 += h1; h3 += h1; h4 += h1; + + ((uint32_t*)out)[0] = h1; + ((uint32_t*)out)[1] = h2; + ((uint32_t*)out)[2] = h3; + ((uint32_t*)out)[3] = h4; +} + +//----------------------------------------------------------------------------- + +void MurmurHash3_x64_128 ( const void * key, const int len, + const uint32_t seed, void * out ) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 16; + int i; + + uint64_t h1 = seed; + uint64_t h2 = seed; + + uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); + uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); + + //---------- + // body + + const uint64_t * blocks = (const uint64_t *)(data); + + for(i = 0; i < nblocks; i++) + { + uint64_t k1 = getblock(blocks,i*2+0); + uint64_t k2 = getblock(blocks,i*2+1); + + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; + + h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; + + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; + + h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*16); + + uint64_t k1 = 0; + uint64_t k2 = 0; + + switch(len & 15) + { + case 15: k2 ^= (uint64_t)(tail[14]) << 48; + case 14: k2 ^= (uint64_t)(tail[13]) << 40; + case 13: k2 ^= (uint64_t)(tail[12]) << 32; + case 12: k2 ^= (uint64_t)(tail[11]) << 24; + case 11: k2 ^= (uint64_t)(tail[10]) << 16; + case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; + case 9: k2 ^= (uint64_t)(tail[ 8]) << 0; + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; + + case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; + case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; + case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; + case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; + case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; + case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; + case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; + case 1: k1 ^= (uint64_t)(tail[ 0]) << 0; + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; h2 ^= len; + + h1 += h2; + h2 += h1; + + h1 = fmix64(h1); + h2 = fmix64(h2); + + h1 += h2; + h2 += h1; + + ((uint64_t*)out)[0] = h1; + ((uint64_t*)out)[1] = h2; +} + +//----------------------------------------------------------------------------- + diff --git a/tests/make_bitmap/murmur3.h b/tests/make_bitmap/murmur3.h new file mode 100644 index 000000000..90878b00d --- /dev/null +++ b/tests/make_bitmap/murmur3.h @@ -0,0 +1,24 @@ +#ifndef _MURMURHASH3_H_ +#define _MURMURHASH3_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +//----------------------------------------------------------------------------- + +void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out); + +void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out); + +void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out); + +//----------------------------------------------------------------------------- + +#ifdef __cplusplus +} +#endif + +#endif // _MURMURHASH3_H_ diff --git a/tests/make_bitmap/runme.sh b/tests/make_bitmap/runme.sh new file mode 100755 index 000000000..4243ea5d7 --- /dev/null +++ b/tests/make_bitmap/runme.sh @@ -0,0 +1,4 @@ +#!/bin/bash +gcc example1.c bitmap.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example1 +gcc example2.c bitmap.c murmur3.c -Wall -I /usr/local/include/freetype2/ -lfreetype -o example2 + diff --git a/tests/make_png/README b/tests/make_png/README new file mode 100644 index 000000000..677cf65dc --- /dev/null +++ b/tests/make_png/README @@ -0,0 +1,37 @@ +To generate hashes and store it in the ./hashes folder, + +1) sudo make hash + (set resoluton in DPI by passing argument + example: sudo make DPI=100, if not specified,default is 72) + +2) Usage ./hash + + Hashes will be saved in a file named + $(font)_$(pt_size)_$(render_mode).hash + + By default, hashes of 256-level gray bitmaps will be generated + + Values for render_mode 0 - monochrome + 1 - anti-aliased + 2 - lcd horizontal + 3 - lcd vertical + +To generate 32-bit RGBA PNG(s) of all glyphs in a font\n + +1) sudo make png + (set resoluton in DPI by passing argument + example: sudo make DPI=100, if not specified,default is 72) + +2.Usage ./ + + Images will be saved in a file named + $(font)_$(pt_size)_$(render_mode)_$(glyph_index).png + + By default, hashes of 256-level gray bitmaps will be generated + + Values for render_mode 0 - monochrome + 1 - anti-aliased + 2 - lcd horizontal-RGB + 3 - lcd horizontal-BGR + 4 - lcd vertical-RGB + 5 - lcd vertical-BGR \ No newline at end of file diff --git a/tests/make_png/bitmap.c b/tests/make_png/bitmap.c new file mode 100644 index 000000000..104acdc1c --- /dev/null +++ b/tests/make_png/bitmap.c @@ -0,0 +1,282 @@ +#include "bitmap.h" + +HASH_128 * Generate_Hash_x64_128( FT_Bitmap * bitmap, + HASH_128 * murmur) +{ + int seed = 99; // Dont change + + MurmurHash3_x64_128(bitmap->buffer, + (bitmap->pitch * bitmap->rows), + seed, + murmur->hash); + + return murmur; +} + +HASH_128 * Generate_Hash_x86_128( FT_Bitmap * bitmap, + HASH_128 * murmur) +{ + int seed = 99; // Dont change + + MurmurHash3_x86_128(bitmap->buffer, + (bitmap->pitch * bitmap->rows), + seed, + murmur->hash); + + return murmur; +} + +HASH_32 * Generate_Hash_x86_32( FT_Bitmap * bitmap, + HASH_32 * murmur) +{ + int seed = 99; // Dont change + + MurmurHash3_x86_32( bitmap->buffer, + (bitmap->pitch * bitmap->rows), + seed, + murmur->hash); + + return murmur; +} + +PIXEL * Pixel_At (IMAGE * bitmap, int x, int y) +{ + return bitmap->pixels + bitmap->width * y + x; +} + +int Generate_PNG (IMAGE *bitmap, + const char *path, + int render_mode) +{ + FILE * fp; + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + + size_t x, y; + png_byte ** row_pointers = NULL; + + int status = -1; + + int pixel_size = 4; + int depth = 8; + + fp = fopen (path, "wb"); + if (! fp) { + goto fopen_failed; + } + + 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; + } + + 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; + } + } + + png_init_io ( png_ptr, + fp); + + png_set_rows (png_ptr, + info_ptr, + row_pointers); + + png_write_png ( png_ptr, + info_ptr, + PNG_TRANSFORM_IDENTITY, + NULL); + + 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 Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){ + + IMAGE fruit; + int x; + int y; + + unsigned char value; + int p; + + switch(render_mode){ + + case 0 : fruit.width = bitmap->width; // MONO and GRAY + fruit.height = bitmap->rows; + + fruit.pixels = calloc ( fruit.width * fruit.height, + sizeof (PIXEL)); + + for (y = 0; y < fruit.height; y++) { + for (x = 0; x < fruit.width; x++) { + + PIXEL * pixel = Pixel_At (& fruit, x, y); + p = (y * bitmap->pitch ) + x; + + value = bitmap->buffer[p]; + + if ( value != 0x00 ){ + value = 0xff; + }else{ + value = 0x00; + } + + 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; + + fruit.pixels = calloc ( fruit.width * fruit.height, + sizeof (PIXEL)); + + for (y = 0; y < fruit.height; y++) { + for (x = 0; x < fruit.width; x++) { + + PIXEL * pixel = Pixel_At (& fruit, x, y); + p = (y * bitmap->pitch ) + x; + + value = bitmap->buffer[p]; + + pixel->red = 255- value; + pixel->green = 255- value; + pixel->blue = 255- value; + pixel->alpha = 255; + } + } + break; + + case 2 : + case 3 : fruit.width = bitmap->width / 3; // LCD + fruit.height = bitmap->rows; + + fruit.pixels = calloc ( fruit.width * fruit.height, + sizeof (PIXEL)); + + for (y = 0; y < fruit.height; y++) { + for (x = 0; x < fruit.width; x++) { + + PIXEL * pixel = Pixel_At (& fruit, x, y); + p = (y * bitmap->pitch ) + (x)*3; + + value = bitmap->buffer[p]; + pixel->red = 255- value; + p++; + + value = bitmap->buffer[p]; + pixel->green = 255- value; + p++; + + value = bitmap->buffer[p]; + pixel->blue = 255- value; + + pixel->alpha = 255; + } + } + break; + + case 4 : + case 5 : fruit.width = bitmap->width; // LCD_V + fruit.height = bitmap->rows / 3; + + fruit.pixels = calloc ( fruit.width * fruit.height, + sizeof (PIXEL)); + + 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; + + value = bitmap->buffer[p]; + pixel->red = 255- value; + p += bitmap->pitch; + + value = bitmap->buffer[p]; + pixel->green = 255- value; + p += bitmap->pitch; + + value = bitmap->buffer[p]; + pixel->blue = 255- value; + + pixel->alpha = 255; + } + } + break; + + default : fruit.width = bitmap->width; + fruit.height = bitmap->rows; + break; + } + + char * file_name = ( char * )calloc(150,sizeof(char)); + + sprintf(file_name, "%s_%d", name, i); + + Generate_PNG (& fruit, file_name, render_mode); + + free (fruit.pixels); +} \ No newline at end of file diff --git a/tests/make_png/bitmap.h b/tests/make_png/bitmap.h new file mode 100644 index 000000000..426d62218 --- /dev/null +++ b/tests/make_png/bitmap.h @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include +#include + +#include + +#include "murmur3.h" // MurmurHash3_x64_128 header file + +#include + + +#include FT_FREETYPE_H +#include FT_MODULE_H +#include FT_LCD_FILTER_H +#include FT_BITMAP_H + +#define BITS_PER_PIXEL_RGBA 32 + +typedef struct { // To store 32bit Hash + FT_UInt32 hash[1]; +}HASH_32; + +typedef struct { // To store 128bit Hash + FT_UInt32 hash[4]; +}HASH_128; + +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); + +//------------------------------------------------------------------------------ + +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); + diff --git a/tests/make_png/hash_to_file.c b/tests/make_png/hash_to_file.c new file mode 100644 index 000000000..e3446594b --- /dev/null +++ b/tests/make_png/hash_to_file.c @@ -0,0 +1,151 @@ +#include "bitmap.h" + +int main (int argc, char const *argv[]) +{ + FT_Library library; + FT_Face face; + FT_GlyphSlot slot; + + FT_Bitmap* bitmap; + + FT_Error error; + + const char* font_file; + int size; + int render_mode; // argument + + int load_flag; // FT_LOAD_XXX + int render_flag; // FT_RENDER_MODE_XXX + int target_flag; // FT_LOAD_TARGET_XXX + char* render_type; // for file_name + + char name[100]; // hashes file name + int i; // for loop + + if (argc != 4) + { + printf("\nTo generate MurmurHash3 hash values of all glyphs\n"); + printf("Hashes will be saved in a file named\n \ + $(font)_$(pt_size)_$(render_mode).hash \n\n"); + + printf("Default: hashes of 256-level gray bitmaps will be generated\n\n"); + + printf("Usage ./ \n\n"); + + printf("Values for render_mode 0 - monochrome\n"); + printf(" 1 - anti-aliased\n"); + printf(" 2 - lcd horizontal-RGB\n"); + printf(" 3 - lcd vertical-RGB\n"); + + return 1; + } + + font_file = argv[1]; + size = atof(argv[2]); + render_mode = atoi(argv[3]); + + error = FT_Init_FreeType( &library ); + if(error){ + printf("Error while initialising library\n"); + } + + if (render_mode > 1 ) + { + error = FT_Library_SetLcdFilter( library, + FT_LCD_FILTER_DEFAULT ); + if(error){ + printf("Error while setting LCD filter\n"); + } + } + + error = FT_New_Face( library, + font_file, + 0, + &face ); + if(error){ + printf("Error loading new face\n"); + } + + error = FT_Set_Char_Size( face, + size * 64, + 0, + DPI, + 0 ); + if(error){ + printf("Error setting character size\n"); + } + + switch ( render_mode ) { + case 0: render_flag = FT_RENDER_MODE_MONO; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_MONO; + render_type = "mono"; + break; + + case 1: render_flag = FT_RENDER_MODE_NORMAL; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_NORMAL; + render_type = "gray"; + break; + + case 2: render_flag = FT_RENDER_MODE_LCD; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_LCD; + render_type = "lcd_rgb"; + break; + + case 3: render_flag = FT_RENDER_MODE_LCD_V; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_LCD_V; + render_type = "lcd_ver_rgb"; + break; + + default:render_flag = FT_RENDER_MODE_NORMAL; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_NORMAL; + render_type = "gray"; + } + + slot = face->glyph; + + FILE* fp; + + sprintf(name,"./hashes/%s_%d_%s.hash",font_file, + size, + render_type); + + fp = fopen(name,"w"); + + for (i = 0; i < face->num_glyphs; ++i) + { + error = FT_Load_Glyph( face, + i, + load_flag | target_flag); + if(error){ + printf("Error loading glyph\n"); + } + + FT_Render_Glyph( slot, + render_flag); + if(error){ + printf("Error rendering the 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], // Print hash + murmur->hash[1], + murmur->hash[2], + murmur->hash[3]); + } + fclose(fp); + + FT_Done_Face ( face ); + FT_Done_FreeType( library ); + + return 0; + +} diff --git a/tests/make_png/make_png.c b/tests/make_png/make_png.c new file mode 100644 index 000000000..8b59d4455 --- /dev/null +++ b/tests/make_png/make_png.c @@ -0,0 +1,178 @@ +#include "bitmap.h" + +int main (int argc, char const *argv[]) +{ + FT_Library library; + FT_Face face; + FT_GlyphSlot slot; + + FT_Bitmap* bitmap; + + FT_Error error; + + const char* font_file; + int size; + int render_mode; // argument + + int load_flag; // FT_LOAD_XXX + int render_flag; // FT_RENDER_MODE_XXX + int target_flag; // FT_LOAD_TARGET_XXX + char* render_type; // for file_name + + char name[100]; // hashes file name + int i; // for loop + + if (argc != 4) + { + printf("\nTo generate 32-bit RGBA PNG(s) of all glyphs in a font\n"); + printf("Images will be saved in a file named \n\ + $(font)_$(pt_size)_$(render_mode)_$(glyph_index).png \n\n"); + + printf("Default: hashes of 256-level gray bitmaps will be generated\n\n"); + + printf("Usage ./ \n\n"); + + printf("Values for render_mode 0 - monochrome\n"); + printf(" 1 - anti-aliased\n"); + printf(" 2 - lcd horizontal-RGB\n"); + printf(" 3 - lcd horizontal-BGR\n"); + printf(" 4 - lcd vertical-RGB\n"); + printf(" 5 - lcd vertical-BGR\n"); + + return 1; + } + + font_file = argv[1]; + size = atof(argv[2]); + render_mode = atoi(argv[3]); + + error = FT_Init_FreeType( &library ); + if(error){ + printf("Error while initialising library\n"); + } + + if (render_mode > 1 ) + { + error = FT_Library_SetLcdFilter( library, + FT_LCD_FILTER_DEFAULT ); + if(error){ + printf("Error while setting LCD filter\n"); + } + } + + error = FT_New_Face( library, + font_file, + 0, + &face ); + if(error){ + printf("Error loading new face\n"); + } + + error = FT_Set_Char_Size( face, + size * 64, + 0, + DPI, + 0 ); + if(error){ + printf("Error setting character size\n"); + } + + switch ( render_mode ) { + case 0: render_flag = FT_RENDER_MODE_MONO; + load_flag = FT_LOAD_MONOCHROME; + target_flag = FT_LOAD_TARGET_MONO; + render_type = "mono"; + break; + + case 1: render_flag = FT_RENDER_MODE_NORMAL; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_NORMAL; + render_type = "gray"; + break; + + case 2: render_flag = FT_RENDER_MODE_LCD; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_LCD; + render_type = "lcd_rgb"; + break; + + case 3: render_flag = FT_RENDER_MODE_LCD; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_LCD; + render_type = "lcd_bgr"; + break; + + case 4: render_flag = FT_RENDER_MODE_LCD_V; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_LCD_V; + render_type = "lcd_ver_rgb"; + break; + + case 5: render_flag = FT_RENDER_MODE_LCD_V; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_LCD_V; + render_type = "lcd_ver_bgr"; + break; + + default:render_flag = FT_RENDER_MODE_NORMAL; + load_flag = FT_LOAD_DEFAULT; + target_flag = FT_LOAD_TARGET_NORMAL; + render_type = "gray"; + } + + slot = face->glyph; + + sprintf(name,"./images/%s_%d_%s", font_file, + size, + render_type); + + for (i = 0; i num_glyphs; ++i) + { + error = FT_Load_Glyph( face, + i, + load_flag | target_flag); + if(error){ + printf("Error loading glyph\n"); + } + + FT_Render_Glyph( slot, + render_flag); + if(error){ + printf("Error rendering the glyph\n"); + } + + bitmap = &slot->bitmap; + + if (bitmap->width == 0 || bitmap->rows == 0) + { + continue; + } + + FT_Bitmap target; + FT_Bitmap_Init( &target ); + + int alignment = 4; + error = FT_Bitmap_Convert( library, + bitmap, + &target, + alignment); + if(error){ + printf("Error converting the bitmap\n"); + } + + if (render_mode == 0) + { + Make_PNG(&target,name,i,render_mode); + + }else{ + + Make_PNG(bitmap,name,i,render_mode); + } + } + + FT_Done_Face ( face ); + FT_Done_FreeType( library ); + + return 0; + +} diff --git a/tests/make_png/makefile b/tests/make_png/makefile new file mode 100644 index 000000000..691a6fd1b --- /dev/null +++ b/tests/make_png/makefile @@ -0,0 +1,28 @@ +# Define required macros here + +SHELL = /bin/sh + +SRC_HASH = hash_to_file.c bitmap.c murmur3.c +SRC_PNG = make_png.c bitmap.c murmur3.c +OBJS = $(src:.c=.o) + +CFLAGS = -Wall -g +CC = gcc +INCLUDE = -I /usr/local/include/freetype2/ +LIBS = -lfreetype -lpng + +DPI = 72 + +all: png hash + +png:$(SRC_PNG) + $(CC) $(CFLAGS) $(INCLUDE) -DDPI=$(DPI) -o $@ $(SRC_PNG) $(OBJS) $(LIBS) + +hash:$(SRC_HASH) + $(CC) $(CFLAGS) $(INCLUDE) -DDPI=$(DPI) -o $@ $(SRC_HASH) $(OBJS) $(LIBS) + +.PHONY: clean +clean: + -rm -f *.o + + diff --git a/tests/make_png/murmur3.c b/tests/make_png/murmur3.c new file mode 100644 index 000000000..484ebfd03 --- /dev/null +++ b/tests/make_png/murmur3.c @@ -0,0 +1,312 @@ +//----------------------------------------------------------------------------- +// Note - The x86 and x64 versions do _not_ produce the same results, as the +// algorithms are optimized for their respective platforms. You can still +// compile and run any of them on any platform, but your performance with the +// non-native version will be less than optimal. + +#include "murmur3.h" + +//----------------------------------------------------------------------------- +// Platform-specific functions and macros + +#ifdef __GNUC__ +#define FORCE_INLINE __attribute__((always_inline)) inline +#else +#define FORCE_INLINE inline +#endif + +static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r ) +{ + return (x << r) | (x >> (32 - r)); +} + +static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r ) +{ + return (x << r) | (x >> (64 - r)); +} + +#define ROTL32(x,y) rotl32(x,y) +#define ROTL64(x,y) rotl64(x,y) + +#define BIG_CONSTANT(x) (x##LLU) + +//----------------------------------------------------------------------------- +// Block read - if your platform needs to do endian-swapping or can only +// handle aligned reads, do the conversion here + +#define getblock(p, i) (p[i]) + +//----------------------------------------------------------------------------- +// Finalization mix - force all bits of a hash block to avalanche + +static FORCE_INLINE uint32_t fmix32 ( uint32_t h ) +{ + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + + return h; +} + +//---------- + +static FORCE_INLINE uint64_t fmix64 ( uint64_t k ) +{ + k ^= k >> 33; + k *= BIG_CONSTANT(0xff51afd7ed558ccd); + k ^= k >> 33; + k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); + k ^= k >> 33; + + return k; +} + +//----------------------------------------------------------------------------- + +void MurmurHash3_x86_32 ( const void * key, int len, + uint32_t seed, void * out ) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 4; + int i; + + uint32_t h1 = seed; + + uint32_t c1 = 0xcc9e2d51; + uint32_t c2 = 0x1b873593; + + //---------- + // body + + const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); + + for(i = -nblocks; i; i++) + { + uint32_t k1 = getblock(blocks,i); + + k1 *= c1; + k1 = ROTL32(k1,15); + k1 *= c2; + + h1 ^= k1; + h1 = ROTL32(h1,13); + h1 = h1*5+0xe6546b64; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*4); + + uint32_t k1 = 0; + + switch(len & 3) + { + case 3: k1 ^= tail[2] << 16; + case 2: k1 ^= tail[1] << 8; + case 1: k1 ^= tail[0]; + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; + + h1 = fmix32(h1); + + *(uint32_t*)out = h1; +} + +//----------------------------------------------------------------------------- + +void MurmurHash3_x86_128 ( const void * key, const int len, + uint32_t seed, void * out ) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 16; + int i; + + uint32_t h1 = seed; + uint32_t h2 = seed; + uint32_t h3 = seed; + uint32_t h4 = seed; + + uint32_t c1 = 0x239b961b; + uint32_t c2 = 0xab0e9789; + uint32_t c3 = 0x38b34ae5; + uint32_t c4 = 0xa1e38b93; + + //---------- + // body + + const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); + + for(i = -nblocks; i; i++) + { + uint32_t k1 = getblock(blocks,i*4+0); + uint32_t k2 = getblock(blocks,i*4+1); + uint32_t k3 = getblock(blocks,i*4+2); + uint32_t k4 = getblock(blocks,i*4+3); + + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + + h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; + + k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; + + h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; + + k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; + + h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; + + k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; + + h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*16); + + uint32_t k1 = 0; + uint32_t k2 = 0; + uint32_t k3 = 0; + uint32_t k4 = 0; + + switch(len & 15) + { + case 15: k4 ^= tail[14] << 16; + case 14: k4 ^= tail[13] << 8; + case 13: k4 ^= tail[12] << 0; + k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; + + case 12: k3 ^= tail[11] << 24; + case 11: k3 ^= tail[10] << 16; + case 10: k3 ^= tail[ 9] << 8; + case 9: k3 ^= tail[ 8] << 0; + k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; + + case 8: k2 ^= tail[ 7] << 24; + case 7: k2 ^= tail[ 6] << 16; + case 6: k2 ^= tail[ 5] << 8; + case 5: k2 ^= tail[ 4] << 0; + k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; + + case 4: k1 ^= tail[ 3] << 24; + case 3: k1 ^= tail[ 2] << 16; + case 2: k1 ^= tail[ 1] << 8; + case 1: k1 ^= tail[ 0] << 0; + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; + + h1 += h2; h1 += h3; h1 += h4; + h2 += h1; h3 += h1; h4 += h1; + + h1 = fmix32(h1); + h2 = fmix32(h2); + h3 = fmix32(h3); + h4 = fmix32(h4); + + h1 += h2; h1 += h3; h1 += h4; + h2 += h1; h3 += h1; h4 += h1; + + ((uint32_t*)out)[0] = h1; + ((uint32_t*)out)[1] = h2; + ((uint32_t*)out)[2] = h3; + ((uint32_t*)out)[3] = h4; +} + +//----------------------------------------------------------------------------- + +void MurmurHash3_x64_128 ( const void * key, const int len, + const uint32_t seed, void * out ) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 16; + int i; + + uint64_t h1 = seed; + uint64_t h2 = seed; + + uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); + uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); + + //---------- + // body + + const uint64_t * blocks = (const uint64_t *)(data); + + for(i = 0; i < nblocks; i++) + { + uint64_t k1 = getblock(blocks,i*2+0); + uint64_t k2 = getblock(blocks,i*2+1); + + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; + + h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; + + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; + + h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*16); + + uint64_t k1 = 0; + uint64_t k2 = 0; + + switch(len & 15) + { + case 15: k2 ^= (uint64_t)(tail[14]) << 48; + case 14: k2 ^= (uint64_t)(tail[13]) << 40; + case 13: k2 ^= (uint64_t)(tail[12]) << 32; + case 12: k2 ^= (uint64_t)(tail[11]) << 24; + case 11: k2 ^= (uint64_t)(tail[10]) << 16; + case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; + case 9: k2 ^= (uint64_t)(tail[ 8]) << 0; + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; + + case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; + case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; + case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; + case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; + case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; + case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; + case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; + case 1: k1 ^= (uint64_t)(tail[ 0]) << 0; + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; h2 ^= len; + + h1 += h2; + h2 += h1; + + h1 = fmix64(h1); + h2 = fmix64(h2); + + h1 += h2; + h2 += h1; + + ((uint64_t*)out)[0] = h1; + ((uint64_t*)out)[1] = h2; +} + +//----------------------------------------------------------------------------- + diff --git a/tests/make_png/murmur3.h b/tests/make_png/murmur3.h new file mode 100644 index 000000000..90878b00d --- /dev/null +++ b/tests/make_png/murmur3.h @@ -0,0 +1,24 @@ +#ifndef _MURMURHASH3_H_ +#define _MURMURHASH3_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +//----------------------------------------------------------------------------- + +void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out); + +void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out); + +void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out); + +//----------------------------------------------------------------------------- + +#ifdef __cplusplus +} +#endif + +#endif // _MURMURHASH3_H_ -- cgit v1.2.1