## IupImage, IupImageRGB, IupImageRGBA

Creates an image to be shown on a label, button, toggle, or as a cursor.

(IupImageRGB and IupImageRGBA, since 3.0)

### Creation

    Ihandle* IupImage(int width, int height, const unsigned char *pixels); [in C]
    Ihandle* IupImageRGB(int width, int height, const unsigned char *pixels); [in C]
    Ihandle* IupImageRGBA(int width, int height, const unsigned char *pixels); [in C]

    iup.image{line0: table, line1: table, ...; colors = colors: table} -> (ih: ihandle) [in Lua]
    iup.image{width = width: number, height = height: number, pixels = pixels: table, colors = colors: table} -> (ih: ihandle) [in Lua]
    iup.imagergb {width = width: number, height = height: number, pixels = pixels: table} -> (ih: ihandle) [in Lua]
    iup.imagergba{width = width: number, height = height: number, pixels = pixels: table} -> (ih: ihandle) [in Lua]

    image(width, height, pixel0, pixel1, ...) [in LED]
    imagergb(width, height, pixel0, pixel1, ...) [in LED]
    imagergba(width, height, pixel0, pixel1, ...) [in LED]

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. 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 so you can discard it after the call.
pixel0, pixel1, pixel2, ...: Value of the pixels. But for IupImageRGB and IupImageRGBA in fact will be one value for each color channel (pixel_r_0, pixel_g_0, pixel_b_0, pixel_r_1, pixel_g_1, pixel_b_1, pixel_r_2, pixel_g_2, pixel_b_2, ...).
line0, line1: unnamed tables, one for each line containing pixels values. See Notes below.
colors: table named colors containing the colors indices.

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. Notice that in Lua the first index in the array is "1", the index "0" is ignored in IupLua. Be careful when setting colors, since they are attributes they follow the same storage rules for standard attributes.

> 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 (since 3.29). (since 3.16)

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. (since 3.0)

CLEARCACHE (write-only): clears the internal native image cache, so WID can be dynamically changed. (since 3.24)

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. (since 3.0)

DPI: resolution expected for display. Used when AUTOSCALE=DPI. If not defined the global attribute IMAGESDPI will be used. (since 3.23)

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. (since 3.0)

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. (since 3.24)

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. (since 3.24)

SCALED (read-only): returns Yes if the image has been resized. (since 3.25)

ORIGINALSCALE (read-only): returns the width and height before the image was scaled. (since 3.25)

WID (read-only): returns the internal pixels data pointer. (since 3.0)

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.

Please observe the rules for creating cursor images: CURSOR.

In GTK uses GdkPixbuf/GdkCursor, in Windows uses HBITMAP/HICON, and in Motif uses Pixmap/Cursor.

#### Usage

Images are used in elements such as buttons and labels by attributes that points to names registered with IupSetHandle. You can also use IupSetAttributeHandle to shortcut the set of an image as an attribute. 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 Windows, names of resources in RC files linked with the application are also accepted. In GTK, names of GTK Stock Items are also accepted. In Motif, names of bitmaps installed on the system are also accepted. For example:

    IupSetAttribute(label, "IMAGE", "TECGRAF_BITMAP");  // available in the "etc/iup.rc" file
    or
    IupSetAttribute(label, "IMAGE", "gtk-open");  // available in the GTK Stock Items

In all drivers, a path to a file name can also be used as the attribute value (since 3.0). But the available file formats supported are system dependent. The Windows driver supports BMP, ICO and CUR. The GTK driver supports the formats supported by the GDK-PixBuf library, such as BMP, GIF, JPEG, PCX, PNG, TIFF and many others. The Motif driver supports the X-Windows bitmap. For example:

    IupSetAttribute(label, "IMAGE", "../etc/tecgraf.bmp");

A more format independent approach can be reached using the IUP-IM Functions.

#### 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. In Windows and in GTK, the alpha channel is composed internally by the system. But in Windows for some controls the alpha must be composed a priori also, it includes: IupItem and IupSubmenu always; and IupToggle when NOT using Visual Styles. This implies that if the control background is not uniform then probably there will be 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 Windows, GTK and Motif:

     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 cian)
     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 (cian)
    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.

#### Samples

