IupImage, IupImageRGB, IupImageRGBA
Creates an image to be shown on a label, button, toggle, or as a cursor.
Creation
Ihandle* IupImage(int width, int height, const unsigned char *pixels);
Ihandle* IupImageRGB(int width, int height, const unsigned char *pixels);
Ihandle* IupImageRGBA(int width, int height, const unsigned char *pixels);
width: Image width in pixels.
height: Image height in pixels.
pixels: Vector containing the value of each pixel.
IupImage uses 1 value per pixel,
IupImageRGB uses 3 values and
IupImageRGBA uses 4 values per pixel, one for each
color channel (r0, g0, b0, r1, g1, b1, ...). Each value is always 8 bit.
Origin is at the top-left corner, and data is oriented top to bottom,
and left to right. The pixels array is duplicated internally and can be
discarded after the call.
Returns: the identifier of the created element, or NULL if an error occurs.
Attributes
"0" Color in index 0.
"1" Color in index 1.
"2" Color in index 2.
...
"i" Color in index i.
The indices can range from 0 to 255. The total number of colors is limited to 256 colors.
The values are integer numbers from 0 to 255, one for each color in the RGB triple (For ex: "64 190 255"). If the value of a given index is "BGCOLOR", the color used will be the background color of the element on which the image will be inserted. The "BGCOLOR" value must be defined within an index less than 16.
Used only for images created with IupImage.
AUTOSCALE: automatically scale the image by a given real factor. Can be "DPI" or a scale factor. If not defined the global attribute IMAGEAUTOSCALE will be used. Values are the same of the global attribute. The minimum resulted size when automatically resized is 24 pixels height.
BGCOLOR: The color used for transparency. If not defined uses the BGCOLOR of the control that contains the image.
BPP (read-only): returns the number of bits per pixel in the image. Images created with IupImage returns 8, with IupImageRGB returns 24 and with IupImageRGBA returns 32.
CLEARCACHE (write-only): clears the internal native image cache, so WID can be dynamically changed.
CHANNELS (read-only): returns the number of channels in the image. Images created with IupImage returns 1, with IupImageRGB returns 3 and with IupImageRGBA returns 4.
DPI: resolution expected for display. Used when AUTOSCALE=DPI. If not defined the global attribute IMAGESDPI will be used.
FLAT_ALPHA: when set to YES, the alpha channel of an RGBA image is flattened against BGCOLOR when the native image is created, instead of relying on system composition. Default: "NO". Honored in Win32, Qt, QML, FLTK and macOS. The Win32 IupMenuItem, IupSubmenu, IupTabs, IupTree and IupToggle controls set it to YES by default on their images.
HEIGHT (read-only): Image height in pixels.
HOTSPOT: Hotspot is the position inside a cursor image indicating the mouse-click spot. Its value is given by the x and y coordinates inside a cursor image. Its value has the format "x:y", where x and y are integers defining the coordinates in pixels. Default: "0:0".
RASTERSIZE (read-only): returns the image size in pixels.
RESHAPE (write-only): given a new size if format "widthxheight", allocates enough memory for the new size and changes WIDTH and HEIGHT attributes. Image contents is ignored and it will contain trash after the reshape.
RESIZE (write-only): given a new size if format "widthxheight", changes WIDTH and HEIGHT attributes, and resizes the image contents using bilinear interpolation for RGB and RGBA images and nearest neighborhood for 8 bits.
SCALE (non-inheritable): number of image pixels in a logical unit of the screen. When defined, the image occupies WIDTH/SCALE x HEIGHT/SCALE logical units, and the first time the image is used its pixels are resampled to the screen scale (SCREENSCALE), so an image with SCALE equal to SCREENSCALE is shown with all its pixels. RGB and RGBA images are resampled with a smoothing filter, images created with IupImage with nearest neighbor. When defined, AUTOSCALE and IMAGEAUTOSCALE are ignored. Changing it after the image is used has no effect. Not supported for cursors and dialog icons.
SCALED (read-only): returns YES if the image has been resized.
ORIGINALSCALE (read-only): returns the width and height before the image was scaled.
WID (read-only): returns the internal pixels data pointer.
WIDTH (read-only): Image width in pixels.
Notes
Application icons are usually 32x32. Toolbar bitmaps are 24x24 or smaller. Menu bitmaps and small icons are 16x16 or smaller.
Images created with the IupImage* constructors can be reused in different elements.
The images should be destroyed when they are no longer necessary, by means of the IupDestroy function. To destroy an image, it cannot be in use, i.e., the controls where it is used should be destroyed first. Images that were associated with controls by names are automatically destroyed in IupClose.
The rules for creating cursor images are in CURSOR.
The underlying native image type per driver:
- Win32: HBITMAP / HICON.
- WinUI: WriteableBitmap.
- GTK 3: GdkPixbuf / GdkCursor.
- GTK 4: GdkTexture / GdkCursor.
- Motif: Pixmap / Cursor.
- Cocoa: NSImage.
- Cocoa Touch: UIImage.
- Qt: QPixmap.
- QML: QPixmap.
- FLTK: Fl_RGB_Image.
- EFL: Evas_Object.
- Android: android.graphics.Bitmap.
- Haiku: BBitmap.
Usage
Images are used in elements such as buttons and labels by attributes that points to names registered with IupSetHandle. IupSetAttributeHandle sets an image as an attribute directly. For example:
Ihandle* image = IupImage(width, height, pixels);
IupSetHandle("MY_IMAGE_NAME", image);
IupSetAttribute(label, "IMAGE", "MY_IMAGE_NAME");
or
IupSetAttributeHandle(label, "IMAGE", image); // an automatic name will be created internally
In all drivers, a path to a file name, or a system-specific stock / named image, can also be used as the attribute value. See IupImageGetHandle for the per-driver list of accepted file formats and named lookups. In WebAssembly, only image files in RESOURCEDIR load.
IupSetAttribute(label, "IMAGE", "TECGRAF_BITMAP"); // a resource in the linked "etc/iup.rc" file (Win32)
IupSetAttribute(label, "IMAGE", "gtk-open"); // a GTK Stock Item (GTK 3)
IupSetAttribute(label, "IMAGE", "../etc/tecgraf.bmp"); // a file path (all drivers)
Colors
In Motif, the alpha channel in RGBA images is always composed with the control BGCOLOR by IUP prior to setting the image at the control, because the native image has no alpha channel. IupDrawImage in Motif keeps the alpha channel when the X11 RENDER extension is available. In all other drivers the alpha channel is composed internally by the system. In Win32, the controls with FLAT_ALPHA=YES compose the alpha against BGCOLOR; a non-uniform control background then shows a visible difference where it should be transparent.
For IupImage, if a color is not set, then it is used a default color for the 16 first colors. The default color table is the same for all drivers:
0 = 0, 0, 0 (black)
1 = 128, 0, 0 (dark red)
2 = 0,128, 0 (dark green)
3 = 128,128, 0 (dark yellow)
4 = 0, 0,128 (dark blue)
5 = 128, 0,128 (dark magenta)
6 = 0,128,128 (dark cyan)
7 = 192,192,192 (gray)
8 = 128,128,128 (dark gray)
9 = 255, 0, 0 (red)
10 = 0,255, 0 (green)
11 = 255,255, 0 (yellow)
12 = 0, 0,255 (blue)
13 = 255, 0,255 (magenta)
14 = 0,255,255 (cyan)
15 = 255,255,255 (white)
For images with more than 16 colors, and up to 256 colors, all the color indices must be defined up to the maximum number of colors. For example, if the biggest image index is 100, then all the colors from i=16 up to i=100 must be defined even if some indices are not used.
Examples
See Also
IupLabel, IupButton, IupToggle, IupDestroy, IupImageGetHandle, IupImageSave, IupImageSaveToBuffer.