Remove dependency between Make_PNG and Generate_PNG

This commit is contained in:
Kushal K S V S 2017-07-12 02:12:55 +05:30
parent fd3ff32ecc
commit 1d4fbc5e29
3 changed files with 107 additions and 147 deletions

View File

@ -140,6 +140,8 @@ int Generate_PNG (IMAGE *bitmap,
}
png_free (png_ptr, row_pointers);
free (bitmap->pixels);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
@ -149,9 +151,8 @@ int Generate_PNG (IMAGE *bitmap,
return status;
}
void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
void Make_PNG(FT_Bitmap* bitmap,IMAGE* fruit,int i,int render_mode){
IMAGE fruit;
int x;
int y;
@ -160,16 +161,16 @@ void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
switch(render_mode){
case 0 : fruit.width = bitmap->width; // MONO and GRAY
fruit.height = bitmap->rows;
case 0 : fruit->width = bitmap->width; // MONO and GRAY
fruit->height = bitmap->rows;
fruit.pixels = calloc ( fruit.width * fruit.height,
fruit->pixels = calloc ( fruit->width * fruit->height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
for (y = 0; y < fruit->height; y++) {
for (x = 0; x < fruit->width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
PIXEL * pixel = Pixel_At ( fruit, x, y);
p = (y * bitmap->pitch ) + x;
value = bitmap->buffer[p];
@ -187,16 +188,16 @@ void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
}
}
break;
case 1 : fruit.width = bitmap->width; // MONO and GRAY
fruit.height = bitmap->rows;
case 1 : fruit->width = bitmap->width; // MONO and GRAY
fruit->height = bitmap->rows;
fruit.pixels = calloc ( fruit.width * fruit.height,
fruit->pixels = calloc ( fruit->width * fruit->height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
for (y = 0; y < fruit->height; y++) {
for (x = 0; x < fruit->width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
PIXEL * pixel = Pixel_At ( fruit, x, y);
p = (y * bitmap->pitch ) + x;
value = bitmap->buffer[p];
@ -210,16 +211,16 @@ void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
break;
case 2 :
case 3 : fruit.width = bitmap->width / 3; // LCD
fruit.height = bitmap->rows;
case 3 : fruit->width = bitmap->width / 3; // LCD
fruit->height = bitmap->rows;
fruit.pixels = calloc ( fruit.width * fruit.height,
fruit->pixels = calloc ( fruit->width * fruit->height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
for (y = 0; y < fruit->height; y++) {
for (x = 0; x < fruit->width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
PIXEL * pixel = Pixel_At ( fruit, x, y);
p = (y * bitmap->pitch ) + (x)*3;
value = bitmap->buffer[p];
@ -239,16 +240,16 @@ void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
break;
case 4 :
case 5 : fruit.width = bitmap->width; // LCD_V
fruit.height = bitmap->rows / 3;
case 5 : fruit->width = bitmap->width; // LCD_V
fruit->height = bitmap->rows / 3;
fruit.pixels = calloc ( fruit.width * fruit.height,
fruit->pixels = calloc ( fruit->width * fruit->height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
for (y = 0; y < fruit->height; y++) {
for (x = 0; x < fruit->width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
PIXEL * pixel = Pixel_At ( fruit, x, y);
p = ((y*3) * bitmap->pitch ) + x;
value = bitmap->buffer[p];
@ -267,21 +268,13 @@ void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
}
break;
default : fruit.width = bitmap->width;
fruit.height = bitmap->rows;
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);
}
void Read_PNG(char *filename, IMAGE * bitmap) {
void Read_PNG(char *filename, IMAGE * after_effect) {
int width, height;
png_bytep *row_pointers;
@ -305,8 +298,8 @@ void Read_PNG(char *filename, IMAGE * bitmap) {
width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
bitmap->width = width;
bitmap->height = height;
after_effect->width = width;
after_effect->height = height;
row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
for(int y = 0; y < height; y++) {
@ -315,7 +308,7 @@ void Read_PNG(char *filename, IMAGE * bitmap) {
png_read_image(png, row_pointers);
bitmap->pixels = (PIXEL*)malloc(width * height * sizeof(PIXEL));
after_effect->pixels = (PIXEL*)malloc(width * height * sizeof(PIXEL));
for(int y = 0; y < height; y++) {
@ -325,7 +318,7 @@ void Read_PNG(char *filename, IMAGE * bitmap) {
png_bytep px = &(row[x * 4]);
PIXEL * pixel = Pixel_At ( bitmap, x, y);
PIXEL * pixel = Pixel_At ( after_effect, x, y);
pixel->red = px[0];
pixel->green = px[1];
@ -341,8 +334,6 @@ void Add_effect_1(IMAGE* base, IMAGE* test, IMAGE* out){
out->pixels = (PIXEL*)malloc(base->width * base->height * sizeof(PIXEL));
PIXEL temp;
for(int y = 0; y < base->height; y++) {
for(int x = 0; x < base->width; x++ ) {
@ -379,39 +370,3 @@ void Add_effect_1(IMAGE* base, IMAGE* test, IMAGE* out){
}
}
}
void Stitch(IMAGE* left, IMAGE* right, IMAGE* result){
result->width = left->width + right->width;
result->height = left->height; // Horizontal clipping
result->pixels = (PIXEL*)malloc(result->width * result->height * sizeof(PIXEL));
for (int y = 0; y < left->height; ++y)
{
for (int x = 0; x < left->width; ++x)
{
PIXEL * pixel_left = Pixel_At ( left, x, y);
PIXEL * pixel_result = Pixel_At ( result, x, y);
pixel_result->red = pixel_left->red;
pixel_result->green = pixel_left->green;
pixel_result->blue = pixel_left->blue;
pixel_result->alpha = pixel_left->alpha;
}
}
for (int y = 0; y < right->height; ++y)
{
for (int x = left->width; x < result->width; ++x)
{
PIXEL * pixel_right = Pixel_At ( right, x - left->width, y);
PIXEL * pixel_result = Pixel_At ( result, x, y);
pixel_result->red = pixel_right->red;
pixel_result->green = pixel_right->green;
pixel_result->blue = pixel_right->blue;
pixel_result->alpha = pixel_right->alpha;
}
}
}

View File

@ -61,14 +61,12 @@ 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);
void Make_PNG (FT_Bitmap* bitmap,IMAGE* fruit, int i,int render_mode);
// Image to file
int Generate_PNG (IMAGE *bitmap, const char *path,int render_mode);
int Generate_PNG (IMAGE *bitmap, const char *path,int render_mode);
// Read PNG
void Read_PNG(char *filename, IMAGE * after_effect);
// Add an effect using two PNG images
// Base Glyph = Gray {127,0,0,255}
// Differences = Red {255,0,0,255}
void Add_effect_1(IMAGE* base, IMAGE* test, IMAGE* out);
// Stitch two PNG files side by side
void Stitch(IMAGE* left, IMAGE* right, IMAGE* result);

View File

@ -2,7 +2,7 @@
int main (int argc, char const *argv[])
{
FT_Library library;
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
@ -11,22 +11,22 @@ int main (int argc, char const *argv[])
FT_Error error;
const char* font_file;
int size;
int render_mode; // argument
int size;
int render_mode; // argument
int load_flag; // FT_LOAD_XXX
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
int target_flag; // FT_LOAD_TARGET_XXX
char* render_type; // for file_name
char name[100]; // hashes file name
int i; // for loop
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");
$(font)_$(pt_size)_$(render_mode)_$(glyph_index).png \n\n");
printf("Default: hashes of 256-level gray bitmaps will be generated\n\n");
@ -42,8 +42,8 @@ int main (int argc, char const *argv[])
return 1;
}
font_file = argv[1];
size = atof(argv[2]);
font_file = argv[1];
size = atof(argv[2]);
render_mode = atoi(argv[3]);
error = FT_Init_FreeType( &library );
@ -78,72 +78,72 @@ int main (int argc, char const *argv[])
}
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 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 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 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 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 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;
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";
default:render_flag = FT_RENDER_MODE_NORMAL;
load_flag = FT_LOAD_DEFAULT;
target_flag = FT_LOAD_TARGET_NORMAL;
render_type = "gray";
}
slot = face->glyph;
slot = face->glyph;
sprintf(name,"./images/%s_%d_%s", font_file,
size,
render_type);
sprintf(name,"./images/%s_%d_%s", font_file,
size,
render_type);
for (i = 0; i <face->num_glyphs; ++i)
{
error = FT_Load_Glyph( face,
for (i = 0; i <face->num_glyphs; ++i)
{
error = FT_Load_Glyph( face,
i,
load_flag | target_flag);
if(error){
if(error){
printf("Error loading glyph\n");
}
}
FT_Render_Glyph( slot,
render_flag);
if(error){
FT_Render_Glyph( slot,
render_flag);
if(error){
printf("Error rendering the glyph\n");
}
}
bitmap = &slot->bitmap;
bitmap = &slot->bitmap;
if (bitmap->width == 0 || bitmap->rows == 0)
if (bitmap->width == 0 || bitmap->rows == 0)
{
continue;
}
@ -160,17 +160,24 @@ int main (int argc, char const *argv[])
printf("Error converting the bitmap\n");
}
char * file_name = ( char * )calloc(150,sizeof(char));
sprintf(file_name, "%s_%d", name, i);
IMAGE fruit;
if (render_mode == 0)
{
Make_PNG(&target,name,i,render_mode);
Make_PNG(&target, &fruit,i,render_mode);
Generate_PNG (& fruit, file_name, render_mode);
}else{
Make_PNG(bitmap,name,i,render_mode);
Make_PNG(bitmap, &fruit,i,render_mode);
Generate_PNG ( &fruit, file_name, render_mode);
}
}
}
FT_Done_Face ( face );
FT_Done_Face ( face );
FT_Done_FreeType( library );
return 0;