Coder Social home page Coder Social logo

loadbmp's Introduction

LoadBMP Build Status Release License

LoadBMP is a single-header C library for loading and saving BMP image files.

LoadBMP has no no dependencies besides the standard C libraries stdlib.h, string.h and stdio.h.

Examples

LoadBMP uses raw buffers and support RGB (LOADBMP_RGB) and RGBA (LOADBMP_RGBA) formats. The order is RGBRGBRGB... or RGBARGBARGBA..., from top left to bottom right, without any padding.

The format ultimately only affects the size and layout of the pixel buffer. Thereby when loading a BMP and selecting LOADBMP_RGBA, an alpha value of 255 is added. When saving the BMP the knowledge of an alpha value is only used to be able to skip it and properly index the colors.

Loading BMP

unsigned char *pixels = NULL;
unsigned int width, height;

unsigned int err = loadbmp_decode_file("image.bmp", &pixels, &width, &height, LOADBMP_RGBA);

if (err)
	printf("LoadBMP Load Error: %u\n", err);

// Remember to free the pixel data when it isn't needed anymore.
free(pixels);

Saving BMP

unsigned char *pixels = ...;
unsigned int width, height;

unsigned int err = loadbmp_encode_file("image.bmp", pixels, width, height, LOADBMP_RGBA);

if (err)
	printf("LoadBMP Load Error: %u\n", err);

Reporting Bugs & Requests

Feel free to use the issue tracker, for reporting bugs, submitting patches or requesting features.

Before submitting bugs, make sure that you're using the latest version of LoadBMP.

License

Copyright (c) 2016 Christian Vallentin <[email protected]>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not
   be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
   distribution.

loadbmp's People

Contributors

vallentin avatar

Stargazers

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

Watchers

 avatar  avatar

loadbmp's Issues

[BUG] invalid memory access

reproduce code

Save a 28x28 gray scale image.

static void reproduce_loadbmp_bug()
{
    uint8_t data[28 * 28];

    const char* save_path = "00.bmp";
    unsigned int err = loadbmp_encode_file(save_path, data, 28, 28, 1);
    if (err)
    {
        printf("LoadBMP Load Error: %u\n", err);
    }
}

Result: invalid memory access

gcc -g -fsanitize=address -fno-omit-framepointer

(base) ➜  mac-arm64 git:(master) ✗ ./example-mnist 
=================================================================
==11051==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x00016fb5af70 at pc 0x0001032a1bc4 bp 0x00016fb5a930 sp 0x00016fb5a0e8
READ of size 3 at 0x00016fb5af70 thread T0
    #0 0x1032a1bc0 in __asan_memcpy+0x1a4 (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x3dbc0)
    #1 0x1002bc420 in loadbmp_encode_file loadbmp.h:246
    #2 0x1002c5940 in reproduce_loadbmp_bug() lenet.cpp:840
    #3 0x1002c57cc in main lenet.cpp:853
    #4 0x10048d088 in start+0x204 (dyld:arm64e+0x5088)

Address 0x00016fb5af70 is located in stack of thread T0 at offset 816 in frame
    #0 0x1002c57ec in reproduce_loadbmp_bug() lenet.cpp:835

  This frame has 1 object(s):
    [32, 816) 'data' (line 837) <== Memory access at offset 816 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x3dbc0) in __asan_memcpy+0x1a4
Shadow bytes around the buggy address:
  0x00702df8b590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b5a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b5b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b5c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b5d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x00702df8b5e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00[f3]f3
  0x00702df8b5f0: f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3
  0x00702df8b600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b610: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b620: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x00702df8b630: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==11051==ABORTING

Converting BMP to texture help

Hello,

Sorry if this isn't the right place to ask. I'm new to all of openGL and i was wondering how i could take what comes back from your function and turn it into a texture.

Thanks for your help!

Why the need of providing "components" when decoding

With components I mean the last parameter (LOADBMP_RGBA/LOADBMP_RGB)

Isn't this something you could actually get from

bmp_info_header[14];

You could even leave the components parameter but make it a reference so whoever calls the function can get the value of the component back, instead of providing it, so you can read random bmps without having to worry about their bits per pixels

Images miss-aligned and colour distorted

Hello,

I have just tested your functions and i found them really useful.

However, there seems to be a problem with slightly small size bmp images (below 500x500) where the re-encoded image seems to have a horizontal offset to the right.

I tested it with a 500x393 it bugs , while i tested it with another image 500x333 and it worked. I used both functions with some other images and i can't find the pattern. I also went through your code and tried to find what was creating the offset.

Have a nice day.

No support for alpha channel in the BMP file?

Looks like this thing can't properly load BMP files saved with an alpha channel. Looking at the code it looks indeed like that is not covered. Is it not part of the official spec or something? GIMP can, for example, save and load bitmaps in this format.

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.