static int save_stdjpeg(const char *name, struct ImBuf *ibuf) { FILE * outfile; struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo; struct my_error_mgr jerr; if ((outfile = fopen(name, "wb")) == NULL) return 0; jpeg_default_quality = 75; cinfo->err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = jpeg_error; /* Establish the setjmp return context for jpeg_error to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jpeg_destroy_compress(cinfo); fclose(outfile); remove(name); return 0; } init_jpeg(outfile, cinfo, ibuf); write_jpeg(cinfo, ibuf); fclose(outfile); jpeg_destroy_compress(cinfo); return 1; }
void write_jpeg(unsigned char *image, int width, int height, char *filename) { struct jpeg_compress_struct jpeg; struct jpeg_error_mgr jerr; FILE *file = NULL; JSAMPROW *rows; int j; file = fopen(filename, "wb"); if(NULL == file) { printf("Failed to open %s\n", filename); exit(1); } rows = malloc(height * sizeof(JSAMPLE*)); if(NULL == rows) { printf("Failed to allocate row memory\n"); exit(1); } init_jpeg(&jpeg, &jerr, file, width, height); jpeg_start_compress(&jpeg, TRUE); for(j = 0; j < height; ++j) rows[j] = &image[j * width * 3]; jpeg_write_scanlines(&jpeg, rows, height); jpeg_finish_compress(&jpeg); fclose(file); jpeg_destroy_compress(&jpeg); }
bool initialize() { char* config_buff = (char*)malloc(sizeof(char)*100); // init some variables prevTime = 0; deltaTime = 0; deltaDist = 0; power_save = false; cam_focus_delay = 8000; // init parser structures for gps and config parse_init(); // turn on leds so we know it started working cc3_led_set_state (1, true); cc3_led_set_state (2, true); // configure uart for gps serial communication cc3_uart_init (0, CC3_UART_RATE_4800, CC3_UART_MODE_8N1, CC3_UART_BINMODE_BINARY); // init the camera and file system cc3_camera_init (); cc3_filesystem_init(); #ifdef LOG snprintf(log_str, 100, "**********\n\rNew Session\n\r"); write_log(); snprintf(log_str, 100, "\n\rReading config file\r\n"); write_log(); #endif // read config file from MMC memory = fopen ("c:/config.txt", "r"); if (memory == NULL) { perror ("fopen failed\r\n"); return false; } // get config file fscanf(memory, "%s", config_buff); if (fclose (memory) == EOF) { perror ("fclose failed\r\n"); return false; } // parse config file parse_Config(config_buff); // if the config is not good then quit if(!config->good) { #ifdef LOG snprintf(log_str, 100, "\n\rconfig.txt INVALID\r\n"); write_log(); #endif return false; } #ifdef LOG snprintf(log_str, 100, "\r\nConfig File:\n\rDelay(ms) - %d\tMin Dist(mm) - %d",(int)config->delay,(int)(config->min_dist*1000)); write_log(); if(config->halo) { snprintf(log_str, 100, "\tHalo - true\r\n"); write_log(); snprintf(log_str, 100, "\tHalo %s:\t Lat*1000000 - %d\tLon*1000000 - %d\tRange(mm) - %d\r\n", config->halo_info->name, (int)(config->halo_info->lat*1000000), (int)(config->halo_info->lon*1000000), (int)(config->halo_info->range*1000) ); write_log(); } else { snprintf(log_str, 100, "\tHalo - false\r\n"); write_log(); } #endif //configure camera cc3_camera_set_colorspace (CC3_COLORSPACE_RGB); cc3_camera_set_resolution (CC3_CAMERA_RESOLUTION_HIGH); cc3_camera_set_auto_white_balance (true); cc3_camera_set_auto_exposure (true); // init pixbuf with width and height and JPEG compression cc3_pixbuf_load(); init_jpeg(); // try to open picNum.txt if exist that will be the // picture number we will start with if not start at 0 #ifdef LOG snprintf(log_str, 100, "\n\rReading picNum file\r\n"); write_log(); #endif memory = fopen ("c:/picNum.txt", "r"); if (memory == NULL) { picNum = 0; } else { char* picNum_buff = (char*)malloc(sizeof(char)*100); fscanf(memory, "%s", picNum_buff); picNum = atoi(picNum_buff); free(picNum_buff); } if (fclose (memory) == EOF) { perror ("fclose failed\r\n"); return false; } #ifdef LOG snprintf(log_str, 100, "Starting picture numbering at: %d\r\n",picNum); write_log(); #endif // starts out awake with no gps signal cc3_led_set_state (1, false); cc3_led_set_state (2, true); cc3_timer_wait_ms(1000); free(config_buff); return true; }