         return;
     
     struct gpujpeg_segment* segment = &d_segment[segment_index];
+    const int segment_mcu_count = segment->mcu_count;
     
     // Initialize huffman coder
     int dc[GPUJPEG_MAX_COMPONENT_COUNT];

            d_table_dc = d_table_cbcr_dc;
            d_table_ac = d_table_cbcr_ac;
        }
            
        // Encode MCUs in segment
        for ( int mcu_index = 0; mcu_index < segment->mcu_count; mcu_index++ ) {
            // Encode 8x8 block
            if (gpujpeg_huffman_gpu_encoder_encode_block(block, data_compressed, s_out, remaining_codewords, &last_dc, tid, d_table_dc, d_table_ac) != 0)
                break;
            block += component->mcu_size;
        }
    }
    // Interleaving mode
    else {
        int segment_index = segment->scan_segment_index; //TODO asi nepotrebne
        // Encode MCUs in segment
        for ( int mcu_index = 0; mcu_index < segment->mcu_count; mcu_index++ ) {
            //assert(segment->scan_index == 0);
            for ( int comp = 0; comp < comp_count; comp++ ) {
                struct gpujpeg_component* component = &d_component[comp];

        struct gpujpeg_component* component = &d_component[segment->scan_index];

        // Get component data for MCU (first block)
        const int16_t* block = &component->d_data_quantized[(segment_index * component->segment_mcu_count) * component->mcu_size];

        // Get coder parameters
        int & last_dc = dc[segment->scan_index];
