Coder Social home page Coder Social logo

Resize image? about nimpng HOT 11 CLOSED

jangko avatar jangko commented on June 2, 2024
Resize image?

from nimpng.

Comments (11)

jangko avatar jangko commented on June 2, 2024

I'm glad you have such motivation, but image scaling/resizing is not something easy, there are many filter/algorithm. And I'm afraid all of them fall into the realm of image processing/rendering.

one of the main purpose of PNG is to preserve pixel data intact because PNG use lossless compression,
scaling/resizing will make the pixel data altered.

But I don't want to kill your motivation either, perhaps you can working with APNG offset and alpha rendering(please read the readme.md and APNG spec). They are fairly easy and can be implemented on top of existing API without knowing to much about the underlying details.

If you already mastered this technique, then you'll have very good foundation to understand resizing/scaling problem.

in the tests folder there is minibmp.nim, you can use that to produce the result of the rendering. your work will become additional tools for nimPNG that is still inside the scope of this library.

from nimpng.

bung87 avatar bung87 commented on June 2, 2024

https://github.com/SolitudeSF/imageman/blob/master/src/imageman/resize.nim I think this file can be ported to nimPNG

from nimpng.

jangko avatar jangko commented on June 2, 2024

no need to port them, you can use nimPNG and imageman together. You only need to convert PNGResult to imageman Image with RGBAU colorspace. And with some pointer tricks, you actually can have almost free conversion.

example:

type
   MyImage = ref Image[ColorRGBAU]

let png = loadPNG32("pngfile.png")
let img = cast[MyImage](png)

let res = resizeBilinear(img[], target_w, target_h)

from nimpng.

bung87 avatar bung87 commented on June 2, 2024

that's cool , there's problem imageman relys on libpng which is not pure, would it acceptable to make a PR port of resize.nim?

from nimpng.

jangko avatar jangko commented on June 2, 2024

you can safely import imageman's images and colors module and use it with nimPNG, if LTO(link time optimization) works properly. no need to worry libpng will be included in your final binary.

no need to port it.

from nimpng.

bung87 avatar bung87 commented on June 2, 2024

Thanks! helpfull , that type casting is magic. solve my problem.

from nimpng.

bung87 avatar bung87 commented on June 2, 2024

I have another problem , how to construct PNG(or encode png bytes) from MyImage I need pass it to my optimizePNG proc or save it somewhere directly, if I use imageman when writePng it will need libpng.

from nimpng.

jangko avatar jangko commented on June 2, 2024

I assume you get the image after resizing then it would be something like this:

type
   MyImage = ref Image[ColorRGBAU]
   TImage = ptr object
      width: int
      height: int
      pixels: seq[uint8]

let png = loadPNG32("pngfile.png")
let img = cast[MyImage](png)

var res = resizeBilinear(img[], target_w, target_h)
let timg = cast[TImage](unsafeAddr(res))
var r = savePNG32("filename.png", timg.pixels, timg.width, timg.height)
if r.isOK:
   # do something
else:
  # do something

tips: as long as the target object have similar structure/size, you can cast it using pointer(no copies), or using object if you can afford copies. you can read more about this trick from C programming language manual.

from nimpng.

bung87 avatar bung87 commented on June 2, 2024

I got unhandled exception: not enough input to encode [PNGError] whether encodePNG or savePNG32, I suppose to store it in memory and pass to next proc,
yeah, that tips helpful, seq[ColorRGBAU] cast to string surprised me , since ColorRGBAU is 4 components, I check the source it's array so it's just bytes

from nimpng.

jangko avatar jangko commented on June 2, 2024

the pixels element length should be w x h x 4 for RGBAU and w x h x 3 for RGBU.

from nimpng.

bung87 avatar bung87 commented on June 2, 2024

yeah, casting can't adjust that seq size , that cause this , I'll retry later . use copymem or something else.

from nimpng.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.