X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=nageru%2Fimage_input.cpp;h=7b11679e03b22089e1f0508e658e049d4be89662;hb=76deccd25a089f2a5a21bcc6054c75699cf752c3;hp=779f7ec0c77745367e974135d9a19b7639a77789;hpb=e3df5f8d9bdc31df5dcd14e5ac214de70f5234af;p=nageru diff --git a/nageru/image_input.cpp b/nageru/image_input.cpp index 779f7ec..7b11679 100644 --- a/nageru/image_input.cpp +++ b/nageru/image_input.cpp @@ -41,9 +41,14 @@ struct SwsContext; using namespace std; +ImageInput::ImageInput() + : sRGBSwitchingFlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, + GL_UNSIGNED_BYTE, 1280, 720) // Resolution will be overwritten. +{} + ImageInput::ImageInput(const string &filename) - : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, - GL_UNSIGNED_BYTE, 1280, 720), // Resolution will be overwritten. + : sRGBSwitchingFlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, + GL_UNSIGNED_BYTE, 1280, 720), // Resolution will be overwritten. pathname(search_for_file_or_die(filename)), current_image(load_image(filename, pathname)) { @@ -66,12 +71,13 @@ void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, uns // is mostly there to save startup time, not RAM). { lock_guard lock(all_images_lock); + assert(all_images.count(pathname)); if (all_images[pathname] != current_image) { current_image = all_images[pathname]; set_texture_num(*current_image->tex); } } - movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num); + sRGBSwitchingFlatInput::set_gl_state(glsl_program_num, prefix, sampler_num); } shared_ptr ImageInput::load_image(const string &filename, const string &pathname) @@ -120,7 +126,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str()); return nullptr; } - AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); + const AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); if (codec == nullptr) { fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str()); return nullptr; @@ -137,17 +143,14 @@ shared_ptr ImageInput::load_image_raw(const string &pat AVFrameWithDeleter frame = av_frame_alloc_unique(); bool eof = false; do { - AVPacket pkt; - unique_ptr pkt_cleanup( - &pkt, av_packet_unref); - av_init_packet(&pkt); - pkt.data = nullptr; - pkt.size = 0; - if (av_read_frame(format_ctx.get(), &pkt) == 0) { - if (pkt.stream_index != stream_index) { + AVPacketWithDeleter pkt = av_packet_alloc_unique(); + pkt->data = nullptr; + pkt->size = 0; + if (av_read_frame(format_ctx.get(), pkt.get()) == 0) { + if (pkt->stream_index != stream_index) { continue; } - if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) { + if (avcodec_send_packet(codec_ctx.get(), pkt.get()) < 0) { fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str()); return nullptr; } @@ -212,7 +215,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat check_error(); glPixelStorei(GL_UNPACK_ROW_LENGTH, linesizes[0] / 4); check_error(); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, frame->width, frame->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data.get()); + glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, frame->width, frame->height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, image_data.get()); check_error(); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); check_error(); @@ -222,7 +225,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat glBindTexture(GL_TEXTURE_2D, 0); check_error(); - shared_ptr image(new Image{unsigned(frame->width), unsigned(frame->height), RefCountedTexture(new GLuint(tex)), last_modified}); + shared_ptr image(new Image{unsigned(frame->width), unsigned(frame->height), UniqueTexture(new GLuint(tex)), last_modified}); return image; } @@ -288,6 +291,15 @@ void ImageInput::update_thread_func(QSurface *surface) } } +void ImageInput::switch_image(const string &pathname) +{ +#ifndef NDEBUG + lock_guard lock(all_images_lock); + assert(all_images.count(pathname)); +#endif + this->pathname = pathname; +} + void ImageInput::start_update_thread(QSurface *surface) { update_thread = thread(update_thread_func, surface);