// Emit a restart marker & resynchronize predictions. static bool emit_restart(ref working_state_ls state, int restart_num) { if (!flush_bits(ref state)) { return(false); } //was emit_byte(state, 0xFF, return false); state.output_bytes[state.next_output_byte++] = 0xFF; state.free_in_buffer--; if (state.free_in_buffer == 0) { if (!dump_buffer(ref state)) { return(false); } } //was emit_byte(state, JPEG_RST0+restart_num, return false); state.output_bytes[state.next_output_byte++] = (byte)(JPEG_RST0 + restart_num); state.free_in_buffer--; if (state.free_in_buffer == 0) { if (!dump_buffer(ref state)) { return(false); } } // The restart counter is not updated until we successfully write the MCU. return(true); }
static bool flush_bits(ref working_state_ls state) { if (!emit_bits(ref state, 0x7F, 7)) { return(false); // fill any partial byte with ones } state.cur.put_buffer = 0; // and reset bit-buffer to empty state.cur.put_bits = 0; return(true); }
// Outputting bits to the file // Only the right 24 bits of put_buffer are used; the valid bits are // left-justified in this part. At most 16 bits can be passed to emit_bits // in one call, and we never retain more than 7 bits in put_buffer // between calls, so 24 bits are sufficient. // Emit some bits; return true if successful, false if must suspend static bool emit_bits(ref working_state_ls state, uint code, int size) { // This routine is heavily used, so it's worth coding tightly. int put_buffer = (int)code; int put_bits = state.cur.put_bits; // if size is 0, caller used an invalid Huffman table entry if (size == 0) { ERREXIT(state.cinfo, J_MESSAGE_CODE.JERR_HUFF_MISSING_CODE); } put_buffer &= (1 << size) - 1; // mask off any extra bits in code put_bits += size; // new number of bits in buffer put_buffer <<= 24 - put_bits; // align incoming bits put_buffer |= state.cur.put_buffer; // and merge with old buffer contents while (put_bits >= 8) { byte c = (byte)((put_buffer >> 16) & 0xFF); //was emit_byte(state, c, return false); state.output_bytes[state.next_output_byte++] = c; state.free_in_buffer--; if (state.free_in_buffer == 0) { if (!dump_buffer(ref state)) { return(false); } } if (c == 0xFF) { // need to stuff a zero byte? //was emit_byte(state, 0, return false); state.output_bytes[state.next_output_byte++] = 0; state.free_in_buffer--; if (state.free_in_buffer == 0) { if (!dump_buffer(ref state)) { return(false); } } } put_buffer <<= 8; put_bits -= 8; } state.cur.put_buffer = put_buffer; // update state variables state.cur.put_bits = put_bits; return(true); }
// Outputting bytes to the file // Empty the output buffer; return true if successful, false if must suspend static bool dump_buffer(ref working_state_ls state) { jpeg_destination_mgr dest = state.cinfo.dest; if (!dest.empty_output_buffer(state.cinfo)) { return(false); } // After a successful buffer dump, must reset buffer pointers state.output_bytes = dest.output_bytes; state.next_output_byte = dest.next_output_byte; state.free_in_buffer = dest.free_in_buffer; return(true); }
// Emit a restart marker & resynchronize predictions. static bool emit_restart(ref working_state_ls state, int restart_num) { if(!flush_bits(ref state)) return false; //was emit_byte(state, 0xFF, return false); state.output_bytes[state.next_output_byte++]=0xFF; state.free_in_buffer--; if(state.free_in_buffer==0) { if(!dump_buffer(ref state)) return false; } //was emit_byte(state, JPEG_RST0+restart_num, return false); state.output_bytes[state.next_output_byte++]=(byte)(JPEG_RST0+restart_num); state.free_in_buffer--; if(state.free_in_buffer==0) { if(!dump_buffer(ref state)) return false; } // The restart counter is not updated until we successfully write the MCU. return true; }
static bool flush_bits(ref working_state_ls state) { if(!emit_bits(ref state, 0x7F, 7)) return false; // fill any partial byte with ones state.cur.put_buffer=0; // and reset bit-buffer to empty state.cur.put_bits=0; return true; }
// Outputting bits to the file // Only the right 24 bits of put_buffer are used; the valid bits are // left-justified in this part. At most 16 bits can be passed to emit_bits // in one call, and we never retain more than 7 bits in put_buffer // between calls, so 24 bits are sufficient. // Emit some bits; return true if successful, false if must suspend static bool emit_bits(ref working_state_ls state, uint code, int size) { // This routine is heavily used, so it's worth coding tightly. int put_buffer=(int)code; int put_bits=state.cur.put_bits; // if size is 0, caller used an invalid Huffman table entry if(size==0) ERREXIT(state.cinfo, J_MESSAGE_CODE.JERR_HUFF_MISSING_CODE); put_buffer&=(1<<size)-1; // mask off any extra bits in code put_bits+=size; // new number of bits in buffer put_buffer<<=24-put_bits; // align incoming bits put_buffer|=state.cur.put_buffer; // and merge with old buffer contents while(put_bits>=8) { byte c=(byte)((put_buffer>>16)&0xFF); //was emit_byte(state, c, return false); state.output_bytes[state.next_output_byte++]=c; state.free_in_buffer--; if(state.free_in_buffer==0) { if(!dump_buffer(ref state)) return false; } if(c==0xFF) { // need to stuff a zero byte? //was emit_byte(state, 0, return false); state.output_bytes[state.next_output_byte++]=0; state.free_in_buffer--; if(state.free_in_buffer==0) { if(!dump_buffer(ref state)) return false; } } put_buffer<<=8; put_bits-=8; } state.cur.put_buffer=put_buffer; // update state variables state.cur.put_bits=put_bits; return true; }
// Outputting bytes to the file // Empty the output buffer; return true if successful, false if must suspend static bool dump_buffer(ref working_state_ls state) { jpeg_destination_mgr dest=state.cinfo.dest; if(!dest.empty_output_buffer(state.cinfo)) return false; // After a successful buffer dump, must reset buffer pointers state.output_bytes=dest.output_bytes; state.next_output_byte=dest.next_output_byte; state.free_in_buffer=dest.free_in_buffer; return true; }