diff options
author | dcommander <dcommander@632fc199-4ca6-4c93-a231-07263d6284db> | 2013-01-18 23:42:31 +0000 |
---|---|---|
committer | dcommander <dcommander@632fc199-4ca6-4c93-a231-07263d6284db> | 2013-01-18 23:42:31 +0000 |
commit | bebb38d3efdeba0f1893c0c3bf74212873b977d8 (patch) | |
tree | cbb9c0fceb3e39c397f5509b7d842f9962c6c07b /cjpeg.c | |
parent | 8525b369cb6144802921c25bcc36a5431a29fab2 (diff) |
Implement in-memory source/destination managers even when not emulating the libjpeg v8 API/ABI
git-svn-id: svn://svn.code.sf.net/p/libjpeg-turbo/code/trunk@913 632fc199-4ca6-4c93-a231-07263d6284db
Diffstat (limited to 'cjpeg.c')
-rw-r--r-- | cjpeg.c | 72 |
1 files changed, 51 insertions, 21 deletions
@@ -5,7 +5,7 @@ * Copyright (C) 1991-1998, Thomas G. Lane. * Modified 2003-2011 by Guido Vollbeding. * Modifications: - * Copyright (C) 2010, D. R. Commander. + * Copyright (C) 2010, 2013, D. R. Commander. * For conditions of distribution and use, see the accompanying README file. * * This file contains a command-line user interface for the JPEG compressor. @@ -139,6 +139,7 @@ select_file_type (j_compress_ptr cinfo, FILE * infile) static const char * progname; /* program name for error messages */ static char * outfilename; /* for -outfile switch */ +boolean memdst; /* for -memdst switch */ LOCAL(void) @@ -187,6 +188,9 @@ usage (void) #endif fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); fprintf(stderr, " -outfile name Specify name for output file\n"); +#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) + fprintf(stderr, " -memdst Compress to memory instead of file (useful for benchmarking)\n"); +#endif fprintf(stderr, " -verbose or -debug Emit debug output\n"); fprintf(stderr, "Switches for wizards:\n"); fprintf(stderr, " -baseline Force baseline quantization tables\n"); @@ -228,6 +232,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv, simple_progressive = FALSE; is_targa = FALSE; outfilename = NULL; + memdst = FALSE; cinfo->err->trace_level = 0; /* Scan command line options, adjust parameters */ @@ -312,7 +317,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv, #ifdef ENTROPY_OPT_SUPPORTED cinfo->optimize_coding = TRUE; #else - fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n", + fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n", progname); exit(EXIT_FAILURE); #endif @@ -329,11 +334,21 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv, simple_progressive = TRUE; /* We must postpone execution until num_components is known. */ #else - fprintf(stderr, "%s: sorry, progressive output was not compiled\n", + fprintf(stderr, "%s: sorry, progressive output was not compiled in\n", progname); exit(EXIT_FAILURE); #endif + } else if (keymatch(arg, "memdst", 2)) { + /* Use in-memory destination manager */ +#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) + memdst = TRUE; +#else + fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n", + progname); + exit(EXIT_FAILURE); +#endif + } else if (keymatch(arg, "quality", 1)) { /* Quality ratings (quantization table scaling factors). */ if (++argn >= argc) /* advance to next argument */ @@ -394,7 +409,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv, scansarg = argv[argn]; /* We must postpone reading the file in case -progressive appears. */ #else - fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n", + fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n", progname); exit(EXIT_FAILURE); #endif @@ -473,7 +488,9 @@ main (int argc, char **argv) int file_index; cjpeg_source_ptr src_mgr; FILE * input_file; - FILE * output_file; + FILE * output_file = NULL; + unsigned char *outbuffer = NULL; + unsigned long outsize = 0; JDIMENSION num_scanlines; /* On Mac, fetch a command line. */ @@ -516,19 +533,21 @@ main (int argc, char **argv) file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); #ifdef TWO_FILE_COMMANDLINE - /* Must have either -outfile switch or explicit output file name */ - if (outfilename == NULL) { - if (file_index != argc-2) { - fprintf(stderr, "%s: must name one input and one output file\n", - progname); - usage(); - } - outfilename = argv[file_index+1]; - } else { - if (file_index != argc-1) { - fprintf(stderr, "%s: must name one input and one output file\n", - progname); - usage(); + if (!memdst) { + /* Must have either -outfile switch or explicit output file name */ + if (outfilename == NULL) { + if (file_index != argc-2) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + outfilename = argv[file_index+1]; + } else { + if (file_index != argc-1) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } } } #else @@ -556,7 +575,7 @@ main (int argc, char **argv) fprintf(stderr, "%s: can't open %s\n", progname, outfilename); exit(EXIT_FAILURE); } - } else { + } else if (!memdst) { /* default output file is stdout */ output_file = write_stdout(); } @@ -579,7 +598,12 @@ main (int argc, char **argv) file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); /* Specify data destination for compression */ - jpeg_stdio_dest(&cinfo, output_file); +#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) + if (memdst) + jpeg_mem_dest(&cinfo, &outbuffer, &outsize); + else +#endif + jpeg_stdio_dest(&cinfo, output_file); /* Start compressor */ jpeg_start_compress(&cinfo, TRUE); @@ -598,13 +622,19 @@ main (int argc, char **argv) /* Close files, if we opened them */ if (input_file != stdin) fclose(input_file); - if (output_file != stdout) + if (output_file != stdout && output_file != NULL) fclose(output_file); #ifdef PROGRESS_REPORT end_progress_monitor((j_common_ptr) &cinfo); #endif + if (memdst) { + fprintf(stderr, "Compressed size: %lu bytes\n", outsize); + if (outbuffer != NULL) + free(outbuffer); + } + /* All done. */ exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); return 0; /* suppress no-return-value warnings */ |