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.

145 lines
3.1 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;
size_t 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;
}
/*********************************************************************/
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);
}
};