src/gpujpeg_huffman_cpu_encoder.c
int
gpujpeg_huffman_cpu_encoder_encode_block(struct gpujpeg_huffman_cpu_encoder* coder, int16_t* block, int* dc, struct gpujpeg_table_huffman_encoder* table_dc, struct gpujpeg_table_huffman_encoder* table_ac)
{
    /*printf("Encode block\n");
    for ( int y = 0; y < 8; y++) {
        for ( int x = 0; x < 8; x++ ) {
            printf("%4d ", block[y * 8 + x]);
        }
        printf("\n");
    }*/

    // Encode the DC coefficient difference per section F.1.2.1
    int temp = block[0] - *dc;
    *dc = block[0];

    int temp2 = temp;
    if ( temp < 0 ) {
        // Temp is abs value of input
        temp = -temp;
        // For a negative input, want temp2 = bitwise complement of abs(input)
        // This code assumes we are on a two's complement machine
        temp2--;
    }

    // Find the number of bits needed for the magnitude of the coefficient
    int nbits = 0;
    while ( temp ) {
        nbits++;
        temp >>= 1;
    }

    //    Write category number
    if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_dc->code[nbits], table_dc->size[nbits]) != 0 ) {
        fprintf(stderr, "[GPUJPEG] [Error] Fail emit bits %d [code: %d, size: %d]!\n", nbits, table_dc->code[nbits], table_dc->size[nbits]);
        return -1;
    }

    //    Write category offset (EmitBits rejects calls with size 0)
    if ( nbits ) {
        if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, (unsigned int) temp2, nbits) != 0 )
            return -1;
    }
    
    // Encode the AC coefficients per section F.1.2.2 (r = run length of zeros)
    int r = 0;
    for ( int k = 1; k < 64; k++ ) 
    {
        if ( (temp = block[gpujpeg_order_natural[k]]) == 0 ) {
            r++;
        } 
        else {
            // If run length > 15, must emit special run-length-16 codes (0xF0)
            while ( r > 15 ) {
                if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_ac->code[0xF0], table_ac->size[0xF0]) != 0 )
                    return -1;
                r -= 16;
            }

            temp2 = temp;
            if ( temp < 0 ) {
                // temp is abs value of input
                temp = -temp;        
                // This code assumes we are on a two's complement machine
                temp2--;
            }

            // Find the number of bits needed for the magnitude of the coefficient
            // there must be at least one 1 bit
            nbits = 1;
            while ( (temp >>= 1) )
                nbits++;

            // Emit Huffman symbol for run length / number of bits
            int i = (r << 4) + nbits;
            if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_ac->code[i], table_ac->size[i]) != 0 )
                return -1;

            // Write Category offset
            if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, (unsigned int) temp2, nbits) != 0 )
                return -1;

            r = 0;
        }
    }

    // If all the left coefs were zero, emit an end-of-block code
    if ( r > 0 ) {
        if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_ac->code[0], table_ac->size[0]) != 0 )
            return -1;
    }

    return 0;
}
/**
 * Encode one 8x8 block
 *
 * @return 0 if succeeds, otherwise nonzero
 */
