Coder Social home page Coder Social logo

How to remove all unnecessary data about nimpng HOT 8 OPEN

jangko avatar jangko commented on June 11, 2024
How to remove all unnecessary data

from nimpng.

Comments (8)

AmjadHD avatar AmjadHD commented on June 11, 2024 1

I think it's not related to compression.
When opening the 3 images in https://www.nayuki.io/page/png-file-chunk-inspector, it shows that gimp only includes IHDR, IDAT and IEND chunks.
while nimPNG also includes PLTE and tRNS

from nimpng.

jangko avatar jangko commented on June 11, 2024

I assume your pngs contain transparency and no animation frames, therefore I use loadPNG32/savePNG32 pair.

what we do here basically only preserve the pixel data(+ transparency) and discard everything else.
if you also want to discard transparency, you can use loadPNG24/savePNG24 version.

And then nimPNG will try to find the best compression for your new png.

let lres = loadPNG32(seq[uint8], "input.png")
if lres.isOk:
  let png = lres.get()
  let sres = savePNG32("output.png", png.data, png.width, png.height)      
  if sres.isErr:
    echo sres.error
else:
  echo lres.error

from nimpng.

AmjadHD avatar AmjadHD commented on June 11, 2024

Hi, Thanks for your help !
That still produces larger output than gimp
Original: auto_complete_detail_pane 157B
GIMP: auto_complete_detail_pane_gimp 103B
nimPNG: auto_complete_detail_pane_nimPNG 134B

from nimpng.

jangko avatar jangko commented on June 11, 2024

then try this. it will take longer time, but produce smaller output.
if it still produce larger size than gimp, then you perhaps should find other png library.
currently nimPNG doesn't expose it's compression library settings to user, and it requires some work to enable that. and unfortunately I'm still too busy to make such modification.

var conf = makePNGEncoder()
conf.filterStrategy = LFS_BRUTE_FORCE

let sres = savePNG32("output.png", png.data, png.width, png.height, conf) 

from nimpng.

jangko avatar jangko commented on June 11, 2024

I see, then you can try to turn off autoConvert. If autoConvert enabled, it will count how many colors in the image and create palette if color number <= 256. This strategy works for bigger image to reduce file size. But looks like your image is small enough and this feature actually add more bytes.

var conf = makePNGEncoder()
conf.autoConvert = false

from nimpng.

jangko avatar jangko commented on June 11, 2024

alternative, read important chunk only, and then write back without reencoding.

import ../nimPNG, streams

proc main() =
  var conf = makePNGDecoder()
  conf.colorConvert = false
  conf.readTextChunks = false
  conf.rememberUnknownChunks = false
  
  var s = newFileStream("input.png", fmRead)
  var png = decodePNG(seq[uint8], s, conf)
  s.close()
    
  s = newFileStream("output.png", fmWrite)
  png.writeChunks s
  s.close()
  
main()

from nimpng.

AmjadHD avatar AmjadHD commented on June 11, 2024

The first code produces 111B (better), gimp produces 103B.
The second one produces a corrupt file.

from nimpng.

jangko avatar jangko commented on June 11, 2024

sorry, out of idea.

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.