Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

Write pixels to a TOP

Still a little green? Ask your beginner's questions here

Write pixels to a TOP

Postby petitsapin » Mon Aug 12, 2019 8:14 am

Hi,

I am doing some pixel manipulation on a TOP with the script CHOP but I am not able to write the updated pixel values back to the TOP once my script has run.
What am I missing? I don't seem to find anything in the Python doc for the movie file in or TOP for how to write pixels.

Thanks
petitsapin
 
Posts: 10
Joined: Wed Aug 29, 2018 4:10 am

Re: Write pixels to a TOP

Postby petitsapin » Fri Aug 23, 2019 9:48 am

I ended up using a C++ TOP to solve this issue, it is also probably faster than using Python. I uploaded a full example with source code https://github.com/dotminic/cppTOPBase

Here is the main piece of code:
Code: Select all
void CppTOP::execute( TOP_OutputFormatSpecs* outputFormat, const OP_Inputs* inputs, TOP_Context* context, void *reserved1)
{
   int memLocation = 0;
   float*   mem = (float*)outputFormat->cpuPixelData[memLocation];
   OP_TOPInput const* inputTOP = inputs->getInputTOP(0);
   
   if (inputTOP != nullptr)
   {
      OP_TOPInputDownloadOptions options;
      options.downloadType = OP_TOPInputDownloadType::Instant;
      options.cpuMemPixelType = OP_CPUMemPixelType::RGBA32Float;
      options.verticalFlip = false;
      
      float const* src = (float const*)inputs->getTOPDataInCPUMemory(inputTOP, &options);

      if (src != nullptr)
      {
         m_mult = (float)inputs->getParDouble("Color");
         for (int y = 0; y < inputTOP->height; y++)
         {
            for (int x = 0; x < inputTOP->width; x++)
            {
               int pixelIndex = (y * inputTOP->width + x) * sizeof(float);
               mem[pixelIndex + 0] = src[pixelIndex + 0] * m_mult;
               mem[pixelIndex + 1] = src[pixelIndex + 1] * m_mult;
               mem[pixelIndex + 2] = src[pixelIndex + 2] * m_mult;
               mem[pixelIndex + 3] = src[pixelIndex + 3];
            }
         }
      }
   }
   
   outputFormat->newCPUPixelDataLocation = memLocation;
}
petitsapin
 
Posts: 10
Joined: Wed Aug 29, 2018 4:10 am

Re: Write pixels to a TOP

Postby timgerritsen » Sat Aug 24, 2019 6:06 pm

Hi there,

I'm not sure what you exactly trying to achieve. The C++ TOP looks like you're copying the input pixels (TOP) to an output and multiply it with a scalar. This is similar to the levelTOP.
To write to a TOP you can use the CHOPtoTOP node. This converts chop data to a TOP. Though this is not the fastest indeed.
Best is if you want to alter TOPs, is to use a GLSLTOP. Using GLSL you can manipulate the pixels on the GPU instead of the CPU, which is super fast.

Cheers,
tim
timgerritsen
 
Posts: 78
Joined: Sun Jan 24, 2016 6:57 am

Re: Write pixels to a TOP

Postby petitsapin » Sat Aug 24, 2019 6:18 pm

Hey Tim,

I wanted to read in the pixels from an input TOP and do some pixel sorting but I couldn't find how to do that in Python so I thought I'd just make a CplusPlus TOP. The code I posted there is just a super basic example for anyone else who is looking to do some pixel manipulation that for some reason has to run on the CPU. In my case I am doing pixel sorting which is super easy on the CPU and not as straight forward on the GPU.
petitsapin
 
Posts: 10
Joined: Wed Aug 29, 2018 4:10 am

Re: Write pixels to a TOP

Postby timgerritsen » Sat Aug 24, 2019 7:48 pm

Aah okay.. yeah definitely agreeing with the pixel sorting :D
Thanks for sharing!
timgerritsen
 
Posts: 78
Joined: Sun Jan 24, 2016 6:57 am


Return to Beginners

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 20 guests