src/gpujpeg_huffman_cpu_encoder.c
int
gpujpeg_huffman_cpu_encoder_encode_mcu(struct gpujpeg_huffman_cpu_encoder* coder, int segment_index, int mcu_index)
{
    // Non-interleaving mode
    if ( coder->comp_count == 1 ) {
        // Get component for current scan
        struct gpujpeg_component* component = &coder->component[coder->scan_index];
 
        // Get component data for MCU
        int16_t* block = &component->data_quantized[(segment_index * component->segment_mcu_count + mcu_index) * component->mcu_size];
        
        // Get coder parameters
        int* dc = &coder->dc[coder->scan_index];
        struct gpujpeg_table_huffman_encoder* table_dc = coder->table_dc[component->type];
        struct gpujpeg_table_huffman_encoder* table_ac = coder->table_ac[component->type];
        
        // Encode 8x8 block
        if ( gpujpeg_huffman_cpu_encoder_encode_block(coder, block, dc, table_dc, table_ac) != 0 )
            return -1;
    } 
    // Interleaving mode
    else {
        assert(coder->scan_index == 0);
        for ( int comp = 0; comp < coder->comp_count; comp++ ) {
            struct gpujpeg_component* component = &coder->component[comp];

            // Prepare mcu indexes
            int mcu_index_x = (segment_index * component->segment_mcu_count + mcu_index) % component->mcu_count_x;
            int mcu_index_y = (segment_index * component->segment_mcu_count + mcu_index) / component->mcu_count_x;
            // Compute base data index
            int data_index_base = mcu_index_y * (component->mcu_size * component->mcu_count_x) + mcu_index_x * (component->mcu_size_x * GPUJPEG_BLOCK_SIZE);
            
            // For all vertical 8x8 blocks
            for ( int y = 0; y < component->sampling_factor.vertical; y++ ) {
                // Compute base row data index
                assert((component->mcu_count_x * component->mcu_size_x) == component->data_width);
                int data_index_row = data_index_base + y * (component->mcu_count_x * component->mcu_size_x * GPUJPEG_BLOCK_SIZE);
                // For all horizontal 8x8 blocks
                for ( int x = 0; x < component->sampling_factor.horizontal; x++ ) {
                    // Compute 8x8 block data index
                    int data_index = data_index_row + x * GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE;
                    
                    // Get component data for MCU
                    int16_t* block = &component->data_quantized[data_index];
                    
                    // Get coder parameters
                    int* dc = &coder->dc[comp];
                    struct gpujpeg_table_huffman_encoder* table_dc = coder->table_dc[component->type];
                    struct gpujpeg_table_huffman_encoder* table_ac = coder->table_ac[component->type];
                    
                    // Encode 8x8 block
                    if ( gpujpeg_huffman_cpu_encoder_encode_block(coder, block, dc, table_dc, table_ac) != 0 )
                        return -1;
                }
            }
        }
    }

    return 0;
}
/**
 * Encode one MCU
 *
 * @return 0 if succeeds, otherwise nonzero
 */
