Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianparvino committed Dec 10, 2017
2 parents 3ff25cf + 0518ae1 commit d78c9da
Show file tree
Hide file tree
Showing 23 changed files with 1,265 additions and 129 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(SOURCES
imagemanip.c
testimage.c)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lm -lpng -std=c11 -g -Wall -Werror=incompatible-pointer-types")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lpng -lm -std=c11 -g -Wall -Werror=incompatible-pointer-types")
# add_definitions(-lm -std=c11 -g -Wall -Werror=incompatible-pointer-types)
# This tells CMake to main.c and name it asciivn
add_executable(asciivn main.c ${SOURCES})
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions asciibuffer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "asciibuffer.h"

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Expand All @@ -20,7 +38,7 @@ struct asciibuffer *new_asciibuffer(size_t width, size_t height)

void flatten(struct asciibuffer *asciibuffer)
{
for (int i = 0; i < asciibuffer->height * asciibuffer->width; ++i)
for (size_t i = 0; i < asciibuffer->height * asciibuffer->width; ++i)
if (asciibuffer->buffer[i] == '\0')
asciibuffer->buffer[i] = ' ';
}
Expand All @@ -32,6 +50,6 @@ void clear(struct asciibuffer *asciibuffer)

void show_asciibuffer(struct asciibuffer *asciibuffer)
{
for (int i = 0; i < asciibuffer->height; ++i)
for (size_t i = 0; i < asciibuffer->height; ++i)
printf("%.*s\n", (int) asciibuffer->width, asciibuffer->buffer + i*asciibuffer->width);
}
17 changes: 17 additions & 0 deletions asciibuffer.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ASCII_BUFFER_H
#define ASCII_BUFFER_H

Expand Down
17 changes: 17 additions & 0 deletions asciibuffercharset.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "asciibuffercharset.h"


17 changes: 17 additions & 0 deletions asciibuffercharset.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ASCII_BUFFER_CHARSET_H
#define ASCII_BUFFER_CHARSET_H

Expand Down
73 changes: 54 additions & 19 deletions asciibufferfill.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
Expand All @@ -6,56 +24,73 @@
#include "imagemanip.h"
#include "charset.h"

#define LENGTH(x) (sizeof(x) / sizeof(*x))

struct cache {
float value;
char character;
};

int cmp_cache(const void *x, const void *y)
{
return ceil(((const struct cache*)x)->value - ((const struct cache*)y)->value);
}

void render_fill(struct asciibuffer *dest,
struct imagebuffer *src,
char fontname[])
{
size_t chardescs = 0;
struct charset *font_charset;
struct charset *font_charset = NULL;

if (!strcmp(fontname, ""))
{
fprintf(stderr, "Warning: no fontname provided. Using fallback.\n");
font_charset = generate_test_charset(&chardescs);

font_charset = generate_test_charset();
}

scale_bilinear((struct imagebuffer *)dest, src);

float cache_value[chardescs];
char cache_character[chardescs];
struct cache cache[font_charset->n];
memset(cache, 0, sizeof(cache));

memset(cache_value, 0, sizeof(*cache_value)*chardescs);
memset(cache_character, 0, chardescs);
// float cache_value[font_charset->n];
// char cache_character[font_charset->n];

// TODO: Use a sorted array
for (size_t character = 0; character < chardescs; ++character)
// memset(cache_value, 0, sizeof(*cache_value)*font_charset->n);
// memset(cache_character, 0, font_charset->n);

for (size_t character = 0; character < font_charset->n; ++character)
{
for (size_t i = 0; i < font_charset->width * font_charset->height; ++i)
{
float dx = font_charset->characters[character].glyph[i] - cache_value[character];
cache_value[character] += dx/(i + 1);
float dx = font_charset->characters[character].glyph[i] - cache[character].value;
cache[character].value += dx/(i + 1);
}

cache_character[character] = font_charset->characters[character].character;
cache[character].character = font_charset->characters[character].character;
}

qsort(cache, LENGTH(cache), sizeof(*cache), cmp_cache);

for (size_t i = 0; i < dest->height * dest->width; ++i)
{
float best_value = 1.0/0;
float best_value = 0;
char best_character = 0;

for (size_t j = 0; j < chardescs; ++j)
for (size_t j = 0; j < LENGTH(cache); ++j)
{
float current_value = fabs(((unsigned char*)(dest->buffer))[i] - cache_value[j]);
if (current_value < best_value)
if (dest->buffer[i] < cache[j].value)
{
best_value = current_value;
best_character = cache_character[j];
break;
}

best_value = cache[j].value;
best_character = cache[j].character;
}

dest->buffer[i] = best_character;
}

free_charset(font_charset, chardescs);
free_charset(font_charset);
}
17 changes: 17 additions & 0 deletions asciibufferfill.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ASCII_BUFFER_FILL_H
#define ASCII_BUFFER_FILL_H

Expand Down
Loading

0 comments on commit d78c9da

Please sign in to comment.