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.
213 lines
5.8 KiB
213 lines
5.8 KiB
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <jpeglib.h>
|
|
#include <UDPTCPNetwork.h>
|
|
#include <math.h>
|
|
|
|
#include "videoframe.h"
|
|
#include "inmemoryfile.h"
|
|
|
|
VideoFrame::VideoFrame() {
|
|
mem = NULL;
|
|
width = 0;
|
|
height = 0;
|
|
mem_allocated = 0;
|
|
};
|
|
|
|
VideoFrame::~VideoFrame() {
|
|
FreeFrame();
|
|
};
|
|
|
|
|
|
void VideoFrame::FreeFrame() {
|
|
if (mem != NULL) {
|
|
free (mem);
|
|
mem = NULL;
|
|
width = 0;
|
|
height = 0;
|
|
mem_allocated = 0;
|
|
}
|
|
};
|
|
|
|
|
|
void VideoFrame::AllocateFrame() {
|
|
int memnewsize = width * height * 3;
|
|
if (memnewsize <= mem_allocated) return;
|
|
else if (memnewsize == 0) FreeFrame();
|
|
|
|
mem = (unsigned char *) realloc (mem, memnewsize);
|
|
mem_allocated = memnewsize;
|
|
|
|
if (mem == NULL) {
|
|
debug ("Error on allocation new frame\n");
|
|
exit (1);
|
|
}
|
|
};
|
|
|
|
|
|
int VideoFrame::SetSize(int w, int h) {
|
|
if (w < 0 && h < 0) return 0;
|
|
if (w == width && h == height) return 1;
|
|
|
|
width = w;
|
|
height = h;
|
|
|
|
AllocateFrame();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
/// @brief converts image to jpeg.
|
|
/// @param imf ptr to in memory file
|
|
/// @param quality
|
|
/// @return 0 on error, 1 on success
|
|
int VideoFrame::ConvertToJpeg(InMemoryFile *imf, int quality) {
|
|
unsigned char *outbuffer = NULL;
|
|
unsigned long int outbuffersize;
|
|
struct jpeg_compress_struct cinfo;
|
|
struct jpeg_error_mgr jerr;
|
|
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
|
|
int row_stride; /* physical row width in image buffer */
|
|
|
|
if (imf == NULL) return 0;
|
|
if (height == 0 || width == 0) return 0;
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
jpeg_mem_dest(&cinfo, &outbuffer, &outbuffersize);
|
|
|
|
cinfo.image_width = width; /* image width and height, in pixels */
|
|
cinfo.image_height = height;
|
|
cinfo.input_components = 3; /* # of color components per pixel */
|
|
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
|
|
jpeg_set_defaults(&cinfo);
|
|
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
|
|
jpeg_start_compress(&cinfo, TRUE);
|
|
row_stride = width * 3; /* JSAMPLEs per row in image_buffer (24bit image)*/
|
|
|
|
while (cinfo.next_scanline < cinfo.image_height) {
|
|
row_pointer[0] = & mem[cinfo.next_scanline * row_stride];
|
|
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
|
}
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
imf->CopyFrom(outbuffer, outbuffersize);
|
|
free (outbuffer);
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int VideoFrame::TestScreen(int w, int h) {
|
|
int x, y;
|
|
unsigned char r, g, b;
|
|
|
|
SetSize (w, h);
|
|
|
|
r = g = b = 0;
|
|
for (x = 0; x < w; x++) {
|
|
for (g = 0, y = 0; y < h; y++) {
|
|
b = r + g;
|
|
|
|
mem[3*(x + y * width) + 0] = r;
|
|
mem[3*(x + y * width) + 1] = g;
|
|
mem[3*(x + y * width) + 2] = b;
|
|
|
|
g++;
|
|
}
|
|
r ++;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
int VideoFrame::CopyTo(VideoFrame *dest, int destw, int desth, float *brightness) {
|
|
unsigned char *destptr;
|
|
if (dest == NULL) return 0;
|
|
|
|
if (destw <= 0 || desth <= 0) {
|
|
destw = width;
|
|
desth = height;
|
|
}
|
|
|
|
dest->SetSize(destw, desth);
|
|
destptr = dest->GetPixBuf();
|
|
if (destptr == NULL) return 0;
|
|
|
|
float scale_x = (float)width / destw;
|
|
float scale_y = (float)height / desth;
|
|
|
|
for (int dy = 0; dy < dest->height; ++dy) {
|
|
for (int dx = 0; dx < dest->width; ++dx) {
|
|
// Map destination coordinates back to source coordinates
|
|
int sx = (int)(dx * scale_x);
|
|
int sy = (int)(dy * scale_y);
|
|
|
|
// Bounds checking (should be unnecessary with correct scale calculation)
|
|
if (sx >= width) sx = width - 1;
|
|
if (sy >= height) sy = height - 1;
|
|
|
|
// Calculate byte index in the source and destination buffers
|
|
int src_idx = 3*((sy * width) + sx);
|
|
int dest_idx = 3*((dy * destw) + dx);
|
|
|
|
// Copy the pixel data (e.g., all 3 RGB bytes)
|
|
memcpy(&destptr[dest_idx], &mem[src_idx], 3);
|
|
|
|
// calculate the overall brightness of the image and store it in the brightness array if provided
|
|
if (brightness != NULL) {
|
|
float pixel_brightness = (0.299f * mem[src_idx] + 0.587f * mem[src_idx + 1] + 0.114f * mem[src_idx + 2]) / 255.0f;
|
|
*brightness += pixel_brightness;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (brightness != NULL) {
|
|
*brightness /= (destw * desth);
|
|
*brightness = sqrtf(*brightness);
|
|
}
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int VideoFrame::CopyTo(VideoFrame *dest) {
|
|
if (dest == NULL) return 0;
|
|
if (dest->GetWidth() != width || dest->GetHeight() != height)
|
|
dest->SetSize(width, height);
|
|
memcpy(dest->GetPixBuf(), mem, width * height * 3);
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int VideoFrame::CopyToFiltered(VideoFrame *dest, int filter_weight) {
|
|
// check if destination is equal to source size, if not resize destination and blindly copy image data
|
|
if (dest == NULL) return 0;
|
|
if (dest->GetWidth() != width || dest->GetHeight() != height) {
|
|
dest->SetSize(width, height);
|
|
memcpy(dest->GetPixBuf(), mem, width * height * 3);
|
|
return 1;
|
|
}
|
|
|
|
// merge the destination image with the source image using the old and the new pixel values averaging them out with the filter_weight
|
|
unsigned char *destptr = dest->GetPixBuf();
|
|
for (int y = 0; y < height; y++) {
|
|
for (int x = 0; x < width; x++) {
|
|
int idx = 3 * (y * width + x);
|
|
destptr[idx + 0] = (destptr[idx + 0] * (100 - filter_weight) + mem[idx + 0] * filter_weight) / 100;
|
|
destptr[idx + 1] = (destptr[idx + 1] * (100 - filter_weight) + mem[idx + 1] * filter_weight) / 100;
|
|
destptr[idx + 2] = (destptr[idx + 2] * (100 - filter_weight) + mem[idx + 2] * filter_weight) / 100;
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
};
|