Encode one MCU
@return 0 if succeeds, otherwise nonzero
Encode one MCU @return 0 if succeeds otherwise nonzero
int
gpujpeg_huffman_cpu_encoder_encode_mcu(struct gpujpeg_huffman_cpu_encoder* coder, int segment_index, int mcu_index)
{
    if ( coder->comp_count == 1 ) {
        struct gpujpeg_component* component = &coder->component[coder->scan_index];
        int16_t* block = &component->data_quantized[(segment_index * component->segment_mcu_count + mcu_index) * component->mcu_size];
        int* dc = &coder->dc[coder->scan_index];
        struct gpujpeg_table_huffman_encoder* table_dc = coder->table_dc[component->type];
        struct gpujpeg_table_huffman_encoder* table_ac = coder->table_ac[component->type];
        if ( gpujpeg_huffman_cpu_encoder_encode_block(coder, block, dc, table_dc, table_ac) != 0 )
            return -1;
    } 
    else {
        assert(coder->scan_index == 0);
        for ( int comp = 0; comp < coder->comp_count; comp++ ) {
            struct gpujpeg_component* component = &coder->component[comp];
            int mcu_index_x = (segment_index * component->segment_mcu_count + mcu_index) % component->mcu_count_x;
            int mcu_index_y = (segment_index * component->segment_mcu_count + mcu_index) / component->mcu_count_x;
            int data_index_base = mcu_index_y * (component->mcu_size * component->mcu_count_x) + mcu_index_x * (component->mcu_size_x * GPUJPEG_BLOCK_SIZE);
            for ( int y = 0; y < component->sampling_factor.vertical; y++ ) {
                assert((component->mcu_count_x * component->mcu_size_x) == component->data_width);
                int data_index_row = data_index_base + y * (component->mcu_count_x * component->mcu_size_x * GPUJPEG_BLOCK_SIZE);
                for ( int x = 0; x < component->sampling_factor.horizontal; x++ ) {
                    int data_index = data_index_row + x * GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE;
                    int16_t* block = &component->data_quantized[data_index];
                    int* dc = &coder->dc[comp];
                    struct gpujpeg_table_huffman_encoder* table_dc = coder->table_dc[component->type];
                    struct gpujpeg_table_huffman_encoder* table_ac = coder->table_ac[component->type];
                    if ( gpujpeg_huffman_cpu_encoder_encode_block(coder, block, dc, table_dc, table_ac) != 0 )
                        return -1;
                }
            }
        }
    }
    return 0;
}
int gpujpeg_huffman_cpu_encoder_encode_mcu ( struct gpujpeg_huffman_cpu_encoder * coder , int segment_index , int mcu_index ) { if ( coder -> comp_count == 1 ) { struct gpujpeg_component * component = & coder -> component [ coder -> scan_index ] ; int16_t * block = & component -> data_quantized [ ( segment_index * component -> segment_mcu_count + mcu_index ) * component -> mcu_size ] ; int * dc = & coder -> dc [ coder -> scan_index ] ; struct gpujpeg_table_huffman_encoder * table_dc = coder -> table_dc [ component -> type ] ; struct gpujpeg_table_huffman_encoder * table_ac = coder -> table_ac [ component -> type ] ; if ( gpujpeg_huffman_cpu_encoder_encode_block ( coder , block , dc , table_dc , table_ac ) != 0 ) return -1 ; } else { assert ( coder -> scan_index == 0 ) ; for ( int comp = 0 ; comp < coder -> comp_count ; comp ++ ) { struct gpujpeg_component * component = & coder -> component [ comp ] ; int mcu_index_x = ( segment_index * component -> segment_mcu_count + mcu_index ) % component -> mcu_count_x ; int mcu_index_y = ( segment_index * component -> segment_mcu_count + mcu_index ) / component -> mcu_count_x ; int data_index_base = mcu_index_y * ( component -> mcu_size * component -> mcu_count_x ) + mcu_index_x * ( component -> mcu_size_x * GPUJPEG_BLOCK_SIZE ) ; for ( int y = 0 ; y < component -> sampling_factor . vertical ; y ++ ) { assert ( ( component -> mcu_count_x * component -> mcu_size_x ) == component -> data_width ) ; int data_index_row = data_index_base + y * ( component -> mcu_count_x * component -> mcu_size_x * GPUJPEG_BLOCK_SIZE ) ; for ( int x = 0 ; x < component -> sampling_factor . horizontal ; x ++ ) { int data_index = data_index_row + x * GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE ; int16_t * block = & component -> data_quantized [ data_index ] ; int * dc = & coder -> dc [ comp ] ; struct gpujpeg_table_huffman_encoder * table_dc = coder -> table_dc [ component -> type ] ; struct gpujpeg_table_huffman_encoder * table_ac = coder -> table_ac [ component -> type ] ; if ( gpujpeg_huffman_cpu_encoder_encode_block ( coder , block , dc , table_dc , table_ac ) != 0 ) return -1 ; } } } } return 0 ; }
Encode one MCU
 @return 0 if succeeds, otherwise nonzero
Encode one MCU @return 0 if succeeds otherwise nonzero
// Non-interleaving mode // Get component for current scan // Get component data for MCU // Get coder parameters // Encode 8x8 block // Interleaving mode // Prepare mcu indexes // Compute base data index // For all vertical 8x8 blocks // Compute base row data index // For all horizontal 8x8 blocks // Compute 8x8 block data index // Get component data for MCU // Get coder parameters // Encode 8x8 block