PNG to Alpha8 image

178 views
Skip to first unread message

Shawn Riordan

unread,
Aug 26, 2022, 7:01:07 AM8/26/22
to skia-discuss
Whenever I create an SkImage by reading a PNG file, the code goes something like this:

      auto skdata = SkData::MakeWithCopy(data, data_size);
      if(skdata != nullptr)
      {
         m_image = SkImage::MakeFromEncoded(std::move(skdata));
         if(m_image != nullptr)
         {
            // yay
         }
      }

And when the PNG file format is 8-bit, the info says that the color type is: kGray_8_SkColorType

I want to treat this single channel image as an Alpha8 image though.
Is there a way to change the colortype to kAlpha_8_SkColorType?

Leon Scroggins

unread,
Aug 26, 2022, 10:31:29 AM8/26/22
to skia-d...@googlegroups.com
SkCodec lets you decode into memory you specify. So you can e.g. create an SkBitmap/SkPixmap that is kAlpha_8 and tell the SkCodec to decode into it as kGray_8.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/706a5def-00b1-4a4d-adf9-b365bb937ef6n%40googlegroups.com.


--

 •  Leon Scroggins

 •  scr...@google.com


Shawn Riordan

unread,
Aug 26, 2022, 1:32:39 PM8/26/22
to skia-discuss
Thank you.  I will look into SkCodec

Shawn Riordan

unread,
Aug 26, 2022, 6:22:46 PM8/26/22
to skia-discuss
I have never worked with SkCodec before.  I am getting a runtime error in this code:

         sk_sp<SkData> skdata = SkData::MakeWithCopy(data, data_size);
         if(skdata != nullptr)
         {
            std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(skdata)); // data is a grayscale PNG file
            if(codec != nullptr)
            {
               SkBitmap bitmap;
               SkImageInfo info = codec->getInfo().makeColorType(kAlpha_8_SkColorType);
               if(bitmap.tryAllocPixels(info))
               {
                  if(codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes()) == SkCodec::kSuccess) // <- returns kInvalidConversion
                  {
                     bitmap.setImmutable();
                     m_image = SkImage::MakeFromBitmap(bitmap);
                     if(m_image != nullptr)
                     {
                        return true;
                     }
                  }
               }
            }
         }

Any idea what am I doing incorrectly?

On Friday, August 26, 2022 at 7:31:29 AM UTC-7 scroggo wrote:

Leon Scroggins

unread,
Aug 29, 2022, 9:57:33 AM8/29/22
to skia-d...@googlegroups.com
On Fri, Aug 26, 2022 at 6:22 PM 'Shawn Riordan' via skia-discuss <skia-d...@googlegroups.com> wrote:
I have never worked with SkCodec before.  I am getting a runtime error in this code:

         sk_sp<SkData> skdata = SkData::MakeWithCopy(data, data_size);
         if(skdata != nullptr)
         {
            std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(skdata)); // data is a grayscale PNG file
            if(codec != nullptr)
            {
               SkBitmap bitmap;
               SkImageInfo info = codec->getInfo().makeColorType(kAlpha_8_SkColorType);
               if(bitmap.tryAllocPixels(info))
               {

Instead of passing info to getPixels on this next line, you should pass codec->getInfo().
 

craste...@gmail.com

unread,
Aug 29, 2022, 10:56:51 AM8/29/22
to skia-discuss
Thank you, kind sir!
Reply all
Reply to author
Forward
0 new messages