0

I have a working code written in C#, which takes an array of byte, representing a bitmap image, and encode it in base64, so it can be shown with a browser.

byte[] bitmap = {...} //getting it from c++ .dll
BitmapSource bs = BitmapSource.Create(w, h, 96, 96, PixelFormats.Bgra32, null, bitmap, Bitmap_Raw_Stride);
return Convert.ToBase64String(bs);

It works fine but I need to make the same thing in C++. I have my byte array, which contains the same thing as the byte array in C#. The problem is that the BitmapSource compresses the bitmap and I dont understand how, from 10 Mo to 1.5 Mo for some bitmaps. How it is done ?

Actually, I can manage to encode my c++ bitmap buffer to base64, but it is too huge to be displayed on a browser.

tring res = base64_encode(buffer, raw_stride* h); //buffer contains my bitmap

I found BitmapSource was using "CachedBitmap", but I cannot figure out how it is calculated ...

Autruche
  • 67
  • 1
  • 6
  • 1
    Unlike other languages like C#, C++ as a language doesn't have any image processing built into it or it's Standard Library. You'd have to use one of already available (or, if you have an appetite for that, develop your own) – SergeyA Mar 12 '19 at 15:29
  • If Windows, you can use GDI+. C++ does not have an image processing library. – Michael Chourdakis Mar 12 '19 at 15:47
  • I use [ImageMagick](https://imagemagick.org)s [Magick++](https://imagemagick.org/script/develop.php#c__) library for that kind of stuff. – Jesper Juhl Mar 12 '19 at 15:54
  • I cannot use GDI+ since this functionnalty needs to be runnable on Linux, and Magick++ mostly works with images saved on disk, not with raw byte array :( – Autruche Mar 12 '19 at 16:50
  • @Autruche Not true. Magick++ works super well with images in memory buffers - check the `Blob` type. See also https://imagemagick.org/Magick++/tutorial/Magick++_tutorial.pdf – Jesper Juhl Mar 12 '19 at 18:35
  • @JesperJuhl You are right, I didnt notice that. I tried to use Magick++ to load my buffer into a blob and save it, but it throws an IllegalAddress exception. Same result when I try to get my base64 from the blob. `InitializeMagick(""); Blob blob(buffer, straw * h); printf("Blob length: %d\n", blob.length()); res = blob.base64();` – Autruche Mar 13 '19 at 13:02

1 Answers1

0

Solved my problem using LodePNG.

byte* buffer = { ... }
string res;
std::vector<unsigned char> png;
unsigned error = lodepng::encode(png, buffer, w, h);
res = base64_encode(&png[0], png.size());
Autruche
  • 67
  • 1
  • 6