Git Product home page Git Product logo

Comments (2)

dynarithmic avatar dynarithmic commented on June 21, 2024

I was able to get a little further using the following

#include <boost/gil.hpp>
#include <boost/gil/extension/io/bmp.hpp>
#include <fstream>
namespace gil = boost::gil;

int main()
{
    std::ifstream infile("test1bpp.bmp", std::ios::binary);
    gil::rgba8_image_t img;
    gil::read_image(infile, img, gil::bmp_tag()); // <-- Doesn't throw exception now

    // Now write the image to another bmp file
    auto v = gil::view(img);
    auto writer = gil::make_writer("test1bpp_output.bmp", gil::bmp_tag());
    gil::write_view(writer, v);  // <-- This creates a 24 bpp image, not a 1 bpp image
}

However, the code to write the image to a second bmp file results in the file being 24 bits-per-pixel, and not the original 1-bit-per-pixel as was the source image. I debugged the code, and it seems that the write_view multiplies the number of channels by 8. I am not sure this will work for 1-bit-per-pixel.

So I am stuck as to how to write the destination bmp using the same 1-bpp as the source image.

The ultimate goal is to be able to convert a 1-bpp bmp to a Group3 or Group4 TIFF, which only supports 1-bit-per-pixel image data. So far, I am able to read the data, but not able to produce a duplicate of the image, even if the image type is the same, source and destination.

from gil.

striezel avatar striezel commented on June 21, 2024

I was able to get a little further using the following

#include <boost/gil.hpp>
#include <boost/gil/extension/io/bmp.hpp>
#include <fstream>
namespace gil = boost::gil;

int main()
{
    std::ifstream infile("test1bpp.bmp", std::ios::binary);
    gil::rgba8_image_t img;
    gil::read_image(infile, img, gil::bmp_tag()); // <-- Doesn't throw exception now
    ...

@dynarithmic:
I am actually surprised that this works, because from my understanding it should not. 🤔 It probably works, because the image is 1700 x 2000 pixels, and that multiplied by one bit for each pixel gives a number that is divisible by 32 but not by 24. Have you checked the data? It may not be what you expect.

But let's get back to the first example using gil::rgb8_image_t. You can actually make that work by using read_and_convert_image() instead of read_image():

#include <boost/gil.hpp>
#include <boost/gil/extension/io/bmp.hpp>
#include <fstream>

namespace gil = boost::gil;

int main()
{
    std::ifstream infile("test1bpp.bmp", std::ios::binary);
    gil::rgb8_image_t img;
    gil::read_and_convert_image(infile, img, gil::bmp_tag());
}

The advantage of read_and_convert_image() over read_image() is that it converts the read image data (in that case: 1 bpp grayscale) to the given image type (in that case: 24 bpp RGB), if GIL can do that, thus potentially avoiding problems with wrong image types.

As far as writing BMP images is concerned, GIL currently only supports writing of 24 bit RGB and 32 bit RGBA bitmaps. At least that is how I interpret that code:

// Write support
template< typename Channel
, typename ColorSpace
>
struct bmp_write_support : write_support_false
{};
template<>
struct bmp_write_support<uint8_t
, rgb_t
> : write_support_true {};
template<>
struct bmp_write_support<uint8_t
, rgba_t
> : write_support_true {};
} // namespace detail

from gil.

Related Issues (20)

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.