Coder Social home page Coder Social logo

jindingwang / cbmp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattflow/cbmp

0.0 0.0 0.0 1.06 MB

C library for reading, manipulating, and saving BMP images.

Home Page: https://mattflow.com/cbmp

Shell 0.67% C++ 85.17% Python 11.39% C 0.72% Makefile 0.65% CMake 0.76% M4 0.64%

cbmp's Introduction

C BMP

Build Status

C Library to read, manipulate, and save BMP images.

Image must be a valid BMP format with 24 or 32 bit pixel data.

Usage

In order to use the library, move cbmp.c and cbmp.h into your project. Link the cbmp.c file when compiling and include the cbmp.h file at the top of the file you are using it in.

Example

This example reads a BMP image and applies an invert to the pixels. It then saves the inverted image to a new file.

#include <stdlib.h>
#include <stdio.h>
#include "cbmp.h"

int main(int argc, char** argv)
{

    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
        exit(1);
    }

    // Read image into BMP struct
    BMP* bmp = bopen(argv[1]);

    unsigned int x, y, width, height;
    unsigned char r, g, b;

    // Gets image width in pixels
    width = get_width(bmp);

    // Gets image height in pixels
    height = get_height(bmp);

    for (x = 0; x < width; x++)
    {
        for (y = 0; y < height; y++)
        {
            // Gets pixel rgb values at point (x, y)
            get_pixel_rgb(bmp, x, y, &r, &g, &b);

            // Sets pixel rgb values at point (x, y)
            set_pixel_rgb(bmp, x, y, 255 - r, 255 - g, 255 - b);
        }
    }

    // Write bmp contents to file
    bwrite(bmp, argv[2]);

    // Free memory
    bclose(bmp);

    return 0;
}

Full API

// Reads a BMP image and returns a poiner to a BMP struct
BMP* bopen(char* file_path);

// Returns a deep copy of the BMP passed
BMP* b_deep_copy(BMP* to_copy);

// Takes a BMP pointer and returns the image width in pixels
int get_width(BMP* bmp);

// Takes a BMP pointer and returns the image height in pixels
int get_height(BMP* bmp);

// Takes a BMP pointer and returns the image depth in bytes
unsigned int get_depth(BMP* bmp);

// Takes a BMP pointer, (x, y), and sets the values of rgb pointers
void get_pixel_rgb(BMP* bmp, int x, int y, unsigned char* r, unsigned char* g, unsigned char* b);

// Takes a BMP pointer, (x, y), and rgb values and updates the pixel color
void set_pixel_rgb(BMP* bmp, int x, int y, unsigned char r, unsigned char g, unsigned char b);

// Writes the contents of the BMP pointer to the given file path
void bwrite(BMP* bmp, char* file_name);

// Frees all memory allocated for the BMP pointer
void bclose(BMP* bmp);

Testing

In order to run tests, create a build directory and enter it:

mkdir build
cd build

Generate Makefiles with cmake:

cmake ..

Build executable:

make

Run the tests executable:

./tests

cbmp's People

Contributors

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