///////////////////////////////////////////////////////////////////////////// // Rev 11/12/2003 // // // BMP2JPG.C // --------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Convert a bmp file to jpeg. // should be linked with the jpeglib-6b (http://www.ijg.org/) // ///////////////////////////////////////////////////////////////////////////// #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ #include "jversion.h" /* for version message */ #ifdef USE_CCOMMAND /* command-line reader for Macintosh */ #ifdef __MWERKS__ #include /* Metrowerks needs this */ #include /* ... and this */ #endif #ifdef THINK_C #include /* Think declares it here */ #endif #endif #include "bmp2jpg.h" /* Create the add-on message string table. */ #define JMESSAGE(code,string) string , static const char * const cdjpeg_message_table[] = { #include "cderror.h" NULL }; #define MAIN #ifdef MAIN // The test program int main( int argc, char * argv[] ) { if( argc != 3 ) { printf( "Usage : %s \n" ); return 1; } return Bmp2Jpg( argv[1], argv[2] ); } #endif ///////////////////////////////////////////////////////////////////////////// // // BMP2JPG ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --Bmp2Jpg-- // // See jpeg-6b/cjpeg.c for details // (This is a dirty copy-paste) // // // ARGUMENTS // Argument1: char * pszBmpFile // Argument2: char * pszJpgFile // RETOUR/RESULTAT // int // REMARQUE // Rev 11/12/2003 ////////////////////////////////////////////////////////////////////////////// int Bmp2Jpg( char * pszBmpFile, char * pszJpgFile ) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; cjpeg_source_ptr src_mgr; FILE * input_file; FILE * output_file; JDIMENSION num_scanlines; /* Initialize the JPEG compression object with default error handling. */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); /* Add some application-specific error messages (from cderror.h) */ jerr.addon_message_table = cdjpeg_message_table; jerr.first_addon_message = JMSG_FIRSTADDONCODE; jerr.last_addon_message = JMSG_LASTADDONCODE; /* Initialize JPEG parameters. * Much of this may be overridden later. * In particular, we don't yet know the input file's color space, * but we need to provide some value for jpeg_set_defaults() to work. */ cinfo.in_color_space = JCS_RGB; /* arbitrary guess */ jpeg_set_defaults(&cinfo); /* Open the input file. */ if ((input_file = fopen(pszBmpFile, READ_BINARY)) == NULL) { return -1; } /* Open the output file. */ if ((output_file = fopen(pszJpgFile, WRITE_BINARY)) == NULL) { return -1; } /* Figure out the input file format, and set up to read it. */ src_mgr = (cjpeg_source_ptr)jinit_read_bmp(&cinfo); src_mgr->input_file = input_file; /* Read the input file header to obtain file size & colorspace. */ (*src_mgr->start_input) (&cinfo, src_mgr); /* Now that we know input colorspace, fix colorspace-dependent defaults */ jpeg_default_colorspace(&cinfo); /* Adjust default compression parameters by re-parsing the options */ //file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); /* Specify data destination for compression */ jpeg_stdio_dest(&cinfo, output_file); /* Start compressor */ jpeg_start_compress(&cinfo, TRUE); /* Process data */ while (cinfo.next_scanline < cinfo.image_height) { num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); } /* Finish compression and release memory */ (*src_mgr->finish_input) (&cinfo, src_mgr); jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); /* Close files, if we opened them */ if (input_file != stdin) fclose(input_file); if (output_file != stdout) fclose(output_file); return 0; }