added BMP class to write a test image - image capturing working!!

git-svn-id: svn://anubis/misc/WebcamTracker@128 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2009-08-12 03:49:29 +00:00
parent c261550290
commit 278f641485
3 changed files with 202 additions and 1 deletions

127
BMP.cc Normal file
View File

@ -0,0 +1,127 @@
// 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 <stdio.h>
#include <iostream>
using namespace std;
BMP::BMP(const char * fileName)
{
open(fileName);
}
BMP::BMP(const char * fileName, int width, int height, unsigned char * data)
{
create(fileName, width, height, data);
}
BMP::~BMP()
{
if (m_fp != NULL)
close();
}
bool BMP::open(const char * fileName)
{
size_t bytes_read;
m_fp = fopen(fileName, "r+");
if (m_fp != NULL)
{
// read file header
bytes_read = 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
bytes_read = fread(&m_info, sizeof(m_info), 1, m_fp);
}
return (m_fp != NULL);
}
bool BMP::create(const char * fileName, int width, int height,
unsigned char * data)
{
size_t bytes_written;
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 */
bytes_written = fwrite(&m_header, sizeof(m_header), 1, m_fp);
bytes_written = 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++)
{
bytes_written = fwrite(data_ptr, 3 * width, 1, m_fp);
if (row_padding)
bytes_written = fwrite(&zero, row_padding, 1, m_fp);
data_ptr -= 3 * width;
}
}
return (m_fp != NULL);
}
void BMP::read(unsigned char * buf)
{
size_t bytes_read;
if (m_fp != NULL)
{
int row_data_bytes = 3 * m_info.width;
int row_padding = row_data_bytes & 0x3;
if (row_padding)
row_padding = 4 - row_padding;
fseek(m_fp,
(m_header.offset != 0
? m_header.offset
: sizeof(m_header) + sizeof(m_info)),
SEEK_SET);
unsigned char * data_ptr = buf + row_data_bytes * (m_info.height - 1);
for (int i = 0; i < m_info.height; i++)
{
bytes_read = fread(data_ptr, row_data_bytes, 1, m_fp);
if (row_padding)
fseek(m_fp, row_padding, SEEK_CUR);
data_ptr -= row_data_bytes;
}
}
}
void BMP::close()
{
if (m_fp != NULL)
fclose(m_fp);
m_fp = NULL;
}

54
BMP.h Normal file
View File

@ -0,0 +1,54 @@
// 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 <stdio.h>
#define BMP_RED 2
#define BMP_GREEN 1
#define BMP_BLUE 0
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(const char * fileName);
BMP(const char * fileName, int width, int height, unsigned char * data);
~BMP();
void read(unsigned char * buf);
int getWidth() { return m_info.width; }
int getHeight() { return m_info.height; }
protected:
FILE * m_fp;
header_t m_header;
info_t m_info;
bool open(const char * fileName);
bool create(const char * fileName, int width, int height,
unsigned char * data);
void close();
};

View File

@ -11,6 +11,7 @@
#include <string.h> /* memset() */
#include <iostream>
#include "WebcamTracker.h"
#include "BMP.h"
using namespace std;
int main(int argc, char * argv[])
@ -59,7 +60,9 @@ bool WebcamTracker::open(const char * device)
void WebcamTracker::start()
{
setFormat(640, 480);
int width = 640;
int height = 480;
setFormat(width, height);
requestBuffers(20);
for (int i = 0; i < m_numbufs; i++)
{
@ -67,9 +70,26 @@ void WebcamTracker::start()
queueBuffer(i);
}
beginStreaming();
v4l2_buffer buf;
dequeueBuffer(&buf);
endStreaming();
/* do something with the image referenced by buf */
unsigned char * image_data = (unsigned char *) m_buffers[buf.index];
unsigned char * bmp_data = new unsigned char[width * height * 3];
for (int j = 0; j < height; j++)
{
for (int k = 0; k < width; k++)
{
unsigned char y = image_data[(width * j + k) * 2];
bmp_data[width * j * 3 + k * 3] = y;
bmp_data[width * j * 3 + k * 3 + 1] = y;
bmp_data[width * j * 3 + k * 3 + 2] = y;
}
}
BMP bmp("webcam.bmp", width, height, bmp_data);
delete bmp_data;
}
void WebcamTracker::requestBuffers(int num)