private static void compress(Stream input, CompressOptions options, Stream output) { Debug.Assert(input != null); Debug.Assert(options != null); Debug.Assert(output != null); jpeg_compress_struct cinfo = new jpeg_compress_struct(new cd_jpeg_error_mgr()); /* 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 = J_COLOR_SPACE.JCS_RGB; /* arbitrary guess */ cinfo.jpeg_set_defaults(); /* Figure out the input file format, and set up to read it. */ cjpeg_source_struct src_mgr = new bmp_source_struct(cinfo); src_mgr.input_file = input; /* Read the input file header to obtain file size & colorspace. */ src_mgr.start_input(); /* Now that we know input colorspace, fix colorspace-dependent defaults */ cinfo.jpeg_default_colorspace(); /* Adjust default compression parameters */ if (!applyOptions(cinfo, options)) return; /* Specify data destination for compression */ cinfo.jpeg_stdio_dest(output); /* Start compressor */ cinfo.jpeg_start_compress(true); /* Process data */ while (cinfo.Next_scanline < cinfo.Image_height) { int num_scanlines = src_mgr.get_pixel_rows(); cinfo.jpeg_write_scanlines(src_mgr.buffer, num_scanlines); } /* Finish compression and release memory */ src_mgr.finish_input(); cinfo.jpeg_finish_compress(); /* All done. */ if (cinfo.Err.Num_warnings != 0) Console.WriteLine("Corrupt-data warning count is not zero"); }
public void TestCompressorWithContextRows() { using (MemoryStream stream = new MemoryStream()) { jpeg_compress_struct compressor = new jpeg_compress_struct(new jpeg_error_mgr()); compressor.Image_height = 100; compressor.Image_width = 100; compressor.In_color_space = J_COLOR_SPACE.JCS_GRAYSCALE; compressor.Input_components = 1; compressor.jpeg_set_defaults(); compressor.Dct_method = J_DCT_METHOD.JDCT_IFAST; compressor.Smoothing_factor = 94; compressor.jpeg_set_quality(75, true); compressor.jpeg_simple_progression(); compressor.Density_unit = DensityUnit.Unknown; compressor.X_density = (short)96; compressor.Y_density = (short)96; compressor.jpeg_stdio_dest(stream); compressor.jpeg_start_compress(true); byte[][] rowForDecompressor = new byte[1][]; int bytesPerPixel = 1; while (compressor.Next_scanline < compressor.Image_height) { byte[] row = new byte[100 * bytesPerPixel]; // wasteful, but gets you 0 bytes every time - content is immaterial. rowForDecompressor[0] = row; compressor.jpeg_write_scanlines(rowForDecompressor, 1); } compressor.jpeg_finish_compress(); byte[] bytes = stream.ToArray(); string filename = "TestCompressorWithContextRows.jpg"; File.WriteAllBytes(Tester.MapOutputPath(filename), bytes); FileAssert.AreEqual(Tester.MapExpectedPath(filename), Tester.MapOutputPath(filename)); } }
/// <summary> /// Initializes the compression object with default parameters, then copy from the source object /// all parameters needed for lossless transcoding. /// </summary> /// <param name="dstinfo">Target JPEG compression object.</param> /// <remarks>Parameters that can be varied without loss (such as scan script and /// Huffman optimization) are left in their default states.</remarks> public void jpeg_copy_critical_parameters(jpeg_compress_struct dstinfo) { /* Safety check to ensure start_compress not called yet. */ if (dstinfo.m_global_state != JpegState.CSTATE_START) ERREXIT(J_MESSAGE_CODE.JERR_BAD_STATE, (int)dstinfo.m_global_state); /* Copy fundamental image dimensions */ dstinfo.m_image_width = m_image_width; dstinfo.m_image_height = m_image_height; dstinfo.m_input_components = m_num_components; dstinfo.m_in_color_space = m_jpeg_color_space; /* Initialize all parameters to default values */ dstinfo.jpeg_set_defaults(); /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. * Fix it to get the right header markers for the image colorspace. */ dstinfo.jpeg_set_colorspace(m_jpeg_color_space); dstinfo.m_data_precision = m_data_precision; dstinfo.m_CCIR601_sampling = m_CCIR601_sampling; /* Copy the source's quantization tables. */ for (int tblno = 0; tblno < JpegConstants.NUM_QUANT_TBLS; tblno++) { if (m_quant_tbl_ptrs[tblno] != null) { if (dstinfo.m_quant_tbl_ptrs[tblno] == null) dstinfo.m_quant_tbl_ptrs[tblno] = new JQUANT_TBL(); Buffer.BlockCopy(m_quant_tbl_ptrs[tblno].quantval, 0, dstinfo.m_quant_tbl_ptrs[tblno].quantval, 0, dstinfo.m_quant_tbl_ptrs[tblno].quantval.Length * sizeof(short)); dstinfo.m_quant_tbl_ptrs[tblno].Sent_table = false; } } /* Copy the source's per-component info. * Note we assume jpeg_set_defaults has allocated the dest comp_info array. */ dstinfo.m_num_components = m_num_components; if (dstinfo.m_num_components < 1 || dstinfo.m_num_components> JpegConstants.MAX_COMPONENTS) ERREXIT(J_MESSAGE_CODE.JERR_COMPONENT_COUNT, dstinfo.m_num_components, JpegConstants.MAX_COMPONENTS); for (int ci = 0; ci < dstinfo.m_num_components; ci++) { dstinfo.Component_info[ci].Component_id = m_comp_info[ci].Component_id; dstinfo.Component_info[ci].H_samp_factor = m_comp_info[ci].H_samp_factor; dstinfo.Component_info[ci].V_samp_factor = m_comp_info[ci].V_samp_factor; dstinfo.Component_info[ci].Quant_tbl_no = m_comp_info[ci].Quant_tbl_no; /* Make sure saved quantization table for component matches the qtable * slot. If not, the input file re-used this qtable slot. * IJG encoder currently cannot duplicate this. */ int tblno = dstinfo.Component_info[ci].Quant_tbl_no; if (tblno < 0 || tblno >= JpegConstants.NUM_QUANT_TBLS || m_quant_tbl_ptrs[tblno] == null) ERREXIT(J_MESSAGE_CODE.JERR_NO_QUANT_TABLE, tblno); JQUANT_TBL c_quant = m_comp_info[ci].quant_table; if (c_quant != null) { JQUANT_TBL slot_quant = m_quant_tbl_ptrs[tblno]; for (int coefi = 0; coefi < JpegConstants.DCTSIZE2; coefi++) { if (c_quant.quantval[coefi] != slot_quant.quantval[coefi]) ERREXIT(J_MESSAGE_CODE.JERR_MISMATCHED_QUANT_TABLE, tblno); } } /* Note: we do not copy the source's Huffman table assignments; * instead we rely on jpeg_set_colorspace to have made a suitable choice. */ } /* Also copy JFIF version and resolution information, if available. * Strictly speaking this isn't "critical" info, but it's nearly * always appropriate to copy it if available. In particular, * if the application chooses to copy JFIF 1.02 extension markers from * the source file, we need to copy the version to make sure we don't * emit a file that has 1.02 extensions but a claimed version of 1.01. * We will *not*, however, copy version info from mislabeled "2.01" files. */ if (m_saw_JFIF_marker) { if (m_JFIF_major_version == 1) { dstinfo.m_JFIF_major_version = m_JFIF_major_version; dstinfo.m_JFIF_minor_version = m_JFIF_minor_version; } dstinfo.m_density_unit = m_density_unit; dstinfo.m_X_density = (short)m_X_density; dstinfo.m_Y_density = (short)m_Y_density; } }
static string outfilename; /* for -outfile switch */ public static void Main(string[] args) { progname = Path.GetFileName(Environment.GetCommandLineArgs()[0]); cd_jpeg_error_mgr err = new cd_jpeg_error_mgr(); jpeg_compress_struct cinfo = new jpeg_compress_struct(err); /* 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 = J_COLOR_SPACE.JCS_RGB; /* arbitrary guess */ cinfo.jpeg_set_defaults(); /* Scan command line to find file names. * It is convenient to use just one switch-parsing routine, but the switch * values read here are ignored; we will rescan the switches after opening * the input file. */ int file_index; if (!parse_switches(cinfo, args, false, out file_index)) { usage(); return; } /* Must have either -outfile switch or explicit output file name */ if (outfilename == null) { // file_index should point to input file if (file_index != args.Length - 2) { Console.WriteLine(string.Format("{0}: must name one input and one output file.", progname)); usage(); return; } // output file comes right after input one outfilename = args[file_index + 1]; } else { // file_index should point to input file if (file_index != args.Length - 1) { Console.WriteLine(string.Format("{0}: must name one input and one output file.", progname)); usage(); return; } } /* Open the input file. */ FileStream input_file = null; if (file_index < args.Length) { try { input_file = new FileStream(args[file_index], FileMode.Open); } catch (Exception e) { Console.WriteLine(string.Format("{0}: can't open {1}", progname, args[file_index])); Console.WriteLine(e.Message); return; } } else { Console.WriteLine(string.Format("{0}: sorry, can't read file from console")); return; } /* Open the output file. */ FileStream output_file = null; if (outfilename != null) { try { output_file = new FileStream(outfilename, FileMode.Create); } catch (Exception e) { Console.WriteLine(string.Format("{0}: can't open {1}", progname, args[file_index])); Console.WriteLine(e.Message); return; } } else { Console.WriteLine(string.Format("{0}: sorry, can't write file to console")); return; } /* Figure out the input file format, and set up to read it. */ cjpeg_source_struct src_mgr = new bmp_source_struct(cinfo); src_mgr.input_file = input_file; /* Read the input file header to obtain file size & colorspace. */ src_mgr.start_input(); /* Now that we know input colorspace, fix colorspace-dependent defaults */ cinfo.jpeg_default_colorspace(); /* Adjust default compression parameters by re-parsing the options */ parse_switches(cinfo, args, true, out file_index); /* Specify data destination for compression */ cinfo.jpeg_stdio_dest(output_file); /* Start compressor */ cinfo.jpeg_start_compress(true); /* Process data */ while (cinfo.Next_scanline < cinfo.Image_height) { int num_scanlines = src_mgr.get_pixel_rows(); cinfo.jpeg_write_scanlines(src_mgr.buffer, num_scanlines); } /* Finish compression and release memory */ src_mgr.finish_input(); cinfo.jpeg_finish_compress(); /* Close files, if we opened them */ input_file.Close(); input_file.Dispose(); output_file.Close(); output_file.Dispose(); /* All done. */ if (cinfo.Err.Num_warnings != 0) Console.WriteLine("Corrupt-data warning count is not zero"); }