     // Color transform
     gpujpeg_color_transform<color_space, color_space_internal>::perform(r1, r2, r3);
     
+    // Position
+    int image_position = gX + x;
+    int image_position_x = image_position % image_width;
+    int image_position_y = image_position / image_width;
+    
     // Store
-    if ( image_position_x < image_width ) {
+    if ( image_position < (image_width * image_height) ) {
         gpujpeg_preprocessor_raw_to_comp_store<s_comp1_samp_factor_h, s_comp1_samp_factor_v>(r1, image_position_x, image_position_y, data.comp[0]);
         gpujpeg_preprocessor_raw_to_comp_store<s_comp2_samp_factor_h, s_comp2_samp_factor_v>(r2, image_position_x, image_position_y, data.comp[1]);
         gpujpeg_preprocessor_raw_to_comp_store<s_comp3_samp_factor_h, s_comp3_samp_factor_v>(r3, image_position_x, image_position_y, data.comp[2]);

    uint8_t r1;
    uint8_t r2 = s_data[offset + 1];
    uint8_t r3;
    if ( (pix_offset + x) % 2 == 0 ) {
        r1 = s_data[offset];
        r3 = s_data[offset + 2];
    } else {

    assert(coder->param_image.comp_count == 3);

    cudaMemset(coder->d_data, 0, coder->data_size * sizeof(uint8_t));
    
    // Select kernel
    gpujpeg_preprocessor_encode_kernel kernel = (gpujpeg_preprocessor_encode_kernel)coder->preprocessor;
    assert(kernel != NULL);
        
    int image_width = coder->param_image.width;
    int image_height = coder->param_image.height;
    
