Prima::ImageViewer(3)
πΈ NAME πΈ
Hey bestie, welcome to the Prima::ImageViewer(3) page β User Contributed Perl Documentation, no cap. This is the manual for Prima::ImageViewer, aka your image, icon, and bitmap viewer. π
β¨ SYNOPSIS β¨
Okay so the quick-start vibe is literally this:
use Prima qw(ImageViewer StdBitmap Application);
Prima::ImageViewer-> new(
image => Prima::StdBitmap::image(0),
zoom => 2.718,
);
run Prima;
π DESCRIPTION π
This module contains the Prima::ImageViewer class, which slays at image-displaying functionality. The widget can display images, icons, and bitmaps, and it allows zooming. β¨
Prima::ImageViewer is a descendant of Prima::Widget::ScrollWidget and inherits its document scrolling behavior and programming interface. See Prima::Widget::ScrollWidget for the details. π«Ά
π¦ API π¦
π Properties π
alignmentINTEGERβ horizontal image alignment, bestie. Pick one of theseta::XXXconstants:ta::Leftta::Centerta::Right
Selects the horizontal image alignment.
Default value: ta::Left
autoZoomBOOLEANβ when set, the image is automatically stretched while keeping aspects to the best available fit, given thezoomPrecision. Scrollbars are turned off ifautoZoomis set to1. πimageOBJECTβ selects the image object to be displayed.OBJECTcan be an instance of thePrima::Image,Prima::Icon, orPrima::DeviceBitmapclasses. πΌοΈimageFileFILEβ sets the imageFILEto be loaded and displayed. Itβs rarely used since it does not return a success flag. πscalingist::XXβ applies scaling when drawing an image.
Default: ist::Box, the default cheap scaling.
β οΈ Warning: scaling types above the ist::Box might be somewhat expensive. πΈ
stretchBOOLEANβ if set, the image is simply stretched over the visual area, without keeping the aspect. Scroll bars, zooming, and keyboard navigation become disabled. π«qualityBOOLEANβ a boolean flag that selects if the palette ofimageis to be copied into the widget palette, providing higher visual quality on paletted displays. See alsopaletteinPrima::Widget.
Default value: 1
valignmentINTEGERβ vertical image alignment. One of theseta::XXXconstants:ta::Topta::Middleorta::Centerta::Bottom
Selects the vertical image alignment.
Note: theta::Middlevalue is not equal tota::Centerβs, however, both constants produce an equal effect here. π
Default value: ta::Bottom
zoomFLOATβ selects the image zoom level. The acceptable value range is between0.01and100. The zoom value is rounded to the closest value divisible by1/zoomPrecision. For example, ifzoomPrecisionis100, the zoom values will be rounded to the precision of hundredth β to fiftieth and twentieth fractional values β.02,.04,.05,.06,.08, and0.1. WhenzoomPrecisionis1000, the precision is one thousandth, and so on. π
Default value: 1
zoomPrecisionINTEGERβ zoom precision of thezoomproperty. The minimal acceptable value is10, where the zoom factor will be rounded to0.2,0.4,0.5,0.6,0.8, and1.0.
The reason behind this arithmetics is that when an image of an arbitrary zoom factor is requested to be displayed, the image sometimes must be drawn from a fractional image pixel. In an example that only involves integer pixels, a 10x zoomed image shifted 3 pixels left must be displayed so that the first image pixel from the left occupies 7 screen pixels, and the next ones β 10 screen pixels. That means that the correct image display routine must ask the system to draw the image at the offset of -3 screen pixels, where the first image pixel column would correspond to that offset.
When the zoom factor is fractional, the picture is getting more complex. For example, with the zoom factor of 12.345 and zero screen offset, the first image pixel begins at the 12th screen pixel, the next one β at the 25th (because of the roundoff), then the 37th, etc etc. If the image is 2000x2000 pixels wide and is asked to be drawn so that it appears shifted 499 screen image pixels left, it needs to be drawn from the 499/12.345=40.42122th image pixel. It might seem that indeed it would be enough to ask the system to begin drawing from image pixel 40, and offset int(0.42122*12.345)=5 screen pixels to the left, however, that procedure will not account for the correct fixed point roundoff that accumulates as the system scales the image. For the zoom factor of 12.345 this roundoff sequence is, as we have seen before, (12,25,37,49,62,74,86,99,111,123) for the first 10 pixels displayed, that occupy (12,13,12,12,13,12,12,13,12,12) screen pixels correspondingly. For the pixels starting at 499, the sequence is (506,519,531,543,556,568,580,593,605,617) offsets or (13,12,12,13,13,12,12,13,12,12) widths β note the two subsequent 13s there. This sequence begins to repeat itself after 200 iterations (12.345*200=2469.000), which means that to achieve correct display results, the image must be asked to be displayed from as far as image pixel 0 if imageβs first pixel on the screen is between 0 and 199 (or for screen pixels 0-2468), then from image pixel 200 for offsets 200-399, (screen pixels 2469-4937), and so on.
Since the system internally allocates memory for image scaling, that means that up to 2*200*min(window_width,image_width)*bytes_per_pixel unnecessary bytes will be allocated for each image drawing call (2 because the calculations are valid for both the vertical and horizontal strips), and this can lead to a slowdown or even request failure when image or window dimensions are large. The proposed solution is to round off the accepted zoom factors so that these offsets are kept small. For example, the N.25 zoom factors require only max 1/.25=4 extra pixels. When the zoomPrecision value is set to 100, the zoom factors are rounded to 0.X2, 0.X4, 0.X5, 0.X6, 0.X8, and 0.X0, thus requiring max 50 extra pixels.
NB: If, despite the efforts, the property gets in the way, increase it to1000or even10000, but note that this may lead to problems. π
Default value: 100
π Methods π
on_paintSELF, CANVASβ thePaintnotification handler is mentioned here for the specific case of its return value, that is the return value of the internalput_imagecall. For those who might be interested input_imagefailures, which mostly occur when trying to draw an image that is too big, the following code might be useful:
sub on_paint
{
my ( $self, $canvas) = @_;
warn "put_image() error:$@" unless $self-> SUPER::on_paint($canvas);
}
screen2pointX, Y, [ X, Y, ... ]β performs translation of integer pairs as(X,Y)-points from the widget coordinates to pixel offsets in the image coordinate system. Takes into account zoom level, image alignments, and offsets. Returns an array of the same length as the input.
Useful for determining correspondence, for example, of a mouse event to an image point. π
The reverse function is point2screen.
point2screen X, Y, [ X, Y, ... ]β performs translation of integer pairs as(X,Y)-points from image pixel offset to widget image coordinates. Takes into account zoom level, image alignments, and offsets. Returns an array of the same length as the input.
Useful for determining the screen location of an image point. π
The reverse function is screen2point.
watch_load_progressIMAGEβ when called, the image viewer begins to track the progress ofIMAGEbeing loaded (seeloadinPrima::Image) and incrementally displays the loading picture. As soon asIMAGEbegins to load, it replaces the existingimageproperty value. Example:
$i = Prima::Image-> new;
$viewer-> watch_load_progress( $i);
$i-> load('huge.jpg');
$viewer-> unwatch_load_progress;
A similar functionality is present in Prima::Dialog::ImageDialog. π
unwatch_load_progressCLEAR_IMAGE=1β stops monitoring the image loading progress. IfCLEAR_IMAGEis0, the leftovers of the incremental loading stay intact in theimageproperty. Otherwise,imageis set toundef. π§zoom_roundZOOMβ rounds the zoom factor tozoomPrecisionprecision, returns the rounded zoom value. The algorithm is the same as used internally in thezoomproperty. π
π AUTHOR π
Dmitry Karasik, <dmitry@karasik.eu.org>. π
π SEE ALSO π
Prima, Prima::Image, Prima::Widget::ScrollWidget, Prima::Dialog::ImageDialog, examples/iv.pl. Go check them out, iykyk. π
This Prima::ImageViewer(3) page is from perl v5.42.0, dated 2024-08-20. Stay iconic, bestie π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms