Getting actual image dimensions in 100% preview

Hi there!

I’ve been trying to achieve a more realistic film grain simulation for my photos, and started out by changing David Tschumperlé’s “Add Grain” filter. Instead of using the default film grain patches provided with the “Add Grain”, I changed the filter to allow the user (me…) to select any image file on the file system as a grain source, and added/changed a few control options just to explore the possibilities for changing the grain characteristics.

For the “more realistic” part, I was thinking of using grain samples with a pre-normalised resolution of 24MP (6000x4000px) and assuming they represent a full 35mm film frame. From there I intend to calculate the approximate scale factor for resizing the grain sample image based on the input image resolution, so that the general grain character would be preserved even if the filter is used on a photo taken with a 12MP, 16MP, 43MP, etc camera (ignoring for the time being the different possible aspect ratios and sensor sizes/formats). I will still allow the user to fine-tune the scale just as in the original “Add Grain” filter.

I think I’m heading in the right direction, but now I want the 100% preview to be faithful to the final effect of the filter, and for this a need the original image’s dimensions so that I can calculate the proper scale factor for the grain sample image. However, when I try to get the current image width (as in {w}), I’m only getting the preview area width. Is there any way to obtain the actual full width & height for the current image when showing the preview at 100%?

I’m currently on gmic-qt 3.0.1 with GIMP 2.10.25 (AppImage), Kubuntu 22.04.

Thanks!

Yes, there is indeed a way for a filter to request the full image rather than just the image seen int the preview area : Add a * after the preview zoom factor, like this:

#@gui My Filter : my_filter, my_filter(1)*
my_filter : 
  display # Here you should see the whole image

EDIT: But I’m not sure this feature was already present in G’MIC-Qt 3.0.1…

Thanks, David! I tried adding the * after the preview zoom factor as you suggested, but it had no effect ({w} still reports the preview area width, not the actual image width). I don’t remember where I got the G’MIC-Qt 3.0.1 compatible with the GIMP AppImage, but I guess I could always build the current version from source and try your suggestion. However, wouldn’t it make the preview too slow? I mean having to apply the filter to the whole image just for preview? I was thinking more of something like accessing the the full image’s dimensions as some pseudo-variables (say, {act_w} instead of {w}) just for using them in my calculations to get the proper grain scale factor to be used in the 100% preview, but still rendering the preview on the cropped image shown by G’MIC-Qt, which is much smaller than the full-sized image. I imagine these properties are available to G’MIC-Qt before invoking the filter preview.

You are not forced to process the entire image by the way.
There are predefined variables $_preview_x0, $_preview_y0, $_preview_x1, $_preview_y1, $_preview_width, $_preview_height, $_preview_area_width, $_preview_area_height that can be used to crop/resize the full image to fit the preview widget.

Commands: gui_crop_preview, gui_resize_preview, gui_crop_resize_preview can be used as well to retrieve the preview thumbnail from the full image.

Sounds good, though a bit more complex than I had anticipated. Looks like I’ll have to get my feet wet learning G’MIC, which I’m sure is a good thing. Thanks for your help! I may have to come back here with a few more questions in the near future.

Basically, what you really need is trying first gui_crop_resize_preview, after you made your calculations on the full image.
It just extracts the image thumbnail displayed in the preview area from the full image, then you are in the exact same case as when you don’t use the “full” preview mode.

#@gui My Filter : my_filter, my_filter_preview(1)*
#@gui : Sigma = float(0,0,10)
my_filter : 
  blur $1

my_filter_preview : 
  W,H:=w,h   # Save full image size in variables $W and $H
  gui_crop_resize_preview  # Extract preview thumbnail from full image
  my_filter {max($W,$H)/10*$1}
1 Like

Worked like a charm! Many thanks for you detailed explanations and for the working example. With the example filter, it turned up to be very simple indeed.

The filter is already doing most of what I wanted, though a little polishing and fine tuning is still in order.

2 Likes

Not going to lie, I actually like the filter based from what I see and am impressed at your progress.

Thanks. I’ll post the source code here once I’m happy with the results. I’m not even sure I’m following the proper patterns and guidelines for G’MIC filters, so any ideas and constructive criticism would be welcome. :slightly_smiling_face:

2 Likes

Once the source code is released, I’d be happy to include the filter (after review) to the G’MIC-Qt plug-in, either in Testing/MarcosC, or directly in one of the main category (Degradation/ maybe ? it is where the other “grain/noise” filters lies).