[Libav-user] Error loading TGA file

Julien Quilez Akend at hotmail.fr
Fri Mar 17 11:56:01 EET 2017


Hi,

I have a problem when I try to load a TGA picture.

My Code :
[code]
AVStream*    MainWindow::openTgaImage(std::string path)
{
    ctx = avformat_alloc_context();
    av_register_all();
    AVDictionary* options = NULL;

    int read = avformat_open_input(&ctx, path.c_str(), NULL, &options);

    if (read >= 0 && avformat_find_stream_info(ctx, &options) >= 0)
    {
        AVStream* stream = NULL;

        for (size_t i = 0; i < ctx->nb_streams; ++i)
        {
            AVStream*    currentStream = ctx->streams[i];

            if (currentStream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && currentStream->codecpar->codec_id == AV_CODEC_ID_TARGA)
                return currentStream;
        }
    }

    return NULL;
}

QImage    MainWindow::parseTgaImage(std::string path)
{
    QImage        ret;
    AVStream*    stream = openTgaImage(path);

    if (stream)
    {
        AVPacket packet;

        av_init_packet(&packet);
        packet.data = NULL;
        packet.size = 0;

        while (av_read_frame(ctx, &packet) >= 0)
        {
            if (packet.stream_index == stream->index)
            {

                  AVPicture frame;

                //  avpicture_alloc(&frame, stream->codec->pix_fmt, stream->codec->width, stream->codec->height);
                //  avpicture_fill(&frame, packet.data, stream->codec->pix_fmt, stream->codec->width, stream->codec->height);

                  QImage    img(stream->codec->width, stream->codec->height, QImage::Format_ARGB32);
                  int        offset = 18; // I need to set offset to 18 because the buffer is shifted.

                  for (size_t y = 0; y < stream->codec->height; ++y)
                  {
                    for (size_t x = 0; x < stream->codec->width; ++x)
                    {
                        QColor color;

                        color.setBlue(packet.data[offset + 0]);
                        color.setGreen(packet.data[offset + 1]);
                        color.setRed(packet.data[offset + 2]);
                        color.setAlpha(packet.data[offset + 3]);
                        img.setPixel(x, y, color.rgba());
                        offset += 4;
                    }
                  }

                  if (!img.isNull())
                      ret = img;

                  avpicture_free(&frame);
            }
        }

        av_free_packet(&packet);
    }

    avformat_close_input(&ctx);

    return ret;
}
[/code]

My problem:
I set the offset to 18, because without this offset, my picture have bad colors.
The packet->size is not good (packet->size == (width * height * 4 (4 for RGBA) + 44)
Why my packet->size has 44 bytes at the end?
Why my picture is shifted and have the bad colors?
If I set my offset to 18, so I start to read to packet->buffer[18], my picture isn't shifted and have the good colors. Why?

Thanks you for you help,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170317/e3824c60/attachment.html>


More information about the Libav-user mailing list