Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing make_pixmap function #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

bredej
Copy link

@bredej bredej commented Dec 9, 2024

I had a looked at the skia_2024 branch and tried to create an interface to be able to update an image from a data structure.

I don't know if it's a good idea to have rgba32 (uint32_t) as a pixel format. This makes the user of the library tackle the endian issue as shown in the example below. If we instead have a pixel format rgba as an uint8_t[4] this will no longer be an issue.

Note that in this PR 'make_pixmap' for rgba32 takes a pointer to uint8_t, not uint32_t. This makes the point of having these template friend functions less relevant.

Not sure how strict you want to follow the skia_branch so some guidance would be appreciated.

#include <elements.hpp>
#include <bit>
#include <vector>

using namespace cycfi::elements;

auto create_rgba32_pixmap()
{
   const int w = 600;
   const int h = 600;
   std::vector<std::uint8_t> buf(w * h * 4);

   for (int y = 0; y != h; ++y) {
      uint8_t *dest = buf.data() + (y * w * 4);
      for (int x = 0; x != w; ++x) {
         float nx = (float)x / w;
         float ny = (float)y / h;
         std::uint8_t r = static_cast<std::uint8_t>(255 * nx);
         std::uint8_t g = static_cast<std::uint8_t>(255 * ny);
         std::uint8_t b = static_cast<std::uint8_t>(255 * (1 - nx) * (1 - ny));
         std::uint8_t a = 255;

         // RGBA32
         if constexpr (std::endian::native == std::endian::big)
         {
            dest[0] = r;
            dest[1] = g;
            dest[2] = b;
            dest[3] = a;
         } else if constexpr (std::endian::native == std::endian::little)
         {
            dest[0] = a;
            dest[1] = b;
            dest[2] = g;
            dest[3] = r;
         }
         dest += 4;
      }
   }

   return make_pixmap<pixel_format::rgba32>(buf.data(), {w, h});
}

int main(int argc, char *argv[]) {
   app _app("Hello pixmap");
   window _win(_app.name());
   _win.on_close = [&_app]() { _app.stop(); };

   view view_(_win);

   auto pm = std::make_shared<pixmap>(create_rgba32_pixmap());
   view_.content(scroller(image{pm}));

   _app.run();
   return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant