Resize images: gocomics uses Xojo ?

Yes, and possibly nil. OriginalPicture.Mask is nil when retina is on but CopyMask will work.

dim oldMaskSurf as RGBSurface = OriginalPicture.CopyMask.RGBSurface

Mask will also be nil on an alpha channel picture.

You should take a look at Maxim Stepin HQX scalers.
https://en.wikipedia.org/wiki/Pixel_art_scaling_algorithms

http://blog.pkh.me/p/19-butchering-hqx-scaling-filters.html

I saw it working with FFmpeg and the results are AMAZING.

[quote=279817:@Will Shank]Yes, and possibly nil. OriginalPicture.Mask is nil when retina is on but CopyMask will work.

dim oldMaskSurf as RGBSurface = OriginalPicture.CopyMask.RGBSurface

Mask will also be nil on an alpha channel picture.[/quote]

Here is the new code with copyMask. Thank you Beatrix and Will :slight_smile:

[code]Private Function BilinearInterpolation(OriginalPicture as Picture, newWidth as Integer, newHeight as Integer, constrainProportion as Boolean) As Picture

dim w as Integer = OriginalPicture.Width
dim h as Integer = OriginalPicture.Height
dim x_ratio as Double = (w - 1)/newWidth
dim y_ratio as Double = (h - 1)/newHeight
if constrainProportion then
if x_ratio >= y_ratio then
newHeight = h/x_ratio
else
newWidth = w/y_ratio
end if
x_ratio = max(x_ratio, y_ratio)
y_ratio = max(x_ratio, y_ratio)
end if

dim oldSurf as RGBSurface = OriginalPicture.RGBSurface
dim oldMaskSurf as RGBSurface = OriginalPicture.CopyMask.RGBSurface
dim InterpolatedPicture as new Picture(newWidth, newHeight, 32)
dim InterpolatedSurf as RGBSurface = InterpolatedPicture.RGBSurface
dim InterpolatedMaskSurf as RGBSurface = InterpolatedPicture.mask.RGBSurface

dim x, y, alphaValue as Integer
dim x_diff, y_diff, blue, red, green, gray as Double
dim a, b, c, d as Color

for i as Integer = 0 to (newHeight - 1)
for j as Integer = 0 to (newWidth - 1)
x = x_ratio * j
y = y_ratio * i
x_diff = (x_ratio * j) -x
y_diff = (y_ratio * i) - y

  'calculations for red, green and blue
  a = oldSurf.Pixel(x, y)
  b = oldSurf.Pixel(x + 1, y)
  c = oldSurf.Pixel(x, y + 1)
  d = oldSurf.Pixel(x + 1, y + 1)
  
  blue = (a.Blue * (1 - x_diff) * (1 - y_diff)) + (b.Blue * x_diff * (1 - y_diff)) + (c.Blue * y_diff * (1 - x_diff)) + (d.Blue * x_diff * y_diff)
  green = (a.green * (1 - x_diff) * (1 - y_diff)) + (b.green * x_diff * (1 - y_diff)) + (c.green * y_diff * (1 - x_diff)) + (d.green * x_diff * y_diff)
  red = (a.red * (1 - x_diff) * (1 - y_diff)) + (b.red * x_diff * (1 - y_diff)) + (c.red * y_diff * (1 - x_diff)) + (d.red * x_diff * y_diff)
  
  InterpolatedSurf.Pixel(j, i) = RGB(red, green, blue)
  
  'now the mask
  a = oldMaskSurf.Pixel(x, y)
  b = oldMaskSurf.Pixel(x + 1, y)
  c = oldMaskSurf.Pixel(x, y + 1)
  d = oldMaskSurf.Pixel(x + 1, y + 1)
  
  gray = (a.Blue * (1 - x_diff) * (1 - y_diff)) + (b.Blue * x_diff * (1 - y_diff)) + (c.Blue * y_diff * (1 - x_diff)) + (d.Blue * x_diff * y_diff)
  
  InterpolatedMaskSurf.Pixel(j, i) = RGB(gray, gray, gray)
next

next

Return InterpolatedPicture
End Function
[/code]

I agree that picture resizing could be optimized, but IMHO the complaint about having to use plugins and declares is unjustified. As it stands, Xojo is already very complete for a RAD. The need for plugins and declares rises from programmer’s ambition that goes beyond the original features of Xojo. If I was to compare, XCode may offer more properties for controls, there are still very frequent times when calling the framework through the equivalent of declares becomes necessary.

I may use XCode and eventualy buy needed libraries (at last, the cost may be the same) ?
;-:slight_smile:

[quote=279840:@Emile Schwarz]I may use XCode and eventualy buy needed libraries (at last, the cost may be the same) ?
;-:)[/quote]

My point exactly. Xojo is not worse.

All is not about cost, BTW. XCode is great, but the mere complexity of it may soon make you waste more time than you can afford.

Hi Beatrix,

I just checked and the resulting quality is the same, with a difference (*).

I resized the image using Preview (OS X) to the correct size and drop it in the window: of course (no Xojo resize) the result is perfect.

  • I do not spend time to search why, but the resized image lost the background (the background image is a WIndows 10 directory, the resized image is painted above it). A simple search does not leads me to understand why: I replaced my .DrawPicture (I commented the line) by a call to your Function (and back).

I confirm what I wrote in a different Conversation: Paste a Function (that is in the Clipboard) does do nothing with Xojo 2015r1 (and here). I had to create a Method, paste the whole code in it, then fills the Method Name, The Parameter declaration and put the Return Kind (to make it a Function); then, comment the Sud / End Sub lines).

I used an assembler nearly 30 years ago, so… complexity (with an oldster mind…)

For the record:

I corrected the image size to be pair values (for Width / Height),
I used Doubles (instead of Integers) as .DrawPicture args,
I even resized my large source image (a copy of) to be 3 x the target size (Width AND Height).

and the resulting image (see above the source one) is still awful.

@Emile Schwarz : show us your results. Really. And your code. Best would be an example download.

If I resize images in Preview I’d say that there is a diffuser (Weichzeichner?). I need to show screenshots and my code together with a bit of unsharp-mask makes the text a bit scrisper.

Beatrix: yes, it is difficult (with my old eye) to be sure the resized image is bad, but once both resize are both displayed, it is clear which one is better.

I’ve made a screen shot of the image above: same lack of quality. I took that screen shot, set an alpha channel (remove the white in the image) and the result quality seems to be better (I am not sure of that).

Time to give a rest to my leading eye.