Code Monkey home page Code Monkey logo

pnm's Introduction

PNM( Portable Anymap Format ) AKA Netpbm

Status

  • Done and ready for use.
    • Implemented: P1, P2, P3, P4, P5, P6

Description

  • PBM, PGM and PPM reader and writer

Key features

  • Headers only
  • One file to include
  • One file to handle
  • No third parties
  • No additional binaries
  • Out of the box ready
  • No need to recompile or start some install process.
  • API Statistics.
    • Number of namespaces: 1( one )
    • Number of functions: 3 ( three )
    • Number of Classes/Structures: 1 ( one )
    • Number of operators: 2 ( two )

Load image in to std::vector

 std::ifstream ifs( "image.pbm", std::ios_base::binary );
 std::vector<std::uint8_t > data;
 PNM::Info info;

 ifs >> PNM::load( data, info );
 if( true == info.valid() )
  {
    std::cout << "width   = "  << info.width ()    << std::endl;
    std::cout << "height  = "  << info.height()    << std::endl;
    std::cout << "max     = "  << info.maximum()   << std::endl;
    std::cout << "channel = "  << info.channel()   << std::endl;
    std::cout << "type    = "  << (int)info.type() << std::endl;
   // data contain valid information
  }
  // One-liner version. 
  { std::ifstream( "image.pbm", std::ios_base::binary ) >> PNM::load( data, info ); }

Save image from std::vector

 std::ofstream ofs("image1.pbm");
 std::vector<std::uint8_t > data;

 //< In here fill data with something >

 ofs << PNM::save( data, 100, 100, PNM::P1 );

 // One-liner version
 { std::ofstream("image2.pbm") << PNM::save( data, 100, 100, PNM::P1 ); }

Load data to raw memory by using custom allocator:

 std::uint8_t *my_allocator( size_t const& size )
  {
   return (std::uint8_t *) malloc( size );
  }

 std::ifstream ifs( "image.pbm", std::ios_base::binary );
 std::uint8_t *data;
 PNM::Info info;

 ifs >> PNM::load( &data, my_allocator, info );
 if( true == info.valid() )
  {
   std::cout << "width   = "  << info.width ()    << std::endl;
   std::cout << "height  = "  << info.height()    << std::endl;
   std::cout << "max     = "  << info.maximum()   << std::endl;
   std::cout << "channel = "  << info.channel()   << std::endl;
   std::cout << "type    = "  << (int)info.type() << std::endl;
   // data contain valid information allocated by my_allocator
  }

  // One-liner version. 
  { std::ifstream( "image.pbm", std::ios_base::binary ) >> PNM::load( &data, my_allocator, info ); }

Load data from already allocated memory

 std::ifstream ifs( "image.pbm", std::ios_base::binary );
 std::uint8_t *data = malloc( 100 * 100 * 3 );
 PNM::Info info;

 ifs >> PNM::load( data, info );
 if( true == info.valid() )
  {
   std::cout << "width   = "  << info.width ()    << std::endl;
   std::cout << "height  = "  << info.height()    << std::endl;
   std::cout << "max     = "  << info.maximum()   << std::endl;
   std::cout << "channel = "  << info.channel()   << std::endl;
   std::cout << "type    = "  << (int)info.type() << std::endl;
   // data contain valid information allocated by my_allocator
  }

  // One-liner version. 
  { std::ifstream( "image.pbm", std::ios_base::binary ) >> PNM::load( data, info ); }

Save data from raw memory

 std::ofstream ofs( "image1.pgm" );
 std::uint8_t data[ 100 * 100 ];
 //< In here fill data with something >

 ofs << PNM::save( data, 100, 100, PNM::P2 );

 // One-liner version
 { std::ofstream( "image2.pgm" ) << PNM::save( data, 100, 100, PNM::P2 ); }

Probe the file

 std::ifstream ifs( "image.pbm", std::ios_base::binary );
 PNM::Info info;

 ifs >> PNM::probe( info );
 if( true == info.valid() )
  {
   std::cout << "width   = "  << info.width ()    << std::endl;
   std::cout << "height  = "  << info.height()    << std::endl;
   std::cout << "max     = "  << info.maximum()   << std::endl;
   std::cout << "channel = "  << info.channel()   << std::endl;
   std::cout << "type    = "  << (int)info.type() << std::endl;
  }

 // One-liner version
 { std::ifstream( "image.pbm" )>> PNM::probe( info ); }

Install

  1. Clone this Repository:
    Examples:
  2. Inform compiler where to find headers:
    Examples:
    • MSVC : /Ic:\my-work-folder\my-git-folder\PNM\src
    • gcc : -I/home/my-user-name/my-work-folder/my-git-folder/PNM/src

Links

Tested against

  • gcc 6.4.0
  • MSVC 2015
  • MSVC 2017

pnm's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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