diff --git a/cs677/pa3/BMP.cc b/cs677/pa3/BMP.cc new file mode 100644 index 0000000..8bdd860 --- /dev/null +++ b/cs677/pa3/BMP.cc @@ -0,0 +1,99 @@ +// BMP.cc +// extracts/inserts data from/into .bmp file +// Adapted by Josh Holtrop from the original access_bmp.c by gw +// Supports reading and creating 24-bit color BMP images + +#include "BMP.h" +#include +#include +using namespace std; + +BMP::BMP(char * fileName) +{ + open(fileName); +} + +BMP::BMP(char * fileName, int width, int height, unsigned char * data) +{ + create(fileName, width, height, data); +} + +BMP::~BMP() +{ + if (m_fp != NULL) + close(); +} + +bool BMP::open(char * fileName) +{ + m_fp = fopen(fileName, "r+"); + if (m_fp != NULL) + { + // read file header + fread(&m_header, sizeof(m_header), 1, m_fp); + if (m_header.id[0] != 'B' || m_header.id[1] != 'M') + { + cerr << fileName << " does not appear to be a BMP file." << endl; + fclose(m_fp); + m_fp = NULL; + } + // read image information + fread(&m_info, sizeof(m_info), 1, m_fp); + } + return (m_fp != NULL); +} + +bool BMP::create(char * fileName, int width, int height, unsigned char * data) +{ + m_fp = fopen(fileName, "w+"); + if (m_fp != NULL) + { + /* Initialize the header structure */ + m_header.id[0] = 'B'; + m_header.id[1] = 'M'; + int row_padding = (3 * width) & 0x3; + if (row_padding) + row_padding = 4 - row_padding; + int row_bytes = (3 * width) + row_padding; + m_header.file_size = sizeof(m_header) + + sizeof(m_info) + + row_bytes * height; + m_header.reserved = 0; + m_header.offset = sizeof(m_header) + sizeof(m_info); + + /* Initialize the information structure */ + m_info.header_size = 40; + m_info.width = width; + m_info.height = height; + m_info.color_planes = 1; + m_info.color_depth = 24; + m_info.compression = 0; + m_info.image_size = 0; + m_info.xresolution = 2835; + m_info.yresolution = 2835; + m_info.num_colors = 0; + m_info.num_important_colors = 0; + + /* write them to the file */ + fwrite(&m_header, sizeof(m_header), 1, m_fp); + fwrite(&m_info, sizeof(m_info), 1, m_fp); + + unsigned int zero = 0; + unsigned char * data_ptr = data + (3 * width) * (height - 1); + for (int i = 0; i < height; i++) + { + fwrite(data_ptr, 3 * width, 1, m_fp); + if (row_padding) + fwrite(&zero, row_padding, 1, m_fp); + data_ptr -= 3 * width; + } + } + return (m_fp != NULL); +} + +void BMP::close() +{ + if (m_fp != NULL) + fclose(m_fp); + m_fp = NULL; +} diff --git a/cs677/pa3/BMP.h b/cs677/pa3/BMP.h new file mode 100644 index 0000000..842d0a6 --- /dev/null +++ b/cs677/pa3/BMP.h @@ -0,0 +1,48 @@ +// BMP.h +// extracts/inserts data from/into .bmp file +// Adapted by Josh Holtrop from the original access_bmp.c by gw +// Supports reading and creating 24-bit color BMP images + +#include + +class BMP +{ +public: + typedef struct + { + char id[2]; + int file_size; + int reserved; + int offset; + } __attribute__ ((packed)) header_t; + + typedef struct + { + int header_size; + int width; + int height; + unsigned short int color_planes; + unsigned short int color_depth; + unsigned int compression; + int image_size; + int xresolution; + int yresolution; + int num_colors; + int num_important_colors; + } __attribute__ ((packed)) info_t; + + BMP(char * fileName); + BMP(char * fileName, int width, int height, unsigned char * data); + ~BMP(); + int getWidth(); + int getHeight(); + +private: + FILE * m_fp; + header_t m_header; + info_t m_info; + + bool open(char * fileName); + bool create(char * fileName, int width, int height, unsigned char * data); + void close(); +}; diff --git a/cs677/pa3/Makefile b/cs677/pa3/Makefile new file mode 100644 index 0000000..fb393f0 --- /dev/null +++ b/cs677/pa3/Makefile @@ -0,0 +1,16 @@ + +TARGETS := edge-detect + +CXXFLAGS := -fopenmp +COMMON_OBJS := BMP.o + +OBJS := $(foreach target,$(TARGETS),$(target).o) +OBJS += $(COMMON_OBJS) + +all: $(TARGETS) + +$(TARGETS): $(OBJS) + $(CXX) -o $@ $@.o $(COMMON_OBJS) $(CXXFLAGS) + +clean: + -rm -f *~ *.o $(TARGETS) diff --git a/cs677/pa3/edge-detect.cc b/cs677/pa3/edge-detect.cc new file mode 100644 index 0000000..8b5cb41 --- /dev/null +++ b/cs677/pa3/edge-detect.cc @@ -0,0 +1,9 @@ + +#include +#include "BMP.h" +using namespace std; + +int main(int argc, char * argv[]) +{ + cout << "edge-detect" << endl; +}