Skip to content

Commit

Permalink
Can load video frame
Browse files Browse the repository at this point in the history
  • Loading branch information
danvim committed Feb 21, 2019
1 parent 54cd0bf commit c2a271f
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 72 deletions.
137 changes: 97 additions & 40 deletions Bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
#include "bitmap.h"

BMP_BITMAPFILEHEADER bmfh;
BMP_BITMAPINFOHEADER bmih;
BMP_BITMAPINFOHEADER bmih;

DIB_COLOR_MAPPING bmp_color_mapping{ 2, 1, 0 };
DIB_COLOR_MAPPING cinepak_color_mapping{ 0, 2, 1 };

// Bitmap data returned is (R,G,B) tuples in row-major order.
unsigned char* readBMP(char* fname,
Expand Down Expand Up @@ -51,16 +54,10 @@ unsigned char* readBMP(char* fname,
fseek( file, pos, SEEK_SET );

width = bmih.biWidth;
height = bmih.biHeight;

int padWidth = width * 3;
int pad = 0;
if ( padWidth % 4 != 0 )
{
pad = 4 - (padWidth % 4);
padWidth += pad;
}
int bytes = height*padWidth;
height = bmih.biHeight;

const auto padding = calculatePadding(width);
const auto bytes = height * padding.padWidth;

unsigned char *data = new unsigned char [bytes];

Expand All @@ -73,34 +70,101 @@ unsigned char* readBMP(char* fname,

fclose( file );

// shuffle bitmap data such that it is (R,G,B) tuples in row-major order
int i, j;
j = 0;
unsigned char temp;
unsigned char* in;
unsigned char* out;
mapColor(data, width, height, padding.pad, bmp_color_mapping);

return data;
}

in = data;
out = data;
unsigned char* mapColor(unsigned char* data, const long int width, const long int height, const long int pad, const DIB_COLOR_MAPPING map)
{
auto in = data;
auto out = data;

for ( j = 0; j < height; ++j )
for (auto j = 0; j < height; ++j)
{
for ( i = 0; i < width; ++i )
for (auto i = 0; i < width; ++i)
{
out[1] = in[1];
temp = in[2];
out[2] = in[0];
out[0] = temp;
unsigned char colors[3] = { in[0], in[1], in[2] };
out[0] = colors[map.c0];
out[1] = colors[map.c1];
out[2] = colors[map.c2];

in += 3;
out += 3;
}
in += pad;
}

return data;
}


return data;
}

unsigned char* reverseMapColor(unsigned char* data, const long width, const long height, const DIB_COLOR_MAPPING map)
{
auto in = data;
auto out = data;
for (int j = 0; j < height; ++j)
{
for (int i = 0; i < width; ++i)
{
unsigned char colors[3] = { in[0], in[1], in[2] };
out[0] = colors[map.c0];
out[1] = colors[map.c1];
out[2] = colors[map.c2];

in += 3;
out += 3;
}
}

return data;
}

unsigned char* repackBmp(unsigned char* data, const long width, const long height, const long pad)
{
const auto padding = calculatePadding(width);
const auto tLength = height * padding.padWidth;

auto *t = new unsigned char[tLength];
memcpy(t, data, tLength);

auto in = t;
auto out = data;
for (auto j = 0; j < height; ++j)
{
for (auto i = 0; i < width; ++i)
{
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];

in += 3;
out += 3;
}
for(auto i = 0; i < pad; i++)
{
out[i] = 0;
}
out += pad;
}

delete[] t;

return data;
}

PADDING calculatePadding(long width)
{
int padWidth = width * 3;
int pad = 0;
if (padWidth % 4 != 0)
{
pad = 4 - (padWidth % 4);
padWidth += pad;
}

return {pad, padWidth};
}

void writeBMP(char* iname,
int width,
int height,
Expand Down Expand Up @@ -143,20 +207,13 @@ void writeBMP(char* iname,
fwrite(&bmih, sizeof(BMP_BITMAPINFOHEADER), 1, outFile);

bytes /= height;
unsigned char* scanline = new unsigned char [bytes];
for ( int j = 0; j < height; ++j )

reverseMapColor(data, width, height, bmp_color_mapping);
for (int j = 0; j < height; ++j)
{
memcpy( scanline, data + j*3*width, bytes );
for ( int i = 0; i < width; ++i )
{
unsigned char temp = scanline[i*3];
scanline[i*3] = scanline[i*3+2];
scanline[i*3+2] = temp;
}
fwrite( scanline, bytes, 1, outFile);
fwrite(data + j * 3 * width, bytes, 1, outFile);
}

delete [] scanline;

fclose(outFile);
}
24 changes: 22 additions & 2 deletions Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,30 @@ typedef struct {
BMP_LONG biYPelsPerMeter;
BMP_DWORD biClrUsed;
BMP_DWORD biClrImportant;
} BMP_BITMAPINFOHEADER;
} BMP_BITMAPINFOHEADER;

typedef struct
{
int pad;
int padWidth;
} PADDING;

typedef struct
{
unsigned int c0;
unsigned int c1;
unsigned int c2;
} DIB_COLOR_MAPPING;

