123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- class IUP_IMAGE
- -- Creates an image to be shown on a label, button, toggle, or as a
- -- cursor.
- --
- -- Application icons are usually 32x32. Toolbar bitmaps are 24x24 or smaller.
- -- Menu bitmaps and small icons are 16x16 or smaller.
- --
- -- Images created with the procedure "image" can be reused in different
- -- elements.
- --
- -- The images should be destroyed when they are no longer necessary, by means
- -- of the "destroy" feature. 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 "close".
- --
- -- Please observe the rules for creating cursor images: CURSOR.
- --
- -- In all drivers, a path to a file name can also be used as the attribute
- -- value. 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 the procedure creation "image", if a color is not set (with
- -- "set_color_at_index"), 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.
- inherit
- IUP_WIDGET
- IUP_WIDGET_INTERNALS
- IUP_WIDGET_BGCOLOR
- create {ANY}
- image,
- image_rgb,
- image_rgba,
- load
- create {CD_CLIENT_IMAGES}
- with_internal
- feature {ANY}
- image (a_width, a_height: INTEGER; a_pixels: ARRAY[INTEGER])
- -- pixels: Array containing the value of each pixel, 1 value per pixel.
- -- Origin is at the top-left corner and data is oriented top to bottom,
- -- and left to right.
- require
- a_width_not_negative: a_width >= 0
- a_height_not_negative: a_height >= 0
- a_values_not_void: a_pixels /= Void
- consistent_size: a_width * a_height = a_pixels.count
- local
- a_image: POINTER
- do
- a_image := int_image(a_width, a_height, convert_array(a_pixels))
- if a_image /= default_pointer then
- set_widget(a_image)
- end
- end
- image_rgb (a_width, a_height: INTEGER; a_pixels: ARRAY[TUPLE[INTEGER, INTEGER, INTEGER]])
- -- pixels: Array containing the value of each pixel, 3 values per pixel.
- -- Origin is at the top-left corner and data is oriented top to bottom,
- -- and left to right.
- require
- a_width_not_negative: a_width >= 0
- a_height_not_negative: a_height >= 0
- a_values_not_void: a_pixels /= Void
- consistent_size: a_width * a_height = a_pixels.count
- local
- a_image: POINTER
- do
- a_image := int_image_rgb(a_width, a_height,
- convert_rgb_array(a_pixels))
- if a_image /= default_pointer then
- set_widget(a_image)
- end
- end
- image_rgba (a_width, a_height: INTEGER; a_pixels: ARRAY[TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]])
- -- pixels: Array containing the value of each pixel, 4 values per pixel.
- -- Origin is at the top-left corner and data is oriented top to bottom,
- -- and left to right.
- require
- a_width_not_negative: a_width >= 0
- a_height_not_negative: a_height >= 0
- a_values_not_void: a_pixels /= Void
- consistent_size: a_width * a_height = a_pixels.count
- local
- a_image: POINTER
- do
- a_image := int_image_rgba(a_width, a_height,
- convert_rgba_array(a_pixels))
- if a_image /= default_pointer then
- set_widget(a_image)
- end
- end
- load (file_name: STRING)
- -- An image loaded from a file. Supported formats: BMP, JPEG, GIF, TIFF,
- -- PNG, PNM, PCX, ICO and others.
- local
- a_image: POINTER
- do
- a_image := int_load(get_pointer(file_name.to_c))
- if a_image /= default_pointer then
- set_widget(a_image)
- end
- end
- was_created: BOOLEAN
- -- Return "True" if no error when create the image. Useful to
- -- be used when an image is created with "load".
- do
- if widget /= default_pointer then
- Result := True
- else
- Result := False
- end
- end
- get_error: STRING
- -- Return: NO_ERROR if success, otherwise return the error reported
- -- by the system. Useful to get the error if there is one
- -- when create an image with "load".
- do
- if widget /= default_pointer then
- Result := "NO_ERROR"
- else
- Result := iup_open.get_global_attribute("IUPIM_LASTERROR")
- end
- end
- -- Operations
- save (file_name, format: STRING): STRING
- -- Save the image.
- -- file_name: Name of the file to be saved.
- -- format: format descriptor for IM (the library IM used internally). For
- -- ex: "BMP", "JPEG", "GIF", "TIFF", "PNG", "PNM", "PCX", "ICO", etc.
- -- Return: NO_ERROR if success, otherwise return the error reported
- -- by the system.
- local
- i: INTEGER
- do
- i := int_save(widget, get_pointer(file_name.to_c), get_pointer(format.to_c))
- if not i.is_equal(0) then
- Result := "NO_ERROR"
- else
- Result := iup_open.get_global_attribute("IUPIM_LASTERROR")
- end
- end
- -- Attributes
- set_color_at_index (rgb: STRING; index: INTEGER)
- -- The indices can range from 0 to 255. The total number of colors is
- -- limited to 256 colors. 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 "image".
- require
- index >= 0
- index <= 255
- is_valid_rgb_string(rgb)
- do
- iup_open.set_attribute(Current, index.out, rgb)
- end
- set_auto_scale (value: REAL_32)
- -- Automatically scale the image by a given real factor. If not defined
- -- the global attribute IMAGEAUTOSCALE will be used. Values are the same
- -- of the global attribute.
- do
- iup_open.set_attribute(Current, "AUTOSCALE", value.out)
- end
- get_bpp: INTEGER
- -- Returns the number of bits per pixel in the image. Images created
- -- with "image" returns 8, with "image_rgb" returns 24 and with
- -- "image_rgba" returns 32.
- local
- str: STRING
- do
- str := iup_open.get_attribute(Current, "BPP")
- if str.is_integer then
- Result := str.to_integer
- end
- end
- -- Skip CLEARCACHE
- get_channels: INTEGER
- -- Returns the number of channels in the image. Images created with
- -- "image" returns 1, with "image_rgb" returns 3 and with
- -- "image_rgba" returns 4.
- local
- str: STRING
- do
- str := iup_open.get_attribute(Current, "CHANNELS")
- if str.is_integer then
- Result := str.to_integer
- end
- end
- get_height: INTEGER
- -- Image height in pixels.
- local
- str: STRING
- do
- str := iup_open.get_attribute(Current, "HEIGHT")
- if str.is_integer then
- Result := str.to_integer
- end
- end
- set_hot_spot (x, y: INTEGER)
- -- 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. Default: "0:0".
- require
- x >= 0
- y >= 0
- local
- v: STRING
- do
- v := x.out
- v.append_string(":")
- v.append_string(y.out)
- iup_open.set_attribute(Current, "HOTSPOT", v)
- end
- get_rastersize: TUPLE[INTEGER, INTEGER]
- -- Returns the Current size, in pixels.
- local
- size: STRING
- do
- size := iup_open.get_attribute(Current, "RASTERSIZE")
- Result := components_of_size(size)
- end
- -- Skip RESHAPE
- resize_to (width, height: INTEGER)
- -- Given a new size changes WIDTH and HEIGHT attributes, and resizes the
- -- image contents using bilinear interpolation. Works only for RGB and
- -- RGBA images.
- require
- width > 0
- height > 0
- local
- v: STRING
- do
- v := width.out
- v.append_string("x")
- v.append_string(height.out)
- iup_open.set_attribute(Current, "RESIZE", v)
- end
- get_width: INTEGER
- -- Image width in pixels.
- local
- str: STRING
- do
- str := iup_open.get_attribute(Current, "WIDTH")
- if str.is_integer then
- Result := str.to_integer
- end
- end
- feature {CD_CLIENT_IMAGES}
- with_internal (a_image: POINTER)
- do
- set_widget(a_image)
- end
- get_wid: POINTER
- -- Returns the internal pixels data pointer.
- do
- Result := int_get_attribute(widget, get_pointer(("WID").to_c))
- end
- feature {NONE}
- -- Internals
- int_image (width, height: INTEGER; pixels: POINTER): POINTER
- external
- "C inline use %"eiffel-iup.h%""
- alias
- "return IupImage ($width, $height, $pixels);"
- end
- int_image_rgb (width, height: INTEGER; pixels: POINTER): POINTER
- external
- "C inline use %"eiffel-iup.h%""
- alias
- "return IupImageRGB ($width, $height, $pixels);"
- end
- int_image_rgba (width, height: INTEGER; pixels: POINTER): POINTER
- external
- "C inline use %"eiffel-iup.h%""
- alias
- "return IupImageRGBA ($width, $height, $pixels);"
- end
- int_load(file_name: POINTER): POINTER
- external
- "C inline use %"eiffel-iup.h%""
- alias
- "return IupLoadImage ($file_name);"
- end
- int_save(wgt, file_name, format: POINTER): INTEGER
- external
- "C inline use %"eiffel-iup.h%""
- alias
- "return IupSaveImage ($wgt, $file_name, $format);"
- end
- int_get_attribute (wgt, name: POINTER): POINTER
- external
- "C inline use %"eiffel-iup.h%""
- alias
- "return IupGetAttribute ($wgt, $name);"
- end
- -- Conversion
- convert_array (matrix: ARRAY[INTEGER]): POINTER
- local
- i, v: INTEGER;
- arg: ARRAY[CHARACTER_8]
- do
- create arg.make_filled(' ', 1, matrix.count + 1)
- i := 0
-
- across
- matrix as ic
- loop
- i := i + 1
- v := ic.item
- arg.put(v.to_character_8, i)
- end
-
- Result := get_pointer(arg.to_c)
- end
- convert_rgb_array (matrix: ARRAY[TUPLE[INTEGER, INTEGER, INTEGER]]): POINTER
- local
- i: INTEGER;
- arg: ARRAY[CHARACTER_8]
- v: TUPLE[INTEGER, INTEGER, INTEGER]
- do
- create arg.make_filled(' ', 1, matrix.count + 1)
- i := 0
-
- across
- matrix as ic
- loop
- i := i + 1
- v := ic.item
- arg.put(v.integer_32_item(1).to_character_8, i)
- i := i + 1
- arg.put(v.integer_32_item(2).to_character_8, i)
- i := i + 1
- arg.put(v.integer_32_item(3).to_character_8, i)
- end
-
- Result := get_pointer(arg.to_c)
- end
- convert_rgba_array (matrix: ARRAY[TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]]): POINTER
- local
- i: INTEGER;
- arg: ARRAY[CHARACTER_8]
- v: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
- do
- create arg.make_filled(' ', 1, matrix.count + 1)
- i := 0
-
- across
- matrix as ic
- loop
- i := i + 1
- v := ic.item
- arg.put(v.integer_32_item(1).to_character_8, i)
- i := i + 1
- arg.put(v.integer_32_item(2).to_character_8, i)
- i := i + 1
- arg.put(v.integer_32_item(3).to_character_8, i)
- i := i + 1
- arg.put(v.integer_32_item(4).to_character_8, i)
- end
-
- Result := get_pointer(arg.to_c)
- end
- end
- -- The MIT License (MIT)
- -- Copyright (c) 2016, 2018, 2019, 2020 by German A. Arias
- -- Permission is hereby granted, free of charge, to any person obtaining a copy
- -- of this software and associated documentation files (the "Software"), to deal
- -- in the Software without restriction, including without limitation the rights
- -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- -- copies of the Software, and to permit persons to whom the Software is
- -- furnished to do so, subject to the following conditions:
- --
- -- The above copyright notice and this permission notice shall be included in
- -- all copies or substantial portions of the Software.
- --
- -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- -- SOFTWARE.
|