Convert System::Drawing::Bitmap^ to Pix* (leptonica)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a Bitmap in memory which gets passed into my library from a C# application. I need to convert it into a leptonica Pix* to pass it into tesseract. Conversely, I'm going to have to take the Pix* from the thresholded image and convert it back to a Bitmap to return it to the C# application but I'm not there yet. Here's my code to go from Bitmap to Pix:
Pix* OcrProcessor::ToPix(Bitmap^ bitmap)
const auto rect = new Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
const auto bitmap_data = bitmap->LockBits(*rect, ImageLockMode::ReadOnly, bitmap->PixelFormat);
auto ptr = bitmap_data->Scan0;
const auto bytes = Math::Abs(bitmap_data->Stride) * bitmap->Height;
const auto rgb_values = gcnew array<unsigned char>(bytes);
Marshal::Copy(ptr, rgb_values, 0, bytes);
auto handle = GCHandle::Alloc(rgb_values, GCHandleType::Pinned);
const auto data = static_cast<unsigned char*>(static_cast<void*>(handle.AddrOfPinnedObject()));
const auto converted = pixReadMemBmp(data, bytes);
bitmap->UnlockBits(bitmap_data);
delete rect;
return converted;
The Pix* converted is always null at the end. Stepping through the code, rgb_values is always an array filled with 255 throughout instead of being filled with the bitmap data. I'm not heavily invested in this particular method, if anyone has a working method to accomplish what I'm looking for.
visual-c++ c++-cli leptonica
add a comment |
I have a Bitmap in memory which gets passed into my library from a C# application. I need to convert it into a leptonica Pix* to pass it into tesseract. Conversely, I'm going to have to take the Pix* from the thresholded image and convert it back to a Bitmap to return it to the C# application but I'm not there yet. Here's my code to go from Bitmap to Pix:
Pix* OcrProcessor::ToPix(Bitmap^ bitmap)
const auto rect = new Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
const auto bitmap_data = bitmap->LockBits(*rect, ImageLockMode::ReadOnly, bitmap->PixelFormat);
auto ptr = bitmap_data->Scan0;
const auto bytes = Math::Abs(bitmap_data->Stride) * bitmap->Height;
const auto rgb_values = gcnew array<unsigned char>(bytes);
Marshal::Copy(ptr, rgb_values, 0, bytes);
auto handle = GCHandle::Alloc(rgb_values, GCHandleType::Pinned);
const auto data = static_cast<unsigned char*>(static_cast<void*>(handle.AddrOfPinnedObject()));
const auto converted = pixReadMemBmp(data, bytes);
bitmap->UnlockBits(bitmap_data);
delete rect;
return converted;
The Pix* converted is always null at the end. Stepping through the code, rgb_values is always an array filled with 255 throughout instead of being filled with the bitmap data. I'm not heavily invested in this particular method, if anyone has a working method to accomplish what I'm looking for.
visual-c++ c++-cli leptonica
That function looks pretty funky, pixReadMemBmp() needs to know how the bitmap is organized to interpret the pixel data correctly. The pixel format matters a lot. Its implementation is not encouraging. But unsurprising. If you can't find a better way in that library then you may have to save it to a temp file.
– Hans Passant
Nov 15 '18 at 15:34
add a comment |
I have a Bitmap in memory which gets passed into my library from a C# application. I need to convert it into a leptonica Pix* to pass it into tesseract. Conversely, I'm going to have to take the Pix* from the thresholded image and convert it back to a Bitmap to return it to the C# application but I'm not there yet. Here's my code to go from Bitmap to Pix:
Pix* OcrProcessor::ToPix(Bitmap^ bitmap)
const auto rect = new Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
const auto bitmap_data = bitmap->LockBits(*rect, ImageLockMode::ReadOnly, bitmap->PixelFormat);
auto ptr = bitmap_data->Scan0;
const auto bytes = Math::Abs(bitmap_data->Stride) * bitmap->Height;
const auto rgb_values = gcnew array<unsigned char>(bytes);
Marshal::Copy(ptr, rgb_values, 0, bytes);
auto handle = GCHandle::Alloc(rgb_values, GCHandleType::Pinned);
const auto data = static_cast<unsigned char*>(static_cast<void*>(handle.AddrOfPinnedObject()));
const auto converted = pixReadMemBmp(data, bytes);
bitmap->UnlockBits(bitmap_data);
delete rect;
return converted;
The Pix* converted is always null at the end. Stepping through the code, rgb_values is always an array filled with 255 throughout instead of being filled with the bitmap data. I'm not heavily invested in this particular method, if anyone has a working method to accomplish what I'm looking for.
visual-c++ c++-cli leptonica
I have a Bitmap in memory which gets passed into my library from a C# application. I need to convert it into a leptonica Pix* to pass it into tesseract. Conversely, I'm going to have to take the Pix* from the thresholded image and convert it back to a Bitmap to return it to the C# application but I'm not there yet. Here's my code to go from Bitmap to Pix:
Pix* OcrProcessor::ToPix(Bitmap^ bitmap)
const auto rect = new Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
const auto bitmap_data = bitmap->LockBits(*rect, ImageLockMode::ReadOnly, bitmap->PixelFormat);
auto ptr = bitmap_data->Scan0;
const auto bytes = Math::Abs(bitmap_data->Stride) * bitmap->Height;
const auto rgb_values = gcnew array<unsigned char>(bytes);
Marshal::Copy(ptr, rgb_values, 0, bytes);
auto handle = GCHandle::Alloc(rgb_values, GCHandleType::Pinned);
const auto data = static_cast<unsigned char*>(static_cast<void*>(handle.AddrOfPinnedObject()));
const auto converted = pixReadMemBmp(data, bytes);
bitmap->UnlockBits(bitmap_data);
delete rect;
return converted;
The Pix* converted is always null at the end. Stepping through the code, rgb_values is always an array filled with 255 throughout instead of being filled with the bitmap data. I'm not heavily invested in this particular method, if anyone has a working method to accomplish what I'm looking for.
visual-c++ c++-cli leptonica
visual-c++ c++-cli leptonica
asked Nov 15 '18 at 14:27
Dewey VozelDewey Vozel
992816
992816
That function looks pretty funky, pixReadMemBmp() needs to know how the bitmap is organized to interpret the pixel data correctly. The pixel format matters a lot. Its implementation is not encouraging. But unsurprising. If you can't find a better way in that library then you may have to save it to a temp file.
– Hans Passant
Nov 15 '18 at 15:34
add a comment |
That function looks pretty funky, pixReadMemBmp() needs to know how the bitmap is organized to interpret the pixel data correctly. The pixel format matters a lot. Its implementation is not encouraging. But unsurprising. If you can't find a better way in that library then you may have to save it to a temp file.
– Hans Passant
Nov 15 '18 at 15:34
That function looks pretty funky, pixReadMemBmp() needs to know how the bitmap is organized to interpret the pixel data correctly. The pixel format matters a lot. Its implementation is not encouraging. But unsurprising. If you can't find a better way in that library then you may have to save it to a temp file.
– Hans Passant
Nov 15 '18 at 15:34
That function looks pretty funky, pixReadMemBmp() needs to know how the bitmap is organized to interpret the pixel data correctly. The pixel format matters a lot. Its implementation is not encouraging. But unsurprising. If you can't find a better way in that library then you may have to save it to a temp file.
– Hans Passant
Nov 15 '18 at 15:34
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321614%2fconvert-systemdrawingbitmap-to-pix-leptonica%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321614%2fconvert-systemdrawingbitmap-to-pix-leptonica%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That function looks pretty funky, pixReadMemBmp() needs to know how the bitmap is organized to interpret the pixel data correctly. The pixel format matters a lot. Its implementation is not encouraging. But unsurprising. If you can't find a better way in that library then you may have to save it to a temp file.
– Hans Passant
Nov 15 '18 at 15:34