You can obtain several images from the IupImageLib, a library of pre-defined images. To view the images you can use the IupView in the applications included in the distribution, available at the Download. IupView is also capable of converting several image formats into an IupImage, and save IUP images as LED, Lua or ICO.

#### IupLua Old Constructor

In Lua, the 8 bpp image can also be created using an unnamed table, using a series of tables for each line. Width and height will be guessed from the tables sizes. For example:

    img = iup.image{
      { 1,2,3,3,3,3,3,3,3,2,1 },
      { 2,1,2,3,3,3,3,3,2,1,2 },
      { 3,2,1,2,3,3,3,2,1,2,3 },
      { 3,3,2,1,2,3,2,1,2,3,3 },
      { 3,3,3,2,1,2,1,2,3,3,3 },
      { 3,3,3,3,2,1,2,3,3,3,3 },
      { 3,3,3,2,1,2,1,2,3,3,3 },
      { 3,3,2,1,2,3,2,1,2,3,3 },
      { 3,2,1,2,3,3,3,2,1,2,3 },
      { 2,1,2,3,3,3,3,3,2,1,2 },
      { 1,2,3,3,3,3,3,3,3,2,1 };
      colors = {
        "0 1 0",      -- index 1
        "255 0 0",    -- index 2
        "255 255 0"   -- index 3
      }
    }

Using this constructor the image data can NOT has 0 indices. Notice that the indexing of the unamed tables is different than the colors table. The first value in the colors table is relative to the color index 1, but the first value of the unamed tables is relative to the pixel 0.

After the image is created in Lua, the unnamed tables are not accessible anymore, since "img[1]" will return the attribute "1" value which is the color "0 1 0". To access the original table values you must use "raawget" function, for example:

    lin0 = rawget(img, 1)  -- line index 0
    lin1 = rawget(img, 2)  -- line index 1
    lin2 = rawget(img, 3)  -- line index 2
    ...
    pixel0 = lin0[1]       -- column index 0
    pixel1 = lin0[2]       -- column index 1
    pixel3 = lin0[3]       -- column index 3
    ...

#### IupLua New Constructors

The new constructors since IUP 3 are different and must contains explicit values for width, height and pixels. Also the indexing of the colors table is the same of the pixels table, the first value is the color index 0. For example:

    img = iup.image{
      width = 11,
      height = 11,
      pixels = {
       1,2,0,0,0,0,0,0,0,2,1,
       2,1,2,0,0,0,0,0,2,1,2,
       0,2,1,2,0,0,0,2,1,2,0,
       0,0,2,1,2,0,2,1,2,0,0,
       0,0,0,2,1,2,1,2,0,0,0,
       0,0,0,0,2,1,2,0,0,0,0,
       0,0,0,2,1,2,1,2,0,0,0,
       0,0,2,1,2,0,2,1,2,0,0,
       0,2,1,2,0,0,0,2,1,2,0,
       2,1,2,0,0,0,0,0,2,1,2,
       1,2,0,0,0,0,0,0,0,2,1},
      colors = {
        "255 255 0"   -- index 0
        "0 1 0",      -- index 1
        "255 0 0",    -- index 2
      }
    }

Although in Lua they are still referenced as index 1, so img.colors[1] returns the color of the index 0 in the image.

Here is the same image but using 24 bpp:

    img = iup.imagergb{
      width = 11,
      height = 11,
      pixels = {
        0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,
      255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,
      255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,
      255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,
      255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,
      255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,
      255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,
      255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,
      255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,  255,255,0,
      255,  0,0,    0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0,  255,  0,0,
        0,255,0,  255,  0,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,255,0,  255,  0,0,    0,255,0}
    }

Then at the pixels table we have:

    r0 = img.pixels[1]   g0 = img.pixels[2]   b0 = img.pixels[3]
    r1 = img.pixels[4]   g1 = img.pixels[5]   b1 = img.pixels[6]
    r3 = img.pixels[7]   g3 = img.pixels[8]   b3 = img.pixels[9]
    ...

If the image was created in C then there is no way to access its pixels values in Lua, except as an userdata using the WID attribute.

### Examples

Browse for Example Files

### See Also

IupLabel, IupButton, IupToggle, IupDestroy.