src/gpujpeg_huffman_cpu_encoder.c
static inline int
gpujpeg_huffman_cpu_encoder_emit_bits(struct gpujpeg_huffman_cpu_encoder* coder, unsigned int code, int size)
{
    // This routine is heavily used, so it's worth coding tightly
    int put_buffer = (int)code;
    int put_bits = coder->put_bits;
    // If size is 0, caller used an invalid Huffman table entry
    if ( size == 0 )
        return -1;
    // Mask off any extra bits in code
    put_buffer &= (((int)1) << size) - 1; 
    // New number of bits in buffer
    put_bits += size;                    
    // Align incoming bits
    put_buffer <<= 24 - put_bits;        
    // And merge with old buffer contents
    put_buffer |= coder->put_value;    
    // If there are more than 8 bits, write it out
    unsigned char uc;
    while ( put_bits >= 8 ) {
        // Write one byte out
        uc = (unsigned char) ((put_buffer >> 16) & 0xFF);
        gpujpeg_writer_emit_byte(coder->writer, uc);
        // If need to stuff a zero byte
        if ( uc == 0xFF ) {  
            // Write zero byte out
            gpujpeg_writer_emit_byte(coder->writer, 0);
        }
        put_buffer <<= 8;
        put_bits -= 8;
    }
    // update state variables
    coder->put_value = put_buffer; 
    coder->put_bits = put_bits;
    return 0;
}
/**
 * Output 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 EmitBits in one call, and we never retain more than 7 bits 
 * in put_buffer between calls, so 24 bits are sufficient.
 * 
 * @param coder  Huffman coder structure
 * @param code  Huffman code
 * @param size  Size in bits of the Huffman code
 * @return void
 */
Output 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 EmitBits in one call, and we never retain more than 7 bits
in put_buffer between calls, so 24 bits are sufficient.

@param coder  Huffman coder structure
@param code  Huffman code
@param size  Size in bits of the Huffman code
@return void
Output 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 EmitBits in one call and we never retain more than 7 bits in put_buffer between calls so 24 bits are sufficient . @param coder Huffman coder structure @param code Huffman code @param size Size in bits of the Huffman code @return void
static inline int
gpujpeg_huffman_cpu_encoder_emit_bits(struct gpujpeg_huffman_cpu_encoder* coder, unsigned int code, int size)
{
    int put_buffer = (int)code;
    int put_bits = coder->put_bits;
    if ( size == 0 )
        return -1;
    put_buffer &= (((int)1) << size) - 1; 
    put_bits += size;                    
    put_buffer <<= 24 - put_bits;        
    put_buffer |= coder->put_value;    
    unsigned char uc;
    while ( put_bits >= 8 ) {
        uc = (unsigned char) ((put_buffer >> 16) & 0xFF);
        gpujpeg_writer_emit_byte(coder->writer, uc);
        if ( uc == 0xFF ) {  
            gpujpeg_writer_emit_byte(coder->writer, 0);
        }
        put_buffer <<= 8;
        put_bits -= 8;
    }
    coder->put_value = put_buffer; 
    coder->put_bits = put_bits;
    return 0;
}
static inline int gpujpeg_huffman_cpu_encoder_emit_bits ( struct gpujpeg_huffman_cpu_encoder * coder , unsigned int code , int size ) { int put_buffer = ( int ) code ; int put_bits = coder -> put_bits ; if ( size == 0 ) return -1 ; put_buffer &= ( ( ( int ) 1 ) << size ) - 1 ; put_bits += size ; put_buffer <<= 24 - put_bits ; put_buffer |= coder -> put_value ; unsigned char uc ; while ( put_bits >= 8 ) { uc = ( unsigned char ) ( ( put_buffer >> 16 ) & 0xFF ) ; gpujpeg_writer_emit_byte ( coder -> writer , uc ) ; if ( uc == 0xFF ) { gpujpeg_writer_emit_byte ( coder -> writer , 0 ) ; } put_buffer <<= 8 ; put_bits -= 8 ; } coder -> put_value = put_buffer ; coder -> put_bits = put_bits ; return 0 ; }
Output bits to the file.
Output bits to the file .
// This routine is heavily used, so it's worth coding tightly // If size is 0, caller used an invalid Huffman table entry // Mask off any extra bits in code // New number of bits in buffer // Align incoming bits // And merge with old buffer contents // If there are more than 8 bits, write it out // Write one byte out // If need to stuff a zero byte // Write zero byte out // update state variables