aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bitmap.c98
-rw-r--r--src/bitmap.h66
-rw-r--r--src/color.c1
-rw-r--r--src/main.c15
4 files changed, 123 insertions, 57 deletions
diff --git a/src/bitmap.c b/src/bitmap.c
index a987e25..c64a4c0 100644
--- a/src/bitmap.c
+++ b/src/bitmap.c
@@ -1,6 +1,20 @@
+/*
+ * src/bitmap.c
+ * (c) 2020 Jonas Gunz <himself@jonasgunz.de>
+ * License: MIT
+*/
+
#include "bitmap.h"
-uint32_t bitmap_flip_byte(unsigned char* _v, int _c)
+static struct bitmap_file_header bitmap_read_file_header(FILE *_file);
+
+static struct bitmap_image bitmap_read_pixel_data(FILE *_file, struct bitmap_file_header _header);
+
+static uint32_t bitmap_flip_byte(unsigned char* _v, int _c);
+
+static uint8_t bitmap_rgb_luminance(uint8_t R, uint8_t G, uint8_t B);
+
+static uint32_t bitmap_flip_byte(unsigned char* _v, int _c)
{
uint32_t ret = 0;
uint32_t counter = (_c-1) * 8;
@@ -14,45 +28,43 @@ uint32_t bitmap_flip_byte(unsigned char* _v, int _c)
return ret;
}//flip
-struct bitmap_pixel_data bitmap_read(char *_file)
+int bitmap_read(char *_file, struct bitmap_image *_bitmap)
{
- struct bitmap_pixel_data ret;
- struct bitmap_file_header header;
+ if (!_bitmap )
+ return 5;
- ret.R = ret.G = ret.B = NULL;
- ret.error = 1;
+ struct bitmap_file_header header;
+ _bitmap->tags = 0x00;
- FILE *bitmap;
+ FILE *input_file;
if(_file)
- bitmap = fopen(_file,"rb");
+ input_file = fopen(_file,"rb");
else
- bitmap = stdin;
+ input_file = stdin;
- if(!bitmap)
- return ret;
+ if(!input_file)
+ return 1;
- header = bitmap_read_file_header(bitmap);
+ header = bitmap_read_file_header(input_file);
if(header.error)
- return ret;
+ return 2;
if(header.biBitCount != 24)
- return ret;
+ return 3;
if(header.biCompression != 0)
- return ret;
-
- ret = bitmap_read_pixel_data(bitmap, header);
+ return 4;
+ *_bitmap = bitmap_read_pixel_data(input_file, header);
free(header.tables);
- fclose(bitmap);
+ fclose(input_file);
- ret.error = 0;
- return ret;
+ return 0;
}
-struct bitmap_file_header bitmap_read_file_header(FILE *_file)
+static struct bitmap_file_header bitmap_read_file_header(FILE *_file)
{
struct bitmap_file_header ret;
unsigned char fileheader[_HEADER_SIZE];
@@ -95,11 +107,11 @@ struct bitmap_file_header bitmap_read_file_header(FILE *_file)
return ret;
}
-struct bitmap_pixel_data bitmap_read_pixel_data(FILE *_file, struct bitmap_file_header _header)
+static struct bitmap_image bitmap_read_pixel_data(FILE *_file, struct bitmap_file_header _header)
{
uint32_t **bitmap_buff;
- struct bitmap_pixel_data ret;
+ struct bitmap_image ret;
uint32_t row_size = _header.biWidth * 3;
while(row_size%4)
@@ -162,3 +174,43 @@ struct bitmap_pixel_data bitmap_read_pixel_data(FILE *_file, struct bitmap_file_
return ret;
}
+
+int bitmap_copy ( struct bitmap_image *_input, struct bitmap_image *_output ) {
+ return 1;
+}
+
+int bitmap_convert_monochrome ( struct bitmap_image *_input, struct bitmap_image *_output ) {
+ if ( !_input || !_output )
+ return 1;
+
+ uint8_t **monochrome_bitmap = malloc( sizeof (*monochrome_bitmap) * _input->x );
+ for ( int i = 0; i < _input->y; i++ ) {
+ monochrome_bitmap[i] = malloc ( sizeof (**monochrome_bitmap) * _input->y );
+ }
+
+ for ( unsigned int x = 0; x < _input->x; x++ ) {
+ for ( unsigned int y = 0; y < _input->y; y++ ) {
+ monochrome_bitmap[x][y] = bitmap_rgb_luminance (
+ _input->R[x][y],
+ _input->G[x][y],
+ _input->B[x][y] );
+ }
+ }
+
+ _output->R = _output->G = _output->B = monochrome_bitmap;
+
+ return 0;
+}
+
+int bitmap_transform ( struct bitmap_image *_input, struct bitmap_image *_output ) {
+ return 1;
+}
+
+static uint8_t bitmap_rgb_luminance(uint8_t R, uint8_t G, uint8_t B)
+{
+ uint8_t ret;
+
+ ret = sqrt( 0.299*pow(R,2) + 0.587*pow(G,2) + 0.114*pow(B,2) ); //(char)(R+R+B+G+G+G)/6;
+
+ return ret;
+}
diff --git a/src/bitmap.h b/src/bitmap.h
index d222ffb..3b942a7 100644
--- a/src/bitmap.h
+++ b/src/bitmap.h
@@ -1,9 +1,16 @@
+/*
+ * src/bitmap.h
+ * (c) 2020 Jonas Gunz <himself@jonasgunz.de>
+ * License: MIT
+*/
+
#ifndef _BITMAP_H_
#define _BITMAP_H_
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
#define _HEADER_SIZE 54 //Fileheader + infoheader
#define IDENTIFIER 0x424d //BM BitMap identifier
@@ -26,45 +33,46 @@
#define G(x) (0x00ff00 & x) >> 8
#define B(x) (0x0000ff & x)
+#define BITMAP_MONOCHROME 0x01
+
struct bitmap_file_header
{
- uint8_t error;
-
- uint16_t bfType;
- uint32_t bfSize;
- uint32_t bfOffBits;
-
- uint32_t biSize;
- int32_t biWidth;
- int32_t biHeight;
- uint16_t biBitCount;
- uint32_t biCompression;
- uint32_t biSizeImage;
- uint32_t biClrUsed;
- uint32_t biClrImportant;
-
- unsigned char *tables;
- uint32_t tablesc;
+ uint8_t error;
+
+ uint16_t bfType;
+ uint32_t bfSize;
+ uint32_t bfOffBits;
+
+ uint32_t biSize;
+ int32_t biWidth;
+ int32_t biHeight;
+ uint16_t biBitCount;
+ uint32_t biCompression;
+ uint32_t biSizeImage;
+ uint32_t biClrUsed;
+ uint32_t biClrImportant;
+
+ unsigned char *tables;
+ uint32_t tablesc;
};
-struct bitmap_pixel_data
+struct bitmap_image
{
- unsigned int x,y;
- uint8_t **R;
- uint8_t **G;
- uint8_t **B;
+ unsigned int x,y;
- uint8_t error;
-};
+ uint8_t **R;
+ uint8_t **G;
+ uint8_t **B;
+ uint8_t tags;
+};
-uint32_t bitmap_flip_byte(unsigned char* _v, int _c);
-
-struct bitmap_pixel_data bitmap_read(char *_file);
+int bitmap_read ( char *_file, struct bitmap_image *_bitmap );
-struct bitmap_file_header bitmap_read_file_header(FILE *_file);
+int bitmap_copy ( struct bitmap_image *_input, struct bitmap_image *_output );
-struct bitmap_pixel_data bitmap_read_pixel_data(FILE *_file, struct bitmap_file_header _header);
+int bitmap_convert_monochrome ( struct bitmap_image *_input, struct bitmap_image *_output );
+int bitmap_transform ( struct bitmap_image *_input, struct bitmap_image *_output );
#endif /* end of include guard: _BITMAP_H_ */
diff --git a/src/color.c b/src/color.c
index f167016..d0fbe1b 100644
--- a/src/color.c
+++ b/src/color.c
@@ -63,6 +63,7 @@ char* calc_col(uint8_t R, uint8_t G, uint8_t B)
}
return colors[ nearest_num ].no;
}
+//TODO consolidate
char* calc_col_ansi(uint8_t R, uint8_t G, uint8_t B)
{
int num = 36 * (R/51) + 6 * (G/51) + (B/51);
diff --git a/src/main.c b/src/main.c
index 532b93a..e9e04e3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,3 +1,9 @@
+/*
+ * src/main.c
+ * (c) 2020 Jonas Gunz <himself@jonasgunz.de>
+ * License: MIT
+*/
+
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@@ -9,7 +15,7 @@
#include "m.h"
#include "color.h"
-/* #define CLEANUP 1 */
+// #define CLEANUP 1
#ifdef _DEBUG
#warning "Compiling with DEBUG"
@@ -50,10 +56,8 @@ int main(int argc, char *argv[])
uint8_t brightness_max = 0x00;
uint8_t brightness_min = 0xff;
- struct bitmap_pixel_data bitmap;
- bitmap = bitmap_read(args.filename);
-
- if(bitmap.error) {
+ struct bitmap_image bitmap;
+ if ( bitmap_read(args.filename, &bitmap) ) {
printf("Error reading file\n");
return 1;
}
@@ -172,6 +176,7 @@ int main(int argc, char *argv[])
DEBUG_PRINTF("Finished!\n");
#ifdef CLEANUP
+#warning "Cleanup included"
for(int i = 0; i < size_x; i++)
free (image_monochrome[i]);
free(image_monochrome);