> On Mar 21, 2017, at 4:40 AM, Heng Zhou <[email protected]> wrote:
>
> Thank you for the interesting post. I happen to be working on a project in
> which I need to composite a text onto a background image. But both the text
> and background image are generated by C++ code, so I hope I can write a few
> lines of code to do the same job as what oiiotool does in your post. I have
> no idea how. Could you (or any other nice people) please share a C++ snippet
> with me? I don't need a shadowed text; just displaying it on the image I
> generated would suffice. Thanks a lot!
>
Sure!
I'm just doing this off the top of my head, so please double check against the
docs, but this should point you there.
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
using namespace OIIO;
...
To load a background image:
ImageBuf bg ("background.tif");
Or to *wrap* an existing buffer (the ImageBuf won't allocate or own the pixel
memory):
unsigned char image[xres*yres*channels];
ImageSpec spec (xres, yres, channels, TypeDesc::UINT8);
ImageBuf bg (spec, image);
Alternately, to *copy* from an existing buffer (with the ImageBuf allocating
and managing its own pixel memory):
ImageBuf bg (spec);
bg.set_pixels (ROI(0,xres,0,yres,0,1,0,channels), TypeDesc::UINT8, image);
To add text to your buffer:
float textcolor[3] = { 1, 1, 1 }; // MUST be at least as long as the number
of channels!
ImageBufAlgo::render_text (bg, 50 /* x */, 100 /* y */,
"Hello, world!" /* text to draw */,
32 /* font size */, "" /* font name, empty for
default */,
textcolor);
And then to save the image to disk:
bg.set_write_format (TypeDesc::UINT8);
bg.write ("output.tif");
Or, if you had "wrapped" an image, the text will already be in your original
buffer.
Or, if you *copied* the image, you can copy out again like this:
bg.get_pixels (ROI(0,xres,0,yres,0,1,0,channels), TypeDesc::UINT8, image);
Does that answer your question?
>
> On Tue, Mar 21, 2017 at 2:00 AM, Larry Gritz <[email protected]
> <mailto:[email protected]>> wrote:
> Let's see if this comes through the mail list with images intact. If not,
> I'll make it into a wiki page or something.
>
> ---
>
> Let's say you have a text image, white on black, and you want to composite it
> over an existing background image. Let's quickly generate one to have it as
> an example:
>
> $ oiiotool -create 128x96 3 -text:x=20:y=50:size=32:color=1,0,0 "Hello"
> -d uint8 -o text.tif
>
> <text.png>
>
> And let's make an RGBA file for our background using something from our
> testsuite:
>
> $ cp tahoe-tiny.tif ./bg.tif
>
> <bg.png>
>
> Now, we could naively composite it:
>
> $ oiiotool text.tif bg.tif --over -o comp.tif
> oiiotool ERROR: over : images must have alpha channels
>
> Oops. Well, adding alpha to the background would be easy,
>
> oiiotool ... bg.tif -ch R,G,B,A=1.0 ...
>
> But what about the foreground?
>
> Well, if we really were just using oiiotool to make the text in the first
> place, we could have given it an alpha channel:
>
> $ oiiotool -create 128x96 4 -text:x=20:y=50:size=32:color=1,0,0,1 "Hello"
> -d uint8 -o textrgba.tif
>
> But let's suppose for a minute that we didn't have that luxury, we have an
> RGBA image of text and that's that. We can construct an alpha channel from
> the luminance, then thresholding it, like this, step by step (first, an
> invalid command line that explains, then a fully valid command line):
>
> oiiotool text.tif # read input
> --dup # duplicate it on the stack
> --chsum:weight=.2126,.7152,.0722 # compute luminance as single
> channel
> --mulc 20 # multiply it...
> --clamp:min=0:max=1 # ...and clamp to threshold
> --chappend # mash the original RGB with the
> luminance-computed alpha
> --chnames R,G,B,A # make sure that new channel has the right
> name
> bg.tif # read the background
> --ch R,G,B,A=1.0 # add an alpha channel to the backgound
> (opaque)
> --over # composite
> -o comp1.tif # output
>
> $ oiiotool text.tif --dup --chsum:weight=.2126,.7152,.0722 --mulc 20
> --clamp:min=0:max=1 --chappend -chnames R,G,B,A bg.tif -ch R,G,B,A=1.0 --over
> -o comp1.tif
>
> The purpose of the --mulc and --clamp is so that a dimmer text color (less
> than luminance 1.0) won't make a semi-transparent alpha and show the
> background color through the text.
>
> <comp1.png>
>
> This is fairly nice, but a little naive about the text readability. What we
> really want is a bit of a blurred drop shadow, black rim around the letters,
> for better visibility against light backgrounds. So let's add a blur to the
> alpha we are generating, thus making the alpha image extend past the edges of
> the letters themselves to suppress part of the background:
>
> $ oiiotool text.tif --dup --chsum:weight=.2126,.7152,.0722 --blur 5x5
> --mulc 20 --clamp:min=0:max=1 --chappend -chnames R,G,B,A bg.tif -ch
> R,G,B,A=1.0 --over -o comp2.tif
>
> <comp2.png>
>
> VoilĂ ! Much nicer! Adjust the blur size and mulc threshold to suit your
> taste.
>
>
>
> --
> Larry Gritz
> [email protected] <mailto:[email protected]>
>
>
>
> _______________________________________________
> Oiio-dev mailing list
> [email protected] <mailto:[email protected]>
> http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.openimageio.org_listinfo.cgi_oiio-2Ddev-2Dopenimageio.org&d=DwMFaQ&c=Ngd-ta5yRYsqeUsEDgxhcqsYYY1Xs5ogLxWPA_2Wlc4&r=yN_mtgnvh2k3WD8QIZam9tbZUPHJMoZc_18KfiSrxx0&m=euk9A4Ibxbdf6xWtliyUXHguHMD7O_gAzN3HZ6km25E&s=I64wq-2NHLavZXU1sdfiiXSdK59wX0HuUtIxnG4WhD0&e=>
>
>
> _______________________________________________
> Oiio-dev mailing list
> [email protected]
> http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org
--
Larry Gritz
[email protected]
_______________________________________________
Oiio-dev mailing list
[email protected]
http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org