Git Product home page Git Product logo

tinyppm's Introduction

tinyppm

Simple .ppm loader written in Rust.

This is more of a toy project, I have written, to be able to easy reuse it for some of my experiments with 2d graphics in Rust.

Usage

v.0.20.0 Note: This version introduces proper error handling. Earlier tinyppm was a bit of pain as it exited when something unexpected happened (unrecognized header, unsupported color depth, non-existing/unreadable file specified...). Currently tinyppm returns an error in case some problem occurs. It is responsibility of the consumer to decide if this is critical or not, and take appropriate action.

Note: the above change will require some slight modification of code that relies on tinyppm 0.1.x.

  1. Add tinyppm to your Cargo.toml
  2. Call read_image_data:
extern crate tinyppm;

fn my_function(filename: &String) {
    let ppm_image_result = tinyppm::ppm_loader::read_image_data(filename);
    let ppm_image = match ppm_image_result {
        Ok(image) => image,
        _ => panic!("unable to read specified image file!"),
    };
    // `ppm_image` is now a struct containing image with, height and pixels 
}

The structure returned is defined as follows:

pub struct PPMImage {
    height: usize,
    width: usize,
    pixels: Vec<u32>,
}

and it exposes 3 public methods:

    pub fn height() -> usize {
        // returns image height
    }

    pub fn width() -> usize {
        // returns image width
    }

    pub fn pixels() -> &Vec<u32> {
        // returns reference to buffer containing pixels
    } 

Details:

  • only 'raw ppm' format is supported (the most popular format of ppm. More details: ppm format specification ).

  • tinyppm supports only True Color images (i.e. 24bits per pixel - 3 color channels & 8 bits per channel). After the image is read it is converted to RGB+A format (32bpp) so that it is ready to be pushed directly to framebuffer.

Additional examples:

If you want to check how it works, please have a look at ppm_viewer which is simple image viewer written in Rust: https://github.com/MaciekTalaska/2d_effects/tree/master/ppm_viewer

License

This code is released under the MIT license.

tinyppm's People

Contributors

maciektalaska 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.