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.

183 lines
4.3 KiB

#include <stdlib.h>
#include <stdio.h>
#include <jpeglib.h>
#include <UDPTCPNetwork.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;
width = w;
height = h;
AllocateFrame();
return 1;
};
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 */
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) {
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);
}
}
return 1;
};
/*********************************************************************/
void VideoFrameFloat::AllocateFrame() {
printf ("VideoFrameFloat::AllocateFrame()\n");
int memnewsize = width * height * 3 * sizeof(float);
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);
}
};