未验证 提交 9ae97508 编写于 作者: Z zdenop 提交者: GitHub

Merge pull request #1551 from stweil/bigendian

Fix Tesseract for big endian machines
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#ifndef TESSERACT_CCUTIL_HELPERS_H_ #ifndef TESSERACT_CCUTIL_HELPERS_H_
#define TESSERACT_CCUTIL_HELPERS_H_ #define TESSERACT_CCUTIL_HELPERS_H_
#include <cassert>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <functional> #include <functional>
...@@ -187,6 +188,7 @@ inline int IntCastRounded(float x) { ...@@ -187,6 +188,7 @@ inline int IntCastRounded(float x) {
// Reverse the order of bytes in a n byte quantity for big/little-endian switch. // Reverse the order of bytes in a n byte quantity for big/little-endian switch.
inline void ReverseN(void* ptr, int num_bytes) { inline void ReverseN(void* ptr, int num_bytes) {
assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
char* cptr = static_cast<char*>(ptr); char* cptr = static_cast<char*>(ptr);
int halfsize = num_bytes / 2; int halfsize = num_bytes / 2;
for (int i = 0; i < halfsize; ++i) { for (int i = 0; i < halfsize; ++i) {
......
...@@ -42,14 +42,12 @@ Input::~Input() { ...@@ -42,14 +42,12 @@ Input::~Input() {
// Writes to the given file. Returns false in case of error. // Writes to the given file. Returns false in case of error.
bool Input::Serialize(TFile* fp) const { bool Input::Serialize(TFile* fp) const {
if (!Network::Serialize(fp)) return false; return Network::Serialize(fp) && shape_.Serialize(fp);
if (fp->FWrite(&shape_, sizeof(shape_), 1) != 1) return false;
return true;
} }
// Reads from the given file. Returns false in case of error. // Reads from the given file. Returns false in case of error.
bool Input::DeSerialize(TFile* fp) { bool Input::DeSerialize(TFile* fp) {
return fp->FReadEndian(&shape_, sizeof(shape_), 1) == 1; return shape_.DeSerialize(fp);
} }
// Returns an integer reduction factor that the network applies to the // Returns an integer reduction factor that the network applies to the
......
...@@ -59,18 +59,40 @@ class StaticShape { ...@@ -59,18 +59,40 @@ class StaticShape {
height_, width_, depth_, loss_type_); height_, width_, depth_, loss_type_);
} }
bool DeSerialize(TFile *fp) {
int32_t tmp;
bool result =
fp->FReadEndian(&batch_, sizeof(batch_), 1) == 1 &&
fp->FReadEndian(&height_, sizeof(height_), 1) == 1 &&
fp->FReadEndian(&width_, sizeof(width_), 1) == 1 &&
fp->FReadEndian(&depth_, sizeof(depth_), 1) == 1 &&
fp->FReadEndian(&tmp, sizeof(tmp), 1) == 1;
loss_type_ = static_cast<LossType>(tmp);
return result;
}
bool Serialize(TFile *fp) const {
int32_t tmp = loss_type_;
return
fp->FWrite(&batch_, sizeof(batch_), 1) == 1 &&
fp->FWrite(&height_, sizeof(height_), 1) == 1 &&
fp->FWrite(&width_, sizeof(width_), 1) == 1 &&
fp->FWrite(&depth_, sizeof(depth_), 1) == 1 &&
fp->FWrite(&tmp, sizeof(tmp), 1) == 1;
}
private: private:
// Size of the 4-D tensor input/output to a network. A value of zero is // Size of the 4-D tensor input/output to a network. A value of zero is
// allowed for all except depth_ and means to be determined at runtime, and // allowed for all except depth_ and means to be determined at runtime, and
// regarded as variable. // regarded as variable.
// Number of elements in a batch, or number of frames in a video stream. // Number of elements in a batch, or number of frames in a video stream.
int batch_; int32_t batch_;
// Height of the image. // Height of the image.
int height_; int32_t height_;
// Width of the image. // Width of the image.
int width_; int32_t width_;
// Depth of the image. (Number of "nodes"). // Depth of the image. (Number of "nodes").
int depth_; int32_t depth_;
// How to train/interpret the output. // How to train/interpret the output.
LossType loss_type_; LossType loss_type_;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册