Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / vision / savetonetwork.c @ 939

History | View | Annotate | Download (1.1 KB)

1 936 rcahoon
#include <stdio.h>
2
#include "jpeglib.h"
3
#include <cv.h>
4
#include "memdst.h"
5
6
int write_JPEG_to_mem (unsigned char *storage, int length, IplImage *_img, int quality)
7
{
8
  struct jpeg_compress_struct cinfo;
9
  struct jpeg_error_mgr jerr;
10
  JSAMPROW row_pointer[1];
11
12
  int numBytes = -1; //size of jpeg after compression
13
14
  IplImage *img;
15
16
  img = cvCloneImage(_img);
17
  cvCvtColor(_img, img, CV_RGB2BGR);
18
19
  cinfo.err = jpeg_std_error(&jerr);
20
21
  jpeg_create_compress(&cinfo);
22
23
  jpeg_memory_dest(&cinfo,(JOCTET*)storage,length,&numBytes);
24
25
  cinfo.image_width = img->width;
26
  cinfo.image_height = img->height;
27
  cinfo.input_components = img->nChannels;
28
  cinfo.in_color_space = JCS_RGB;
29
30
  jpeg_set_defaults(&cinfo);
31
  jpeg_set_quality(&cinfo, quality, TRUE );
32
33
  jpeg_start_compress(&cinfo, TRUE);
34
35
  while (cinfo.next_scanline < cinfo.image_height) {
36
    row_pointer[0] = (unsigned char *)(img->imageData + cinfo.next_scanline * img->widthStep);
37
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
38
  }
39
40
  jpeg_finish_compress(&cinfo);
41
  jpeg_destroy_compress(&cinfo);
42
43 939 rcahoon
  cvReleaseImage(&img);
44
45 936 rcahoon
  return numBytes;
46
}