Encode one 8x8 block
@return 0 if succeeds, otherwise nonzero
Encode one 8x8 block @return 0 if succeeds otherwise nonzero
int
gpujpeg_huffman_cpu_encoder_encode_block(struct gpujpeg_huffman_cpu_encoder* coder, int16_t* block, int* dc, struct gpujpeg_table_huffman_encoder* table_dc, struct gpujpeg_table_huffman_encoder* table_ac)
{
    int temp = block[0] - *dc;
    *dc = block[0];
    int temp2 = temp;
    if ( temp < 0 ) {
        temp = -temp;
        temp2--;
    }
    int nbits = 0;
    while ( temp ) {
        nbits++;
        temp >>= 1;
    }
    if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_dc->code[nbits], table_dc->size[nbits]) != 0 ) {
        fprintf(stderr, "[GPUJPEG] [Error] Fail emit bits %d [code: %d, size: %d]!\n", nbits, table_dc->code[nbits], table_dc->size[nbits]);
        return -1;
    }
    if ( nbits ) {
        if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, (unsigned int) temp2, nbits) != 0 )
            return -1;
    }
    int r = 0;
    for ( int k = 1; k < 64; k++ ) 
    {
        if ( (temp = block[gpujpeg_order_natural[k]]) == 0 ) {
            r++;
        } 
        else {
            while ( r > 15 ) {
                if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_ac->code[0xF0], table_ac->size[0xF0]) != 0 )
                    return -1;
                r -= 16;
            }
            temp2 = temp;
            if ( temp < 0 ) {
                temp = -temp;        
                temp2--;
            }
            nbits = 1;
            while ( (temp >>= 1) )
                nbits++;
            int i = (r << 4) + nbits;
            if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_ac->code[i], table_ac->size[i]) != 0 )
                return -1;
            if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, (unsigned int) temp2, nbits) != 0 )
                return -1;
            r = 0;
        }
    }
    if ( r > 0 ) {
        if ( gpujpeg_huffman_cpu_encoder_emit_bits(coder, table_ac->code[0], table_ac->size[0]) != 0 )
            return -1;
    }
    return 0;
}
int gpujpeg_huffman_cpu_encoder_encode_block ( struct gpujpeg_huffman_cpu_encoder * coder , int16_t * block , int * dc , struct gpujpeg_table_huffman_encoder * table_dc , struct gpujpeg_table_huffman_encoder * table_ac ) { int temp = block [ 0 ] - * dc ; * dc = block [ 0 ] ; int temp2 = temp ; if ( temp < 0 ) { temp = - temp ; temp2 -- ; } int nbits = 0 ; while ( temp ) { nbits ++ ; temp >>= 1 ; } if ( gpujpeg_huffman_cpu_encoder_emit_bits ( coder , table_dc -> code [ nbits ] , table_dc -> size [ nbits ] ) != 0 ) { fprintf ( stderr , " \n " , nbits , table_dc -> code [ nbits ] , table_dc -> size [ nbits ] ) ; return -1 ; } if ( nbits ) { if ( gpujpeg_huffman_cpu_encoder_emit_bits ( coder , ( unsigned int ) temp2 , nbits ) != 0 ) return -1 ; } int r = 0 ; for ( int k = 1 ; k < 64 ; k ++ ) { if ( ( temp = block [ gpujpeg_order_natural [ k ] ] ) == 0 ) { r ++ ; } else { while ( r > 15 ) { if ( gpujpeg_huffman_cpu_encoder_emit_bits ( coder , table_ac -> code [ 0xF0 ] , table_ac -> size [ 0xF0 ] ) != 0 ) return -1 ; r -= 16 ; } temp2 = temp ; if ( temp < 0 ) { temp = - temp ; temp2 -- ; } nbits = 1 ; while ( ( temp >>= 1 ) ) nbits ++ ; int i = ( r << 4 ) + nbits ; if ( gpujpeg_huffman_cpu_encoder_emit_bits ( coder , table_ac -> code [ i ] , table_ac -> size [ i ] ) != 0 ) return -1 ; if ( gpujpeg_huffman_cpu_encoder_emit_bits ( coder , ( unsigned int ) temp2 , nbits ) != 0 ) return -1 ; r = 0 ; } } if ( r > 0 ) { if ( gpujpeg_huffman_cpu_encoder_emit_bits ( coder , table_ac -> code [ 0 ] , table_ac -> size [ 0 ] ) != 0 ) return -1 ; } return 0 ; }
Encode one 8x8 block
 @return 0 if succeeds, otherwise nonzero
Encode one 8x8 block @return 0 if succeeds otherwise nonzero
/*printf("Encode block\n");
    for ( int y = 0; y < 8; y++) {
        for ( int x = 0; x < 8; x++ ) {
            printf("%4d ", block[y * 8 + x]);
        }
        printf("\n");
    }*/ // Encode the DC coefficient difference per section F.1.2.1 // Temp is abs value of input // For a negative input, want temp2 = bitwise complement of abs(input) // This code assumes we are on a two's complement machine // Find the number of bits needed for the magnitude of the coefficient //    Write category number //    Write category offset (EmitBits rejects calls with size 0) // Encode the AC coefficients per section F.1.2.2 (r = run length of zeros) // If run length > 15, must emit special run-length-16 codes (0xF0) // temp is abs value of input // This code assumes we are on a two's complement machine // Find the number of bits needed for the magnitude of the coefficient // there must be at least one 1 bit // Emit Huffman symbol for run length / number of bits // Write Category offset // If all the left coefs were zero, emit an end-of-block code