]> git.sesse.net Git - nageru/blobdiff - futatabi/jpeg_frame_view.cpp
Work around a bogus compilation warning (it claimed the generated copy constructor...
[nageru] / futatabi / jpeg_frame_view.cpp
index 85c708d4d20f9aff8f137d0d3573b489d9a368f5..6ab19482fb93772956036c19175b6ecef36262e3 100644 (file)
@@ -112,6 +112,8 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
        }
        JPEGDestroyer destroy_dinfo(&dinfo);
 
+       jpeg_save_markers(&dinfo, JPEG_APP0 + 1, 0xFFFF);
+
        if (!error_mgr.run([&dinfo, &jpeg] {
                    jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
                    jpeg_read_header(&dinfo, true);
@@ -119,8 +121,6 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
                return get_black_frame();
        }
 
-       jpeg_save_markers(&dinfo, JPEG_APP0 + 1, 0xFFFF);
-
        if (dinfo.num_components != 3) {
                fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
                        dinfo.num_components,
@@ -163,13 +163,15 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
        unsigned chroma_width_blocks = mcu_width_blocks * dinfo.comp_info[1].h_samp_factor;
 
        PBO pbo = global_pbo_pool->alloc_pbo();
+       const size_t chroma_width = dinfo.image_width / frame->chroma_subsampling_x;
+       const size_t chroma_height = dinfo.image_height / frame->chroma_subsampling_y;
        size_t cb_offset = dinfo.image_width * dinfo.image_height;
-       size_t cr_offset = cb_offset + (dinfo.image_width / 2) * dinfo.image_height;
+       size_t cr_offset = cb_offset + chroma_width * chroma_height;
        uint8_t *y_pix = pbo.ptr;
        uint8_t *cb_pix = pbo.ptr + cb_offset;
        uint8_t *cr_pix = pbo.ptr + cr_offset;
        unsigned pitch_y = luma_width_blocks * DCTSIZE;
-       unsigned pitch_chroma = chroma_width_blocks * DCTSIZE * 2;
+       unsigned pitch_chroma = chroma_width_blocks * DCTSIZE;
 
        if (dinfo.marker_list != nullptr &&
            dinfo.marker_list->marker == JPEG_APP0 + 1 &&
@@ -201,13 +203,14 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
        // FIXME: what about resolutions that are not divisible by the block factor?
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo.pbo);
        frame->y = create_texture_2d(frame->width, frame->height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
-       frame->cb = create_texture_2d(frame->width / 2, frame->height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(cb_offset));
-       frame->cr = create_texture_2d(frame->width / 2, frame->height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(cr_offset));
+       frame->cb = create_texture_2d(chroma_width, chroma_height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(cb_offset));
+       frame->cr = create_texture_2d(chroma_width, chroma_height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(cr_offset));
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
-       glFlushMappedNamedBufferRange(pbo.pbo, 0, dinfo.image_width * dinfo.image_height * 2);
+       glFlushMappedNamedBufferRange(pbo.pbo, 0, dinfo.image_width * dinfo.image_height + chroma_width * chroma_height * 2);
        glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT);
        pbo.upload_done = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
+       glFlush();
        frame->uploaded_ui_thread = pbo.upload_done;
        frame->uploaded_interpolation = pbo.upload_done;
        global_pbo_pool->release_pbo(move(pbo));
@@ -409,6 +412,7 @@ void JPEGFrameView::setFrame(shared_ptr<Frame> frame)
        lock_guard<mutex> lock(cache_mu);
        PendingDecode decode;
        decode.frame = std::move(frame);
+       decode.fade_alpha = 0.0f;
        pending_decodes.push_back(decode);
        any_pending_decodes.notify_all();
 }