GreycLab / CImg

The CImg Library is a small and open-source C++ toolkit for image processing
http://cimg.eu
Other
1.5k stars 286 forks source link

Alpha backgrounds #283

Closed sophieblep closed 4 years ago

sophieblep commented 4 years ago

Hopefully a very easy question for someone. I'm able to use draw_text to put text onto a loaded image. What I want to do is draw the text in the absence of an image, i.e. it would need its own invisible image buffer.

How easy is it to create a 640x480 transparent CImg buffer that I could draw a selectable font upon? I plan to blit it onto other dynamically loaded layers.

Thank you for your help, o wizards of CImg!

dtschump commented 4 years ago

Something like this:

#include "CImg.h"
using namespace cimg_library;

int main(int argc,char **argv) {

  // Generate "text over transparent background" image.
  CImg<unsigned char> text(640,480,1,4,0);  // RGBA transparent image.
  const unsigned char color[] = { 255,255,128,255 }; // RGBA color
  text.draw_text(20,20,"Hello there ! ",color,0,1,32);

  // Generate random color image.
  CImg<unsigned char> img(640,480,1,3);
  img.rand(0,255).blur(7).normalize(0,180);

  // Add text on image 'img'.
  img.draw_image(0,0,0,0,
                 text.get_shared_channels(0,2),
                 text.get_shared_channel(3),
                 1,255);

  img.display();
  return 0;
}

Gives this:

ex

sophieblep commented 4 years ago

Perfect, couldn't have asked for a quicker or more concise answer. Add another star to your wizard hat sir. Thank you! x