Code Monkey home page Code Monkey logo

ppmkit's Introduction

PPMKit

Simple read/write PPM files (P3/P6) in Swift (macOS, iOS, tvOS, watchOS, macCatalyst, Linux).

Swift Package Manager

Read a PPM file

Load a PPM file as a CGImage

let ppmData = Data(...)
let cgImage = try PPM.readImage(data: ppmData)

Load raw PPM image data from a file

let image: PPM.Image = try PPM.read(fileURL: ppm1URL)
// width = image.width
// height = image.height
// rawRGBBytes = image.data 

Write a PPM file

Write a CGImage to a PPM file

NOTE: PPM doesn't support transparency, so any transparency information is lost.

let cgImage = CGImage(...)
let data = try PPM.writeImage(cgImage, format: .P3)

Write raw RGB data to a PPM file

let image = PPM.Image(...)
try image.write(format: .P3, fileURL: <some_url>)

Create PPM data from raw pixels

public static func image(rgbData: [PPM.RGB], width: Int, height: Int) throws -> PPM.Image

Note: The number of pixels in the rgbData array must be equal to width * height

let pixels: [PPM.RGB] = [ ... ]
let image = PPM.image(rgbData: pixels, width: 100, height: 100)

Linux support

  • Linux only supports very naive color conversions between RGB-CMYK-Gray.

To build/test linux support using a mac

See: Testing Swift packages on Linux using Docker

  1. Install Docker Desktop for Mac on your mac
  2. Make sure that docker is running (or else the next command will fail with a weird or no error message)
  3. Run the following command in the directory you want to mirror in your linux
docker run --rm --privileged --interactive --tty --volume "$(pwd):/src" --workdir "/src" swift:latest

Now, from within the docker container, run

swift build
swift test

Note that the /src directory in the Linux container is a direct mirror of the current directory on the host OS, not a copy. If you delete a file in /src in the Linux container, that file will be gone on the host OS, too.

License

MIT License

Copyright (c) 2022 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

ppmkit's People

Contributors

dagronf avatar

Stargazers

 avatar

Watchers

 avatar

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.