                 // 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);
                 
+                // Get huffman tables
+                struct gpujpeg_table_huffman_encoder* d_table_dc = NULL;
+                struct gpujpeg_table_huffman_encoder* d_table_ac = NULL;
+                if ( component->type == GPUJPEG_COMPONENT_LUMINANCE ) {
+                    d_table_dc = d_table_y_dc;
+                    d_table_ac = d_table_y_ac;
+                } else {
+                    d_table_dc = d_table_cbcr_dc;
+                    d_table_ac = d_table_cbcr_ac;
+                }
+                
+                // Get sampling faactors
+                const int comp_vertical_sampling = component->sampling_factor.vertical;
+                const int comp_horizontal_sampling = component->sampling_factor.horizontal;
+                
+                // Pointer to quantized data of the component
+                const int16_t* const d_component_data_quantized = component->d_data_quantized;
+                
+                // Size and count of MCUs in component
+                const int comp_mcu_stride = component->mcu_count_x * component->mcu_size_x * GPUJPEG_BLOCK_SIZE;
+                
                 // For all vertical 8x8 blocks
-                for ( int y = 0; y < component->sampling_factor.vertical; y++ ) {
+                for ( int y = 0; y < comp_vertical_sampling; y++ ) {
                     // Compute base row data index
-                    int data_index_row = data_index_base + y * (component->mcu_count_x * component->mcu_size_x * GPUJPEG_BLOCK_SIZE);
+                    int data_index_row = data_index_base + y * comp_mcu_stride;
                     // For all horizontal 8x8 blocks
-                    for ( int x = 0; x < component->sampling_factor.horizontal; x++ ) {
+                    for ( int x = 0; x < comp_horizontal_sampling; 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->d_data_quantized[data_index];
+                        const int16_t* block = d_component_data_quantized + data_index;
                         
                         // Get coder parameters
                         int & last_dc = dc[comp];
             
-                        // Get huffman tables
-                        struct gpujpeg_table_huffman_encoder* d_table_dc = NULL;
-                        struct gpujpeg_table_huffman_encoder* d_table_ac = NULL;
-                        if ( component->type == GPUJPEG_COMPONENT_LUMINANCE ) {
-                            d_table_dc = d_table_y_dc;
-                            d_table_ac = d_table_y_ac;
-                        } else {
-                            d_table_dc = d_table_cbcr_dc;
-                            d_table_ac = d_table_cbcr_ac;
-                        }
-                        
                         // Encode 8x8 block
                         gpujpeg_huffman_gpu_encoder_encode_block(block, data_compressed, s_out, remaining_codewords, &last_dc, tid, d_table_dc, d_table_ac);
                     }

        return;
    
    struct gpujpeg_segment* segment = &d_segment[segment_index];
    
    // Initialize huffman coder
    int dc[GPUJPEG_MAX_COMPONENT_COUNT];

 * @return 0 if succeeds, otherwise nonzero
 */
__device__ int
gpujpeg_huffman_gpu_encoder_encode_block(const int16_t * block, unsigned int * &data_compressed, unsigned int * const s_out,
                int & remaining_codewords, int *last_dc, int tid,
                struct gpujpeg_table_huffman_encoder* d_table_dc, struct gpujpeg_table_huffman_encoder* d_table_ac)
{
