{"id":27106437,"url":"https://github.com/jcbritobr/pnm","last_synced_at":"2025-04-06T19:55:58.551Z","repository":{"id":56597359,"uuid":"301112897","full_name":"jcbritobr/pnm","owner":"jcbritobr","description":"PNM is a collection of open image formats. This library supports ppm, pgm and pbm formats,","archived":false,"fork":false,"pushed_at":"2020-10-29T15:34:00.000Z","size":889,"stargazers_count":5,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T22:35:58.754Z","etag":null,"topics":["decoder","encoder","go","golang","image","image-processing","pbm-image","pgm-image","pnm","ppm-image"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcbritobr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-04T11:31:21.000Z","updated_at":"2023-11-27T01:04:31.000Z","dependencies_parsed_at":"2022-08-15T21:40:26.953Z","dependency_job_id":null,"html_url":"https://github.com/jcbritobr/pnm","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcbritobr%2Fpnm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcbritobr%2Fpnm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcbritobr%2Fpnm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcbritobr%2Fpnm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcbritobr","download_url":"https://codeload.github.com/jcbritobr/pnm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247543581,"owners_count":20955865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decoder","encoder","go","golang","image","image-processing","pbm-image","pgm-image","pnm","ppm-image"],"created_at":"2025-04-06T19:55:58.002Z","updated_at":"2025-04-06T19:55:58.542Z","avatar_url":"https://github.com/jcbritobr.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PNM\r\nPNM 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.\r\n\r\n* Description \\\r\nEach 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.\r\n\r\n| **Type**        | **Magic Number** | **Extension** | **Color**                                                                    |\r\n|-----------------|------------------|---------------|------------------------------------------------------------------------------|\r\n| Portable Bitmap | P1/P4            | .pbm          | 0-1(White \u0026 Black)                                                           |\r\n| Portable GrayMap| P2/P5            | .pgm          | 0-255(gray scale), variable, black to white range                            |\r\n| Portable PixMap | P3/P6            | .ppm          | 16 777 216 (0-255 for each RGB channel), some support for 0-65535 per channel|\r\n\r\n\u003cp\u003e\u003c/p\u003e\r\n\r\nThe 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.\r\n\r\n* Encoder usage\r\n```go\r\nimport (\r\n    \"math/rand\"\r\n    \"os\"\r\n    \"github.com/jcbritobr/pnm\"\r\n)\r\n\r\nfunc main() {\r\n    image := pnm.NewPGMImage(800, 800, 255, pnm.PGMBinary)\r\n    file, err := os.Create(\"testdata/synimage.pgm\")\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n    encoder := pnm.NewEncoder(file)\r\n\r\n    imgbuf := image.Buffer()\r\n\r\n    for i := range image.Buffer() {\r\n        data := rand.Intn(255-0) + 0\r\n        imgbuf[i] = byte(data)\r\n    }\r\n\r\n    err = encoder.Encode(image)\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n}\r\n```\r\n\r\n* Decoder usage\r\n```go\r\nimport (\r\n    \"fmt\"\r\n    \"os\"\r\n\r\n    \"github.com/jcbritobr/pnm\"\r\n)\r\n\r\nfunc main() {\r\n    var image pnm.PGMImage\r\n    file, err := os.Open(\"testdata/synimage.pgm\")\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n    decoder := pnm.NewDecoder(file, pnm.PGMBinary)\r\n    err = decoder.Decode(\u0026image)\r\n\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n}\r\n```\r\n\r\n* Using ImageBuffer and color.Model (RGBA)\r\n```go\r\nimport (\r\n    \"os\"\r\n    \"github.com/jcbritobr/pnm\"\r\n    \"github.com/jcbritobr/pnm/buffer\"\r\n    \"github.com/jcbritobr/pnm/color\"\r\n)\r\n\r\nfunc main() {\r\n    var image pnm.PPMImage\r\n    file, err := os.Open(\"testdata/tree_1.ppm\")\r\n    if err != nil {\r\n        t.Errorf(\"fail with %v\", err)\r\n    }\r\n    defer file.Close()\r\n\r\n    decoder := pnm.NewDecoder(file, pnm.PPMBinary)\r\n    err = decoder.Decode(\u0026image)\r\n    if err != nil {\r\n        t.Errorf(\"fail to decode image %v\", err)\r\n    }\r\n\r\n    imbuf := buffer.NewImageBuffer(image)\r\n    rgba := color.RGBA{}\r\n    newbuf := []byte{}\r\n    for {\r\n        n, _ := imbuf.Read(rgba[:])\r\n        if n \u003c= 0 {\r\n            break\r\n        }\r\n        r, g, b, _ := rgba.RGBA()\r\n        r = 255 - r\r\n        g = 255 - g\r\n        b = 255 - b\r\n        newbuf = append(newbuf, r)\r\n        newbuf = append(newbuf, g)\r\n        newbuf = append(newbuf, b)\r\n    }\r\n\r\n    image.SetBuffer(newbuf)\r\n\r\n    fe, err := os.Create(\"testdata/tree_3.ppm\")\r\n    if err != nil {\r\n        t.Errorf(\"fail to create file %v\", err)\r\n    }\r\n    defer fe.Close()\r\n\r\n    encoder := pnm.NewEncoder(fe)\r\n    err = encoder.Encode(\u0026image)\r\n    if err != nil {\r\n        t.Errorf(\"Encode() = %v want %v\", err, nil)\r\n    }\r\n}\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcbritobr%2Fpnm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcbritobr%2Fpnm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcbritobr%2Fpnm/lists"}