You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SimpleSkyCam/videoframe.cc

144 lines
2.6 KiB

#include "video.h"
#include "config.h"
#include "gui.h"
#define VIDEOFRAME_DEPTH_BYTES 3
VideoFrame::VideoFrame() {
data = NULL;
w = 0;
h = 0;
size = 0;
};
VideoFrame::~VideoFrame() {
if (data) {
free (data);
data = NULL;
}
w = 0;
h = 0;
size = 0;
};
//
// we will only allocate a new image if the needed size increases
void VideoFrame::SetSize(int nw, int nh) {
uint32_t newsize = VIDEOFRAME_DEPTH_BYTES * nh * nw;
if (newsize > size || data == NULL) {
unsigned char *newdata = (unsigned char *) malloc (newsize);
if (data != NULL) free (data);
data = newdata;
size = newsize;
}
w = nw;
h = nh;
};
VideoFrame VideoFrame::operator=(VideoFrame rightside) {
w = rightside.w;
h = rightside.h;
size = rightside.size;
if (size > 0) data = (unsigned char *) malloc (rightside.size);
if (rightside.data != NULL && size > 0) {
memcpy (data, rightside.data, size);
}
return *this;
}
void VideoFrame::CopyFrom(VideoFrame *source) {
SetSize (source->w, source->h);
memcpy(data, source->data, VIDEOFRAME_DEPTH_BYTES * w * h);
}
void VideoFrame::CopyFrom(FloatImage *source) {
float min, max;
int x, y, i, idx;
unsigned char v;
SetSize (source->w, source->h);
// find min max;
min = source->data[0];
max = source->data[0];
for (y = 0; y < h; y++) for (x = 0; x < w; x++)
for (i = 0; i < 3; i++) {
idx = 3 * (y * w + x);
if (min > source->data[idx+i]) min = source->data[idx+i];
if (max < source->data[idx+i]) max = source->data[idx+i];
}
for (y = 0; y < h; y++) for (x = 0; x < w; x++)
for (i = 0; i < 3; i++) {
idx = 3 * (y * w + x);
v = (float)(256.0 * (source->data[idx+i]-min) / (max-min));
data[idx+i] = v;
}
}
FloatImage::FloatImage() {
data = NULL;
w = 0;
h = 0;
size = 0;
};
FloatImage::~FloatImage() {
if (data) {
free (data);
data = NULL;
}
w = 0;
h = 0;
size = 0;
};
//
// we will only allocate a new image if the needed size increases
void FloatImage::SetSize(int nw, int nh) {
uint32_t newsize = 3 * nh * nw * sizeof(float);
if (newsize > size || data == NULL) {
float *newdata = (float *) malloc (newsize);
if (data != NULL) free (data);
data = newdata;
size = newsize;
}
w = nw;
h = nh;
};
FloatImage FloatImage::operator=(FloatImage rightside) {
w = rightside.w;
h = rightside.h;
size = rightside.size;
if (size > 0) data = (float *) malloc (rightside.size);
if (rightside.data != NULL && size > 0) {
memcpy (data, rightside.data, size);
}
return *this;
}
void FloatImage::CopyFrom(FloatImage *source) {
SetSize (source->w, source->h);
memcpy(data, source->data, 3 * w * h);
}