Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Image non const getPixelsPtr  (Read 3485 times)

0 Members and 1 Guest are viewing this topic.

danverde

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Image non const getPixelsPtr
« on: June 29, 2015, 09:25:38 pm »
I would like to see a non-const getPixelsPtr method so that I can load the image data directly from another API.
I set the image size with the create method, I then would like to pass the address return by getPixelsPtr to the ffmpeg image convert methods so that it loads my image directly.  Currently I have to create a temporary location and then call he loadfrommemory  This results in an extra memcpy that I would like to avoid.  I would like to pass the getPixelsPtr directly into the avpicture_fill method directly.  Below is how I would use it.

thoughts?

  IMAGE_PTR_T pImage = m_ImageCache.create(dPTS,
    m_pAVStrm->codec->width, m_pAVStrm->codec->height);

  ffmpeg::avpicture_fill(&pict, pImage->image.getPixelsPtr(), ffmpeg::PIX_FMT_RGBA,
    m_pAVStrm->codec->width, m_pAVStrm->codec->height);

  m_pImgConvertCtx = ffmpeg::sws_getCachedContext(m_pImgConvertCtx,
    m_pAVStrm->codec->width, m_pAVStrm->codec->height, m_pAVStrm->codec->pix_fmt, // src size / fmt
    m_pAVStrm->codec->width, m_pAVStrm->codec->height, ffmpeg::PIX_FMT_RGBA, // dest size / fmt
    SWS_FAST_BILINEAR, NULL, NULL, NULL);
  if (m_pImgConvertCtx == 0)
  {
    throw std::runtime_error("Cannot initialize the conversion context");
  }

  sws_scale(m_pImgConvertCtx, pFrame->data, pFrame->linesize,
    0, m_pAVStrm->codec->height, pict.data, pict.linesize);
 
« Last Edit: June 29, 2015, 09:48:58 pm by danverde »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Image non const getPixelsPtr
« Reply #1 on: June 30, 2015, 08:27:11 pm »
Create your own vector of Uint8 and then pass the data() pointer to the FFmpeg and Image class. Actually if you want to draw said image and thus need to upload it to the GPU, you can call update() on a texture directly with your pixel vector. That way you avoid the copy and go straight to the GPU which will always require a data transfer from RAM to VRAM.

Also depending on what you want to do, you might want to checkout already created libraries which use FFmpeg.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

danverde

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Image non const getPixelsPtr
« Reply #2 on: July 01, 2015, 02:28:41 am »
Is there a sf::Image method that will use my data pointer directly without doing a memcpy into it's internal vector?  I don't see one.  I am hoping to avoid this extra memcpy as I am decompressing many frames and do not want the extra overhead of the extra memcpy.

from Image.cpp
////////////////////////////////////////////////////////////
void Image::create(unsigned int width, unsigned int height, const Uint8* pixels)
{
    if (pixels && width && height)
    {
        ...
       I want to avoid the step below

        // Copy the pixels
        std::size_t size = width * height * 4;
        m_pixels.resize(size);
        std::memcpy(&m_pixels[0], pixels, size); // faster than vector::assign
    }
    else
    ...
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Image non const getPixelsPtr
« Reply #3 on: July 01, 2015, 09:43:43 am »
Did you read what I wrote?

You don't need to use an sf::Image. Work with your own vector of Uint8 and once filled, pass it to the texture via update().
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Image non const getPixelsPtr
« Reply #4 on: July 01, 2015, 10:46:01 pm »
As I understand it, he is wanting to manually decompress into the Image/Texture/etc.

Rather than decompress to a vector/array/etc, then pass the decompressed array into the Image/Texture to be copied.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Image non const getPixelsPtr
« Reply #5 on: July 01, 2015, 11:27:40 pm »
I understood that, that's why I said he can "decompress" into his own vector of Uint8 and then pass that to the texture's update method. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

danverde

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Image non const getPixelsPtr
« Reply #6 on: July 02, 2015, 06:19:49 am »
I did read what you wrote, but I miss understood that you meant write it directly to the texture, skipping the image altogether.  Thanks, the piece I have been missing.

 

anything