0
\$\begingroup\$

I have 1-bit monochrome image which I have to put into a BYTE buffer. The monochrome image is kept to a dynamic memory of OLE_COLOR. I have to copy the color data to BYTE buffer. Before moving into trial and error, I would like to share if my algorithm is something that will do as I am expecting worth a review.

    const long stride = 4;
    const long height = 32;
    const gsl::span<const OLE_COLOR> textureColors = {colorParam, gsl::narrow_cast<gsl::span<OLE_COLOR>::index_type>(width * height)};
    std::vector<BYTE> texture;
    texture.resize(gsl::narrow_cast<size_t>(stride * height));
    gsl::span<BYTE> colors = {texture};
    for (long y = 0; y < height; ++y)
    {
        const auto bitmapRow = textureColors.subspan(y, gsl::narrow_cast<gsl::span<OLE_COLOR>::index_type>(width));
        const auto textureRow = colors.subspan(y * stride, stride);
        for (long s = 0; s < stride; ++s)
        {
            auto& destColors = *textureRow.subspan(s, 1).data();
            for (int b = 0; b < 8; ++b)
            {
                const auto srcColor = *bitmapRow.subspan(s * 8 + b, 1).data();
                destColors += gsl::narrow_cast<BYTE>((destColors << b) | srcColor);
            }
        }
    }
\$\endgroup\$
5
  • \$\begingroup\$ Yes. The code is working. But I have faced other issues later using this data which is not related to this issue. Yes, I have to resize. You can see I have put the allocated memory to gsl::span I have access that memory later in loop for write. \$\endgroup\$ Jan 30 '20 at 10:07
  • \$\begingroup\$ @Juho Did you mean something like the getColors() ? I have edited. \$\endgroup\$ Jan 30 '20 at 10:14
  • \$\begingroup\$ Are you sure this works? getColors returns a span (non-owning view) of a local variable texture, that won't exist outside of the scope of getColors. \$\endgroup\$
    – user673679
    Jan 30 '20 at 12:05
  • \$\begingroup\$ I have restored back the original code. Someone suggested as review but looks like removed his comments already. The above code is working code. \$\endgroup\$ Jan 30 '20 at 12:56
  • 1
    \$\begingroup\$ I would like to take a closer look. I need to know what headers you are including. Something for gsl? this? github.com/microsoft/GSL and something for OLE_COLOR? (sorry I am on Linux but can switch if required) If you can turn your algorithm into an executable with a main() and provide maybe a binary blob of the input image via an external link. Ideally also a blob file of the output image. Basically you need a "test-harness" to test your code. That will be essential for validating your algorithm, and for us to review it. \$\endgroup\$ Jan 31 '20 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.