                                | (code_word >> (code_bit_size - new_bit_count) & ((1 << new_bit_count) - 1));
         
         out_byte_ptr[0] = out_byte;
+        if(out_byte == 0xFF) {
+            out_byte_ptr[1] = 0;
+            buffer_bits += 8;
+        }
         code_bit_size -= new_bit_count;
         buffer_bits += new_bit_count;
     }

                        }
                        
                        // Encode 8x8 block
                        gpujpeg_huffman_gpu_encoder_encode_block(block, data_compressed, s_temp, s_out, remaining_bits, remaining_bytes, &last_dc, tid, d_table_dc, d_table_ac);
                    }
                }
            }
        }
    }

    // Emit left bits
    gpujpeg_huffman_gpu_encoder_emit_left_bits(data_compressed, s_temp, s_out, remaining_bits, remaining_bytes, tid);

    // Output restart marker
    if (tid == 0 ) {
        int restart_marker = GPUJPEG_MARKER_RST0 + (segment->scan_segment_index % 8);
        gpujpeg_huffman_gpu_encoder_marker(data_compressed, restart_marker);
                
        // Set compressed size
        segment->data_compressed_size = data_compressed - data_compressed_start;
    }
    __syncthreads();
}

/** Documented at declaration */
int

        dc[comp] = 0;
    
    // Prepare data pointers
    uint4 * data_compressed = (uint4*)(d_data_compressed + segment->data_compressed_index);
    uint4 * data_compressed_start = data_compressed;
    
    // Non-interleaving mode
    if ( comp_count == 1 ) {
