Convert bitmap into c heafer file.
This commit is contained in:
commit
62c4b97150
137
Makefile
Normal file
137
Makefile
Normal file
@ -0,0 +1,137 @@
|
||||
CROSS_COMPILE ?=
|
||||
|
||||
TARGET_FILE ?= bmp2h
|
||||
|
||||
CC = $(CROSS_COMPILE)gcc
|
||||
CPP = $(CROSS_COMPILE)cpp
|
||||
CHECK = cppcheck
|
||||
LCOV = lcov
|
||||
GENHTML = genhtml
|
||||
|
||||
SRC_DIR = src
|
||||
OBJ_DIR = obj
|
||||
BIN_DIR = bin
|
||||
COVERAGE_DIR = coverage
|
||||
UNIT_TEST_SRC_DIR = test/unit
|
||||
UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR)
|
||||
|
||||
PREFIX ?= /usr
|
||||
BIN_INSTALL_DIR = $(PREFIX)/bin
|
||||
|
||||
INCLUDES := inc
|
||||
INCLUDES += $(PREFIX)/include/
|
||||
INCLUDES += $(EXTRA_INC)
|
||||
|
||||
LIBS =
|
||||
|
||||
LD_FLAGS += $(addprefix -L,$(EXTRA_LIB_DIR))
|
||||
|
||||
LD_FLAGS += -lm
|
||||
|
||||
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
|
||||
INCLUDES += test/inc
|
||||
C_FLAGS += --coverage
|
||||
LD_FLAGS += --coverage
|
||||
endif
|
||||
|
||||
C_FLAGS += -fpic
|
||||
|
||||
C_FLAGS += -O0 -g -Wall
|
||||
CPP_FLAGS += $(addprefix -I, $(INCLUDES))
|
||||
|
||||
CHECK_FLAGS = $(addprefix -I,$(filter-out $(PREFIX)/include/,$(INCLUDES)))
|
||||
CHECK_FLAGS += --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr --force
|
||||
|
||||
C_SRCS = $(wildcard $(SRC_DIR)/*.c)
|
||||
C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS)))
|
||||
|
||||
UNIT_TEST_SRCS = $(wildcard $(UNIT_TEST_SRC_DIR)/*.c) $(filter-out %main.c,$(CC_SRCS))
|
||||
UNIT_TEST_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst $(UNIT_TEST_SRC_DIR)%,$(UNIT_TEST_OBJ_DIR)%,$(patsubst %.c,%.o,$(UNIT_TEST_SRCS))))
|
||||
|
||||
OBJS = $(C_OBJS)
|
||||
|
||||
UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
|
||||
TARGET = $(BIN_DIR)/$(TARGET_FILE)
|
||||
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
|
||||
|
||||
.PHONY: all install uninstall clean distclean
|
||||
all: $(TARGET)
|
||||
|
||||
install: all
|
||||
install -d $(BIN_INSTALL_DIR)
|
||||
install -m 0666 $(TARGET) $(BIN_INSTALL_DIR)
|
||||
|
||||
uninstall:
|
||||
rm -f $(BIN_INSTALL_DIR)/$(TARGET_FILE)
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
rm -f $(OBJS) $(patsubst %.o,%.d,$(OBJS))
|
||||
rm -f $(UNIT_TEST_OBJS) $(patsubst %.o,%.d,$(UNIT_TEST_OBJS)) $(UNIT_TEST_TARGET)
|
||||
rm -f $(UNIT_TEST_OBJ_DIR)/*.gcda
|
||||
rm -f $(UNIT_TEST_OBJ_DIR)/*.gcno
|
||||
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
|
||||
rm -fr $(COVERAGE_DIR)
|
||||
|
||||
distclean:
|
||||
rm -rf $(OBJ_DIR) $(BIN_DIR) $(COVERAGE_DIR)
|
||||
|
||||
.PHONY: coverage
|
||||
coverage: _cov_genhtml
|
||||
$(eval COVERAGE:=$(shell grep 'lines' .coverage.tmp | egrep -o '[0-9]+.[0-9]+%'))
|
||||
@echo
|
||||
@echo Line coverage: $(COVERAGE)
|
||||
@echo
|
||||
@rm -f .coverage.tmp
|
||||
|
||||
.PHONY: _cov_lcov
|
||||
_cov_lcov: exec_unit_test
|
||||
@rm -f $(UNIT_TEST_OBJ_DIR)/*.gcda
|
||||
@rm -f $(UNIT_TEST_OBJ_DIR)/*.gcno
|
||||
@mkdir -p $(COVERAGE_DIR)
|
||||
$(LCOV) -c -d $(OBJ_DIR) -o $(COVERAGE_DIR)/coverage.info
|
||||
|
||||
.PHONY: _cov_genhtml
|
||||
_cov_genhtml: _cov_lcov
|
||||
$(GENHTML) -o $(COVERAGE_DIR)/html $(COVERAGE_DIR)/coverage.info > .coverage.tmp
|
||||
|
||||
.PHONY: unit_test
|
||||
build_unit_test: $(UNIT_TEST_TARGET)
|
||||
|
||||
.PHONY: unit_test
|
||||
exec_unit_test: $(UNIT_TEST_TARGET)
|
||||
$(UNIT_TEST_TARGET)
|
||||
|
||||
.PHONY: check
|
||||
check: $(CC_SRCS)
|
||||
$(CHECK) $(CHECK_FLAGS) $(CC_SRCS)
|
||||
|
||||
$(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE)
|
||||
@mkdir -p $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)
|
||||
$(CC) $(CC_FLAGS) $(LD_FLAGS) $(UNIT_TEST_OBJS) -o $@
|
||||
|
||||
$(TARGET): $(OBJS) $(THIS_MAKEFILE)
|
||||
@mkdir -p $(BIN_DIR)
|
||||
$(CC) $(CC_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
|
||||
|
||||
.PRECIOUS: $(OBJ_DIR)/%.d
|
||||
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
$(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(OBJ_DIR)/%.d,$(SRC_DIR)/%.c,$@)
|
||||
|
||||
.PRECIOUS: $(UNIT_TEST_OBJ_DIR)/%.d
|
||||
$(UNIT_TEST_OBJ_DIR)/%.d: $(UNIT_TEST_SRC_DIR)/%.c
|
||||
@mkdir -p $(UNIT_TEST_OBJ_DIR)
|
||||
$(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(UNIT_TEST_OBJ_DIR)/%.d,$(UNIT_TEST_SRC_DIR)/%.c,$@)
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
|
||||
|
||||
$(UNIT_TEST_OBJ_DIR)/%.o: $(UNIT_TEST_SRC_DIR)/%.c $(UNIT_TEST_OBJ_DIR)/%.d
|
||||
@mkdir -p $(UNIT_TEST_OBJ_DIR)
|
||||
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(patsubst %.o,%.d,$(OBJS))
|
||||
endif
|
116
src/main.c
Normal file
116
src/main.c
Normal file
@ -0,0 +1,116 @@
|
||||
#include <stdio.h> //fseek fread
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h> //memset
|
||||
|
||||
/****************************** Bitmap standard information*************************************/
|
||||
/*Bitmap file header 14bit*/
|
||||
typedef struct BMP_FILE_HEADER {
|
||||
uint16_t bType; //File identifier
|
||||
uint32_t bSize; //The size of the file
|
||||
uint16_t bReserved1; //Reserved value, must be set to 0
|
||||
uint16_t bReserved2; //Reserved value, must be set to 0
|
||||
uint32_t bOffset; //The offset from the beginning of the file header to the beginning of the image data bit
|
||||
} __attribute__ ((packed)) BMPFILEHEADER; // 14bit
|
||||
|
||||
/*Bitmap information header 40bit*/
|
||||
typedef struct BMP_INFO {
|
||||
uint32_t bInfoSize; //The size of the header
|
||||
uint32_t bWidth; //The width of the image
|
||||
uint32_t bHeight; //The height of the image
|
||||
uint16_t bPlanes; //The number of planes in the image
|
||||
uint16_t bBitCount; //The number of bits per pixel
|
||||
uint32_t bCompression; //Compression type
|
||||
uint32_t bmpImageSize; //The size of the image, in bytes
|
||||
uint32_t bXPelsPerMeter; //Horizontal resolution
|
||||
uint32_t bYPelsPerMeter; //Vertical resolution
|
||||
uint32_t bClrUsed; //The number of colors used
|
||||
uint32_t bClrImportant; //The number of important colors
|
||||
} __attribute__ ((packed)) BMPINF;
|
||||
|
||||
/*Color table: palette */
|
||||
typedef struct RGB_QUAD {
|
||||
uint8_t rgbBlue; //Blue intensity
|
||||
uint8_t rgbGreen; //Green strength
|
||||
uint8_t rgbRed; //Red intensity
|
||||
uint8_t rgbReversed; //Reserved value
|
||||
} __attribute__ ((packed)) RGBQUAD;
|
||||
|
||||
|
||||
#define RGB(r,g,b) (((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3))
|
||||
|
||||
static void Paint_SetPixel(uint16_t Xpoint, uint16_t Ypoint, uint16_t Color);
|
||||
static uint16_t image[240 * 240];
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
FILE *fp; //Define a file pointer
|
||||
BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
|
||||
BMPINF bmpInfoHeader; //Define a bmp bitmap header structure
|
||||
int opt;
|
||||
char *infile, *outfile;
|
||||
|
||||
while((opt = getopt(argc, argv, "i:o:")) != -1) {
|
||||
if(opt == 'i') {
|
||||
infile = optarg;
|
||||
} else if(opt == 'o') {
|
||||
outfile = optarg;
|
||||
}
|
||||
}
|
||||
|
||||
// Binary file open
|
||||
if((fp = fopen(infile, "rb")) == NULL) { // fp = 0x00426aa0
|
||||
printf("Cann't open the file!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set the file pointer from the beginning
|
||||
fseek(fp, 0, SEEK_SET); // fp = 0x00426aa0
|
||||
fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp);// sizeof(BMPFILEHEADER) must be 14,
|
||||
fread(&bmpInfoHeader, sizeof(BMPINF), 1, fp);
|
||||
|
||||
int row, col;
|
||||
short data;
|
||||
RGBQUAD rgb;
|
||||
int len = bmpInfoHeader.bBitCount / 8; //RGB888,one 3 byte = 1 bitbmp
|
||||
|
||||
// get bmp data and show
|
||||
fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
|
||||
for(row = 0; row < bmpInfoHeader.bHeight; row++) {
|
||||
for(col = 0; col < bmpInfoHeader.bWidth; col++) {
|
||||
if(fread((char *)&rgb, 1, len, fp) != len){
|
||||
perror("get bmpdata:\r\n");
|
||||
break;
|
||||
}
|
||||
data = RGB((rgb.rgbRed), (rgb.rgbGreen), (rgb.rgbBlue));
|
||||
Paint_SetPixel(col, bmpInfoHeader.bHeight - row - 1, data);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
if((fp = fopen(outfile, "w")) == NULL) {
|
||||
printf("Cann't open the file!\n");
|
||||
return 0;
|
||||
}
|
||||
fprintf(fp, "#ifndef __BITMAP_H__\n");
|
||||
fprintf(fp, "#define __BITMAP_H__\n\n");
|
||||
fprintf(fp, "uint16_t bitmap[240 * 240] = {\n");
|
||||
for(uint32_t i = 1; i < 240 * 240; i++) {
|
||||
fprintf(fp, "0x%04x,", image[i - 1]);
|
||||
if(i % 16 == 0) {
|
||||
fprintf(fp, "\n");
|
||||
}
|
||||
}
|
||||
fprintf(fp, "0x%04x};\n\n", image[240 * 240 - 1]);
|
||||
fprintf(fp, "#endif\n\n");
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void Paint_SetPixel(uint16_t X, uint16_t Y, uint16_t Color)
|
||||
{
|
||||
Color = ((Color <<8 ) & 0xff00) | (Color >> 8);
|
||||
image[X + Y * 240] = Color;
|
||||
}
|
Loading…
Reference in New Issue
Block a user