aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c130
1 files changed, 98 insertions, 32 deletions
diff --git a/main.c b/main.c
index d353fbf..19ac6d6 100644
--- a/main.c
+++ b/main.c
@@ -19,15 +19,10 @@
#define BI_CLR_USED 0x2e
#define BI_CLR_IMPORTANT 0x32
-#define CHAR_SIZE 10 //How many pixels should form one ASCII char?
+#define CHAR_SIZE_X 20 //How many pixels should form one ASCII char?
+#define CHAR_SIZE_Y (2 * CHAR_SIZE_X)
-const char map[] = {' ', '.', ',', '-', ':', ';', '!', '/','?' , '%', '$', '#'};
-
-struct ascii_pixel
-{
- char i[3 * CHAR_SIZE][CHAR_SIZE]; //
- char c;
-};
+const char map[] = {' ', '.', ',', '-', '~', ':', ';', '!', '/','?' , '%', '$', '#'};
//Routine for flipping bytes
uint32_t flip(unsigned char* _v, int _c);
@@ -35,29 +30,33 @@ uint32_t flip(unsigned char* _v, int _c);
//Calculate average
char avg(int argc, char *argv);
+//Select Char based on 1B brightness Value
+char calc_char(uint8_t _c);
+
int main(int argc, char *argv[])
{
unsigned char fileheader[_HEADER_SIZE];
unsigned char *tables;
- uint32_t **bitmap_buff;
- uint32_t read_counter = 0;
-
- uint16_t bfType = 0;
- uint32_t bfSize = 0;
- uint32_t bfOffBits = 0;
-
- uint32_t biSize = 0;
- int32_t biWidth = 0;
- int32_t biHeight = 0;
- uint16_t biBitCount = 0;
- uint32_t biCompression = 0;
- uint32_t biSizeImage = 0;
- uint32_t biClrUsed = 0;
+ uint32_t **bitmap_buff;
+ char **ascii_buff;
+ uint32_t read_counter = 0;
+
+ uint16_t bfType = 0;
+ uint32_t bfSize = 0;
+ uint32_t bfOffBits = 0;
+
+ uint32_t biSize = 0;
+ int32_t biWidth = 0;
+ int32_t biHeight = 0;
+ uint16_t biBitCount = 0;
+ uint32_t biCompression = 0;
+ uint32_t biSizeImage = 0;
+ uint32_t biClrUsed = 0;
uint32_t biClrImportant = 0;
- if(argc < 2)
+ if(argc != 3)
{
- printf("No input file specified. Abort.\n");
+ printf("Usage: %s <input> <output>\n", argv[0]);
return 1;
}
@@ -121,11 +120,11 @@ int main(int argc, char *argv[])
uint32_t d = bfOffBits - read_counter;
tables = malloc(sizeof(char)* d);
fread(tables, sizeof(char), d, bitmap);
-
read_counter += d;
printf("Read to %x\n", read_counter);
- uint32_t row_size = biWidth * 3; //One pixel is 3Byte, One line is multiple of 4Bytes
+ //One pixel is 3Byte, One line is multiple of 4Bytes
+ uint32_t row_size = biWidth * 3;
while(row_size%4)
row_size++;
@@ -133,21 +132,82 @@ int main(int argc, char *argv[])
//If biHeight > 0 Data starts with last row!!
//Allocate 2D array
- bitmap_buff = malloc(sizeof(*bitmap_buff) * biWidth);
- for(int i = 0; i < biWidth; i++)
+ bitmap_buff = malloc(sizeof(*bitmap_buff) * biHeight);
+ for(int i = 0; i < biHeight; i++)
+ {
+ bitmap_buff[i] = malloc(sizeof(*bitmap_buff[i]) * biWidth);
+ }
+
+ //Copy Bitmap
+ for(int row = 0; row < biHeight; row++)
{
- bitmap_buff[i] = malloc(sizeof(*bitmap_buff[i]) * biHeight);
+ //printf("Row %i\n", row);
+ fread(bitmap_buff[row], sizeof(char), row_size, bitmap);
+ read_counter += row_size;
}
+ printf("Finished copying Bitmap\n");
- for(int i = 0; i < biWidth; i++)
- free(bitmap_buff[i]);
+ //Calculate Averages of CHAR_SIZE x CHAR_SIZE blocks
+ unsigned int size_x,size_y;
+ size_x = biWidth / CHAR_SIZE_X;
+ size_y = biHeight / CHAR_SIZE_Y;
+
+ printf("Creating ASCII File %u x %u\n", size_x, size_y);
+
+ ascii_buff = malloc(sizeof(*ascii_buff) * size_x);
+ for (int i = 0; i < size_x; i++)
+ {
+ ascii_buff[i] = malloc(sizeof(ascii_buff[i]) * size_y);
+ }
+
+ //Nest thine Lööps
+ for(int x = 0; x < size_x; x++)
+ {
+ for(int y = 0; y < size_y; y++)
+ {
+ char b = 0;
+
+
+ for(int row = y * CHAR_SIZE_Y; row < (y + 1) * CHAR_SIZE_Y; row ++)
+ {
+ //x * CHAR_SIZE_X
+ char d[2] = {avg(CHAR_SIZE_X, ascii_buff[y]), b};
+ b = avg(2, d);
+ }
+ ascii_buff[x][y] = calc_char(b);
+ }
+ }
+
+ //Write Output
+ printf("Opening %s for writing.\n", argv[2]);
+ FILE *out = fopen(argv[2], "w");
+ if(!out)
+ {
+ printf("Error opening output File. Check writing permissions.\n");
+ return 1;
+ }
+
+ for(int y = size_y; y >= 0; y--)
+ {
+ for(int x = 0; x < size_x; x++)
+ {
+ fputc(ascii_buff[x][y], out);
+ }
+ fputc('\n', out);
+ }
+
+ //Cleanup
+ for(int i = 0; i < biHeight; i++)
+ free (bitmap_buff[i]);
free(bitmap_buff);
fclose(bitmap);
+ fclose(out);
+
return 0;
}
-
+//One pixel is 3Byte, One line is multiple of 4Bytes
uint32_t flip(unsigned char* _v, int _c)
{
uint32_t ret = 0;
@@ -174,3 +234,9 @@ char avg(int argc, char *argv)
return ret;
}//flip
+
+char calc_char(uint8_t _c)
+{
+ float c = (float)_c / 255.0;
+ return map [(int)(sizeof(map) * c)];
+}