root/trunk/lib/cc3-ilp/cc3_jpg.c

Revision 392, 2.5 kB (checked in by goodea, 2 years ago)

pixbuf

Line 
1 /*
2  * Copyright 2006-2007  Anthony Rowe and Adam Goode
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "cc3.h"
23 #include "cc3_jpg.h"
24 #include <jpeglib.h>
25
26 typedef struct {
27   struct jpeg_compress_struct cinfo;
28   struct jpeg_error_mgr jerr;
29 } cc3_jpeg_t;
30
31
32 static void destroy_jpeg(cc3_jpeg_t *cj);
33 static cc3_jpeg_t *init_jpeg (void);
34 static void capture_current_jpeg(cc3_jpeg_t *cj, FILE *f);
35
36 void cc3_jpeg_send_simple(void) {
37   // capture a frame to the FIFO
38   cc3_pixbuf_load();
39
40   // init jpeg (allocates memory based on FIFO contents)
41   cc3_jpeg_t *cj = init_jpeg();
42
43   capture_current_jpeg(cj, stdout);
44
45   destroy_jpeg(cj);
46 }
47
48
49
50 static cc3_jpeg_t *init_jpeg(void) {
51   cc3_jpeg_t *cj = malloc(sizeof(cc3_jpeg_t));
52   if (cj == NULL) {
53     return NULL;
54   }
55
56   // init error structure
57   cj->cinfo.err = jpeg_std_error(&cj->jerr);
58
59   // init jpeg structure
60   jpeg_create_compress(&cj->cinfo);
61
62   // parameters for jpeg image
63   cj->cinfo.image_width = cc3_g_pixbuf_frame.width;
64   cj->cinfo.image_height = cc3_g_pixbuf_frame.height;
65
66   //printf( "image width=%d image height=%d\n", cinfo.image_width, cinfo.image_height );
67   cj->cinfo.input_components = 3;
68  // cinfo.in_color_space = JCS_YCbCr;
69   cj->cinfo.in_color_space = JCS_RGB;
70
71   // set image quality, etc.
72   jpeg_set_defaults(&cj->cinfo);
73   jpeg_set_quality(&cj->cinfo, 85, true);
74
75   // return
76   return cj;
77 }
78
79 static void capture_current_jpeg(cc3_jpeg_t *cj, FILE *f) {
80   JSAMPROW row_pointer[1];
81
82   // allocate memory for 1 row
83   row_pointer[0] = cc3_malloc_rows(1);
84   if (row_pointer[0] == NULL) {
85     return;
86   }
87
88   // output is file
89   jpeg_stdio_dest(&cj->cinfo, f);
90
91   // read and compress
92   jpeg_start_compress(&cj->cinfo, TRUE);
93   while (cj->cinfo.next_scanline < cj->cinfo.image_height) {
94     cc3_pixbuf_read_rows(row_pointer[0], 1);
95     jpeg_write_scanlines(&cj->cinfo, row_pointer, 1);
96   }
97
98   // finish
99   jpeg_finish_compress(&cj->cinfo);
100
101   free(row_pointer[0]);
102 }
103
104
105 static void destroy_jpeg(cc3_jpeg_t *cj) {
106   jpeg_destroy_compress(&cj->cinfo);
107   free(cj);
108 }
Note: See TracBrowser for help on using the browser.