Merge pull request #24 from G-e-n-e-v-e-n-s-i-S/resize_image

Add resize_image script function
This commit is contained in:
GenevensiS
2024-10-04 23:12:42 +02:00
committed by GitHub
6 changed files with 53 additions and 1 deletions
+1
View File
@@ -86,6 +86,7 @@ These functions are built into the program, other [[type:function]]s can be defi
| [[fun:saturate]] Saturate/desaturate an image. | [[fun:saturate]] Saturate/desaturate an image.
| [[fun:invert_image]] Invert the colors of an image. | [[fun:invert_image]] Invert the colors of an image.
| [[fun:recolor_image]] Change the colors of an image to match the font color. | [[fun:recolor_image]] Change the colors of an image to match the font color.
| [[fun:resize_image]] Stretch or squeeze an image to a given height and width.
| [[fun:enlarge]] Enlarge an image by putting a border around it. | [[fun:enlarge]] Enlarge an image by putting a border around it.
| [[fun:crop]] Crop an image, giving only a small subset of it. | [[fun:crop]] Crop an image, giving only a small subset of it.
| [[fun:flip_horizontal]] Flip an image horizontally. | [[fun:flip_horizontal]] Flip an image horizontally.
+14
View File
@@ -0,0 +1,14 @@
Function: resize_image
--Usage--
> resize_image(input: image, height: height_of_new_image, width: width_of_new_image)
Shrink or grow an image to a new size specified in the parameters, so it can be further worked on.
If you want to display an image in a field, MSE will automatically resize it to fit the field, you do not need to use this function.
--Parameters--
! Parameter Type Description
| @input@ [[type:image]] Image to resize
| @height@ [[type:int]] Height of the resulting image
| @width@ [[type:int]] Width of the resulting image
+13
View File
@@ -304,6 +304,19 @@ bool EnlargeImage::operator == (const GeneratedImage& that) const {
&& border_size == that2->border_size; && border_size == that2->border_size;
} }
// ----------------------------------------------------------------------------- : ResizeImage
Image ResizeImage::generate(const Options& opt) const {
Image img = image->generate(opt);
return resample(img, width, height);
}
bool ResizeImage::operator == (const GeneratedImage& that) const {
const ResizeImage* that2 = dynamic_cast<const ResizeImage*>(&that);
return that2 && *image == *that2->image
&& width == that2->width
&& height == that2->height;
}
// ----------------------------------------------------------------------------- : CropImage // ----------------------------------------------------------------------------- : CropImage
Image CropImage::generate(const Options& opt) const { Image CropImage::generate(const Options& opt) const {
+15
View File
@@ -287,6 +287,21 @@ private:
double border_size; double border_size;
}; };
// ----------------------------------------------------------------------------- : ResizeImage
/// Resize an image by resampling it
class ResizeImage : public SimpleFilterImage {
public:
inline ResizeImage(const GeneratedImageP& image, int width, int height)
: SimpleFilterImage(image), width(max(1, width)), height(max(1, height))
{}
Image generate(const Options& opt) const override;
bool operator == (const GeneratedImage& that) const override;
private:
int width;
int height;
};
// ----------------------------------------------------------------------------- : CropImage // ----------------------------------------------------------------------------- : CropImage
/// Crop an image at a certain point, to a certain size /// Crop an image at a certain point, to a certain size
+8
View File
@@ -134,6 +134,13 @@ SCRIPT_FUNCTION(enlarge) {
return make_intrusive<EnlargeImage>(input, border_size); return make_intrusive<EnlargeImage>(input, border_size);
} }
SCRIPT_FUNCTION(resize_image) {
SCRIPT_PARAM_C(GeneratedImageP, input);
SCRIPT_PARAM(int, width);
SCRIPT_PARAM(int, height);
return make_intrusive<ResizeImage>(input, width, height);
}
SCRIPT_FUNCTION(crop) { SCRIPT_FUNCTION(crop) {
SCRIPT_PARAM_C(GeneratedImageP, input); SCRIPT_PARAM_C(GeneratedImageP, input);
SCRIPT_PARAM(int, width); SCRIPT_PARAM(int, width);
@@ -253,6 +260,7 @@ void init_script_image_functions(Context& ctx) {
ctx.setVariable(_("invert_image"), script_invert_image); ctx.setVariable(_("invert_image"), script_invert_image);
ctx.setVariable(_("recolor_image"), script_recolor_image); ctx.setVariable(_("recolor_image"), script_recolor_image);
ctx.setVariable(_("enlarge"), script_enlarge); ctx.setVariable(_("enlarge"), script_enlarge);
ctx.setVariable(_("resize_image"), script_resize_image);
ctx.setVariable(_("crop"), script_crop); ctx.setVariable(_("crop"), script_crop);
ctx.setVariable(_("flip_horizontal"), script_flip_horizontal); ctx.setVariable(_("flip_horizontal"), script_flip_horizontal);
ctx.setVariable(_("flip_vertical"), script_flip_vertical); ctx.setVariable(_("flip_vertical"), script_flip_vertical);
@@ -71,7 +71,7 @@ $built_in_functions = array(
'exclusive_choice' =>'', 'exclusive_choice' =>'',
'require_exclusive_choice'=>'', 'require_exclusive_choice'=>'',
'remove_choice' =>'', 'remove_choice' =>'',
// images // images
'linear_blend' =>'', 'linear_blend' =>'',
'masked_blend' =>'', 'masked_blend' =>'',
'combine_blend' =>'', 'combine_blend' =>'',
@@ -81,6 +81,7 @@ $built_in_functions = array(
'saturate' =>'', 'saturate' =>'',
'invert_image' =>'', 'invert_image' =>'',
'recolor_image' =>'', 'recolor_image' =>'',
'resize_image' =>'',
'enlarge' =>'', 'enlarge' =>'',
'crop' =>'', 'crop' =>'',
'flip_horizontal' =>'', 'flip_horizontal' =>'',