Ticket #144: Output file error.c

File Output file error.c, 2.5 kB (added by DARCAM, 8 months ago)
Line 
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "cc3.h"
5 #include "jpeglib.h"
6
7 static void capture_current_jpeg(FILE *f);
8 static void init_jpeg(void);
9 static void destroy_jpeg(void);
10
11 int main(void)
12 {
13
14   int i,j;
15   FILE *f;
16
17   cc3_uart_init (0, CC3_UART_RATE_115200, CC3_UART_MODE_8N1, CC3_UART_BINMODE_TEXT);
18
19   cc3_camera_init ();
20
21   cc3_filesystem_init();
22
23   cc3_camera_set_resolution(CC3_CAMERA_RESOLUTION_LOW);
24   cc3_pixbuf_frame_set_subsample (CC3_SUBSAMPLE_NEAREST, 2, 1);
25   cc3_timer_wait_ms(1000);
26
27   // init pixbuf with width and height
28   cc3_pixbuf_load();
29
30   Init_jpeg();
31
32   cc3_led_set_state(1, true);
33
34   i = 0;
35   while(true)
36   {
37
38     printf("Image jpg num: %d\n",i);
39     f = fopen("c:/cmucam.jpg", "w+");
40     if ( f!=NULL )
41     {
42       capture_current_jpeg(f);
43       rewind(f);
44       j=0;
45       while ( !feof(f) )
46       {
47         fgetc (f);
48         j++;
49       }
50       printf("Image size: %d bytes\n",j);
51       fclose(f);
52     }
53     else
54     {
55      printf("Error acess file !\n");
56     }
57     i++;
58   }
59
60   destroy_jpeg();
61   return 0;
62
63 }
64
65 //------------------------------------------------------------------------------
66
67 static struct jpeg_compress_struct cinfo;
68 static struct jpeg_error_mgr jerr;
69 uint8_t *row;
70
71 void init_jpeg(void)
72 {
73
74   cinfo.err = jpeg_std_error(&jerr);
75   jpeg_create_compress(&cinfo);
76
77   // parameters for jpeg image
78   cinfo.image_width = cc3_g_pixbuf_frame.width;
79   cinfo.image_height = cc3_g_pixbuf_frame.height;
80   printf( "image width=%d image height=%d\n", cinfo.image_width, cinfo.image_height );
81   cinfo.input_components = 3;
82
83   cinfo.in_color_space = JCS_RGB;
84
85   // set image quality, etc.
86   jpeg_set_defaults(&cinfo);
87   jpeg_set_quality(&cinfo, 100, true);
88
89   // allocate memory for 1 row
90   row = cc3_malloc_rows(1);
91   if(row==NULL) printf( "Out of memory!\n" );
92
93 }
94
95 void capture_current_jpeg(FILE *f)
96 {
97
98   JSAMPROW row_pointer[1]; row_pointer[0] = row;
99
100   // output is file
101   jpeg_stdio_dest(&cinfo, f);
102
103   // capture a frame to the FIFO
104   cc3_pixbuf_load();
105
106   // read and compress
107   jpeg_start_compress(&cinfo, TRUE);
108   while (cinfo.next_scanline < cinfo.image_height)
109   {
110
111     cc3_pixbuf_read_rows(row, 1);
112     jpeg_write_scanlines(&cinfo, row_pointer, 1);
113
114   }
115
116
117   // finish
118   jpeg_finish_compress(&cinfo);
119
120 }
121
122 void destroy_jpeg(void)
123 {
124
125   jpeg_destroy_compress(&cinfo);
126   free(row);
127
128 }
129