Coder Social home page Coder Social logo

jcbritobr / pnm Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 889 KB

PNM is a collection of open image formats. This library supports ppm, pgm and pbm formats,

License: MIT License

Go 100.00%
golang go image-processing image encoder decoder pnm pgm-image ppm-image pbm-image

pnm's Introduction

PNM

PNM is a collection of open image formats. They are also referred as portable anymap format(PNM). This library supports ppm(portable pixmap), pgm(portable graymap) and pbm(portable bitmap) formats. There is also an encoder and decoder implementations.

  • Description
    Each file starts with a two byte magic number (in ascii) that identifies the type of the file it is (PPM, PGM, PBM) and its encoding (ascii/plain or binary/raw). The magic number is a capital P followed by a single-digit number. Below is a explanation table with ascii/binary magic number formats.
Type Magic Number Extension Color
Portable Bitmap P1/P4 .pbm 0-1(White & Black)
Portable GrayMap P2/P5 .pgm 0-255(gray scale), variable, black to white range
Portable PixMap P3/P6 .ppm 16 777 216 (0-255 for each RGB channel), some support for 0-65535 per channel

The ascii/plain format allow for human readability and easy transfer to other platforms. The binary/raw formats are more efficient in size but will be dependent of platforms.

  • Encoder usage
import (
    "math/rand"
    "os"
    "github.com/jcbritobr/pnm"
)

func main() {
    image := pnm.NewPGMImage(800, 800, 255, pnm.PGMBinary)
    file, err := os.Create("testdata/synimage.pgm")
    if err != nil {
        panic(err)
    }
    encoder := pnm.NewEncoder(file)

    imgbuf := image.Buffer()

    for i := range image.Buffer() {
        data := rand.Intn(255-0) + 0
        imgbuf[i] = byte(data)
    }

    err = encoder.Encode(image)
    if err != nil {
        panic(err)
    }
}
  • Decoder usage
import (
    "fmt"
    "os"

    "github.com/jcbritobr/pnm"
)

func main() {
    var image pnm.PGMImage
    file, err := os.Open("testdata/synimage.pgm")
    if err != nil {
        panic(err)
    }
    decoder := pnm.NewDecoder(file, pnm.PGMBinary)
    err = decoder.Decode(&image)

    if err != nil {
        panic(err)
    }
}
  • Using ImageBuffer and color.Model (RGBA)
import (
    "os"
    "github.com/jcbritobr/pnm"
    "github.com/jcbritobr/pnm/buffer"
    "github.com/jcbritobr/pnm/color"
)

func main() {
    var image pnm.PPMImage
    file, err := os.Open("testdata/tree_1.ppm")
    if err != nil {
        t.Errorf("fail with %v", err)
    }
    defer file.Close()

    decoder := pnm.NewDecoder(file, pnm.PPMBinary)
    err = decoder.Decode(&image)
    if err != nil {
        t.Errorf("fail to decode image %v", err)
    }

    imbuf := buffer.NewImageBuffer(image)
    rgba := color.RGBA{}
    newbuf := []byte{}
    for {
        n, _ := imbuf.Read(rgba[:])
        if n <= 0 {
            break
        }
        r, g, b, _ := rgba.RGBA()
        r = 255 - r
        g = 255 - g
        b = 255 - b
        newbuf = append(newbuf, r)
        newbuf = append(newbuf, g)
        newbuf = append(newbuf, b)
    }

    image.SetBuffer(newbuf)

    fe, err := os.Create("testdata/tree_3.ppm")
    if err != nil {
        t.Errorf("fail to create file %v", err)
    }
    defer fe.Close()

    encoder := pnm.NewEncoder(fe)
    err = encoder.Encode(&image)
    if err != nil {
        t.Errorf("Encode() = %v want %v", err, nil)
    }
}

pnm's People

Contributors

jgardona avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pnm's Issues

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.