Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * jcapistd.c |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1994-1996, Thomas G. Lane. |
michael@0 | 5 | * This file is part of the Independent JPEG Group's software. |
michael@0 | 6 | * For conditions of distribution and use, see the accompanying README file. |
michael@0 | 7 | * |
michael@0 | 8 | * This file contains application interface code for the compression half |
michael@0 | 9 | * of the JPEG library. These are the "standard" API routines that are |
michael@0 | 10 | * used in the normal full-compression case. They are not used by a |
michael@0 | 11 | * transcoding-only application. Note that if an application links in |
michael@0 | 12 | * jpeg_start_compress, it will end up linking in the entire compressor. |
michael@0 | 13 | * We thus must separate this file from jcapimin.c to avoid linking the |
michael@0 | 14 | * whole compression library into a transcoder. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #define JPEG_INTERNALS |
michael@0 | 18 | #include "jinclude.h" |
michael@0 | 19 | #include "jpeglib.h" |
michael@0 | 20 | |
michael@0 | 21 | |
michael@0 | 22 | /* |
michael@0 | 23 | * Compression initialization. |
michael@0 | 24 | * Before calling this, all parameters and a data destination must be set up. |
michael@0 | 25 | * |
michael@0 | 26 | * We require a write_all_tables parameter as a failsafe check when writing |
michael@0 | 27 | * multiple datastreams from the same compression object. Since prior runs |
michael@0 | 28 | * will have left all the tables marked sent_table=TRUE, a subsequent run |
michael@0 | 29 | * would emit an abbreviated stream (no tables) by default. This may be what |
michael@0 | 30 | * is wanted, but for safety's sake it should not be the default behavior: |
michael@0 | 31 | * programmers should have to make a deliberate choice to emit abbreviated |
michael@0 | 32 | * images. Therefore the documentation and examples should encourage people |
michael@0 | 33 | * to pass write_all_tables=TRUE; then it will take active thought to do the |
michael@0 | 34 | * wrong thing. |
michael@0 | 35 | */ |
michael@0 | 36 | |
michael@0 | 37 | GLOBAL(void) |
michael@0 | 38 | jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables) |
michael@0 | 39 | { |
michael@0 | 40 | if (cinfo->global_state != CSTATE_START) |
michael@0 | 41 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 42 | |
michael@0 | 43 | if (write_all_tables) |
michael@0 | 44 | jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */ |
michael@0 | 45 | |
michael@0 | 46 | /* (Re)initialize error mgr and destination modules */ |
michael@0 | 47 | (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); |
michael@0 | 48 | (*cinfo->dest->init_destination) (cinfo); |
michael@0 | 49 | /* Perform master selection of active modules */ |
michael@0 | 50 | jinit_compress_master(cinfo); |
michael@0 | 51 | /* Set up for the first pass */ |
michael@0 | 52 | (*cinfo->master->prepare_for_pass) (cinfo); |
michael@0 | 53 | /* Ready for application to drive first pass through jpeg_write_scanlines |
michael@0 | 54 | * or jpeg_write_raw_data. |
michael@0 | 55 | */ |
michael@0 | 56 | cinfo->next_scanline = 0; |
michael@0 | 57 | cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | /* |
michael@0 | 62 | * Write some scanlines of data to the JPEG compressor. |
michael@0 | 63 | * |
michael@0 | 64 | * The return value will be the number of lines actually written. |
michael@0 | 65 | * This should be less than the supplied num_lines only in case that |
michael@0 | 66 | * the data destination module has requested suspension of the compressor, |
michael@0 | 67 | * or if more than image_height scanlines are passed in. |
michael@0 | 68 | * |
michael@0 | 69 | * Note: we warn about excess calls to jpeg_write_scanlines() since |
michael@0 | 70 | * this likely signals an application programmer error. However, |
michael@0 | 71 | * excess scanlines passed in the last valid call are *silently* ignored, |
michael@0 | 72 | * so that the application need not adjust num_lines for end-of-image |
michael@0 | 73 | * when using a multiple-scanline buffer. |
michael@0 | 74 | */ |
michael@0 | 75 | |
michael@0 | 76 | GLOBAL(JDIMENSION) |
michael@0 | 77 | jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines, |
michael@0 | 78 | JDIMENSION num_lines) |
michael@0 | 79 | { |
michael@0 | 80 | JDIMENSION row_ctr, rows_left; |
michael@0 | 81 | |
michael@0 | 82 | if (cinfo->global_state != CSTATE_SCANNING) |
michael@0 | 83 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 84 | if (cinfo->next_scanline >= cinfo->image_height) |
michael@0 | 85 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); |
michael@0 | 86 | |
michael@0 | 87 | /* Call progress monitor hook if present */ |
michael@0 | 88 | if (cinfo->progress != NULL) { |
michael@0 | 89 | cinfo->progress->pass_counter = (long) cinfo->next_scanline; |
michael@0 | 90 | cinfo->progress->pass_limit = (long) cinfo->image_height; |
michael@0 | 91 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /* Give master control module another chance if this is first call to |
michael@0 | 95 | * jpeg_write_scanlines. This lets output of the frame/scan headers be |
michael@0 | 96 | * delayed so that application can write COM, etc, markers between |
michael@0 | 97 | * jpeg_start_compress and jpeg_write_scanlines. |
michael@0 | 98 | */ |
michael@0 | 99 | if (cinfo->master->call_pass_startup) |
michael@0 | 100 | (*cinfo->master->pass_startup) (cinfo); |
michael@0 | 101 | |
michael@0 | 102 | /* Ignore any extra scanlines at bottom of image. */ |
michael@0 | 103 | rows_left = cinfo->image_height - cinfo->next_scanline; |
michael@0 | 104 | if (num_lines > rows_left) |
michael@0 | 105 | num_lines = rows_left; |
michael@0 | 106 | |
michael@0 | 107 | row_ctr = 0; |
michael@0 | 108 | (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines); |
michael@0 | 109 | cinfo->next_scanline += row_ctr; |
michael@0 | 110 | return row_ctr; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | |
michael@0 | 114 | /* |
michael@0 | 115 | * Alternate entry point to write raw data. |
michael@0 | 116 | * Processes exactly one iMCU row per call, unless suspended. |
michael@0 | 117 | */ |
michael@0 | 118 | |
michael@0 | 119 | GLOBAL(JDIMENSION) |
michael@0 | 120 | jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data, |
michael@0 | 121 | JDIMENSION num_lines) |
michael@0 | 122 | { |
michael@0 | 123 | JDIMENSION lines_per_iMCU_row; |
michael@0 | 124 | |
michael@0 | 125 | if (cinfo->global_state != CSTATE_RAW_OK) |
michael@0 | 126 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 127 | if (cinfo->next_scanline >= cinfo->image_height) { |
michael@0 | 128 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); |
michael@0 | 129 | return 0; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | /* Call progress monitor hook if present */ |
michael@0 | 133 | if (cinfo->progress != NULL) { |
michael@0 | 134 | cinfo->progress->pass_counter = (long) cinfo->next_scanline; |
michael@0 | 135 | cinfo->progress->pass_limit = (long) cinfo->image_height; |
michael@0 | 136 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | /* Give master control module another chance if this is first call to |
michael@0 | 140 | * jpeg_write_raw_data. This lets output of the frame/scan headers be |
michael@0 | 141 | * delayed so that application can write COM, etc, markers between |
michael@0 | 142 | * jpeg_start_compress and jpeg_write_raw_data. |
michael@0 | 143 | */ |
michael@0 | 144 | if (cinfo->master->call_pass_startup) |
michael@0 | 145 | (*cinfo->master->pass_startup) (cinfo); |
michael@0 | 146 | |
michael@0 | 147 | /* Verify that at least one iMCU row has been passed. */ |
michael@0 | 148 | lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE; |
michael@0 | 149 | if (num_lines < lines_per_iMCU_row) |
michael@0 | 150 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
michael@0 | 151 | |
michael@0 | 152 | /* Directly compress the row. */ |
michael@0 | 153 | if (! (*cinfo->coef->compress_data) (cinfo, data)) { |
michael@0 | 154 | /* If compressor did not consume the whole row, suspend processing. */ |
michael@0 | 155 | return 0; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | /* OK, we processed one iMCU row. */ |
michael@0 | 159 | cinfo->next_scanline += lines_per_iMCU_row; |
michael@0 | 160 | return lines_per_iMCU_row; |
michael@0 | 161 | } |