Code Monkey home page Code Monkey logo

tinypgm's Introduction

⚠️ This repository has moved to: https://gitlab.com/mbitsnbites/tinypgm

tinypgm

About

This is a small C library for loading and saving 8-bit PGM (portable graymap) images.

Building

Use CMake to build tinypgm, preferrably out-of-tree. E.g.

cd my/build/dir
cmake -G Ninja -DCMAKE_INSTALL_PREFIX=path/to/my/install path/to/tinypgm
ninja install

Usage

Loading an image

Here is an example of loading an image, including all the necessary error handling:

#include <stdio.h>
#include <stdlib.h>
#include <tinypgm.h>

#define MY_PGM_FILE "hello.pgm"

int main() {
  tpgm_info_t info;
  char* data;

  /* Get image information. */
  if (!tpgm_load_info(MY_PGM_FILE, &info)) {
    fprintf(stderr, "Failed to load image.\n");
    return 1;
  }
  printf("Image dimensions: %dx%d (max value: %d)\n",
      info.width, info.height, info.max_value);

  /* Allocate memory for the image data. */
  data = (char*)malloc(info.data_size);
  if (!data) {
    fprintf(stderr, "Failed to allocate memory for the image data.\n");
    return 1;
  }

  /* Load the image data. */
  if (!tpgm_load_data(MY_PGM_FILE, NULL, data, info.data_size)) {
    fprintf(stderr, "Failed to load the image data.\n");
    free(data);
    return 1;
  }

  /* Do something with the image data. */
  /* ... */

  /* Free the image data. */
  free(data);

  return 0;
}

Saving an image

Here is an example of saving an image, including all the necessary error handling:

#include <stdio.h>
#include <tinypgm.h>

#define MY_PGM_FILE "hello.pgm"
#define IMAGE_WIDTH 1280
#define IMAGE_HEIGHT 768

int main() {
  tpgm_info_t info;
  char* data;

  /* Allocate memory for the image. */
  data = (char*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT);
  if (!data) {
    fprintf(stderr, "Failed to allocate memory for the image.\n");
    return 1;
  }

  /* Fill out the image data somehow. */
  /* ... */

  /* Save the image data. */
  if (!tpgm_save_data(MY_PGM_FILE, data, IMAGE_WIDTH, IMAGE_HEIGHT, 0)) {
    fprintf(stderr, "Failed to save the image data.\n");
    free(data);
    return 1;
  }

  /* Free the image data. */
  free(data);

  return 0;
}

License

The library is released under the zlib/libpng license. See COPYING.

tinypgm's People

Contributors

mbitsnbites avatar

Stargazers

Samuel Gomes avatar  avatar  avatar  avatar Max Lv avatar DENIS DOS SANTOS SILVA avatar

Watchers

James Cloos avatar  avatar DENIS DOS SANTOS SILVA 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.