// global I/O routines
extern unsigned char* readBMP( char* fname, int& width, int& height );
extern void writeBMP( char* iname, int width, int height, unsigned char* data );
extern unsigned char* mapColor(unsigned char* data, long int width, long int height, long int pad, DIB_COLOR_MAPPING map);
extern unsigned char* reverseMapColor(unsigned char* data, long int width, long int height, DIB_COLOR_MAPPING map);
extern unsigned char* repackBmp(unsigned char* data, long int width, long int height, long int pad);
extern PADDING calculatePadding(long width);
extern void writeBMP( char* iname, int width, int height, unsigned char* data );

extern DIB_COLOR_MAPPING bmp_color_mapping;
extern DIB_COLOR_MAPPING cinepak_color_mapping;

#endif
68 changes: 39 additions & 29 deletions ImpressionistDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,52 +161,62 @@ double ImpressionistDoc::getAlpha()
//---------------------------------------------------------
int ImpressionistDoc::loadImage(char *iname)
{
// try to open the image to read
unsigned char* data;
int width,
height;
int width, height;

const auto data = readBMP(iname, width, height);

return loadImageFromData(data, width, height);
}

if ( (data=readBMP(iname, width, height))==NULL )
int ImpressionistDoc::loadImageFromData(unsigned char* dataPtr, const int width, const int height)
{
if (dataPtr == nullptr)
{
fl_alert("Can't load bitmap file");
return 0;
}

// reflect the fact of loading the new image
m_nWidth = width;
m_nPaintWidth = width;
m_nHeight = height;
m_nPaintHeight = height;
m_nWidth = width;
m_nPaintWidth = width;
m_nHeight = height;
m_nPaintHeight = height;

// release old storage
if (m_ucHistory) delete[] m_ucHistory;
if ( m_ucBitmap ) delete [] m_ucBitmap;
if ( m_ucOriginal ) delete [] m_ucOriginal;
if ( m_ucPainting ) delete [] m_ucPainting;
if (m_ucEdge) delete[] m_ucEdge;
delete[] m_ucHistory;
if (m_ucBitmap == m_ucOriginal)
{
delete[] m_ucOriginal;
} else
{
delete[] m_ucBitmap;
delete[] m_ucOriginal;
}
delete[] m_ucPainting;
delete[] m_ucEdge;

m_ucBitmap = data;
m_ucOriginal = data;
m_ucBitmap = dataPtr;
m_ucOriginal = dataPtr;

// allocate space for draw view
m_ucPainting = new unsigned char [width*height*3];
m_ucHistory = new unsigned char [width*height*3];
m_ucEdge = new unsigned char [width*height*3];
memset(m_ucPainting, 0, width*height*3);
memset(m_ucHistory, 0, width*height*3);
memset(m_ucEdge, 0, width*height*3);

m_pUI->m_mainWindow->resize(m_pUI->m_mainWindow->x(),
m_pUI->m_mainWindow->y(),
width*2,
height+25);
m_ucPainting = new unsigned char[width*height * 3];
m_ucHistory = new unsigned char[width*height * 3];
m_ucEdge = new unsigned char[width*height * 3];
memset(m_ucPainting, 0, width*height * 3);
memset(m_ucHistory, 0, width*height * 3);
memset(m_ucEdge, 0, width*height * 3);

m_pUI->m_mainWindow->resize(m_pUI->m_mainWindow->x(),
m_pUI->m_mainWindow->y(),
width * 2,
height + 25);

// display it on origView
m_pUI->m_origView->resizeWindow(width, height);
m_pUI->m_origView->resizeWindow(width, height);
m_pUI->m_origView->refresh();

// refresh paint view as well
m_pUI->m_paintView->resizeWindow(width, height);
m_pUI->m_paintView->resizeWindow(width, height);
m_pUI->m_paintView->refresh();


Expand Down
1 change: 1 addition & 0 deletions ImpressionistDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ImpressionistDoc
void setUI(ImpressionistUI* ui); // Assign the UI to use

int loadImage(char *iname); // called by the UI to load image
int loadImageFromData(unsigned char* dataPtr, int width, int height);
int loadAnotherImage(char* iname);
int loadMuralImage(char* iname);
int loadAlphaMap(char* iname);
Expand Down
3 changes: 3 additions & 0 deletions ImpressionistUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "impressionistUI.h"
#include "impressionistDoc.h"
#include <sstream>
#include "VideoProcessor.h"

/*
//------------------------------ Widget Examples -------------------------------------------------
Expand Down Expand Up @@ -670,6 +671,8 @@ Fl_Menu_Item ImpressionistUI::menuitems[] = {
{"&Load Another Img", FL_ALT +'l', (Fl_Callback*)ImpressionistUI::cb_load_another_image },
{"&Load Mural Img", FL_ALT +'m', (Fl_Callback*)ImpressionistUI::cb_load_mural_image },
{"&Load Alpha Map Img", FL_ALT +'a', (Fl_Callback*)ImpressionistUI::cb_load_alpha_map_image },
{"Video Auto-Fill", 0, VideoProcessor::cbVideoAutoFill},
{"Video Paintly", 0, VideoProcessor::cbVideoPaintly},
{ 0 },
{ "&Help", 0, 0, 0, FL_SUBMENU },
{ "&About", FL_ALT + 'a', (Fl_Callback *)ImpressionistUI::cb_about },
Expand Down
Loading

0 comments on commit c2a271f

Please sign in to comment.