~ubuntu-branches/ubuntu/utopic/tesseract/utopic-proposed

« back to all changes in this revision

Viewing changes to classify/shapetable.cpp

  • Committer: Package Import Robot
  • Author(s): Jeff Breidenbach
  • Date: 2012-10-23 23:25:05 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20121023232505-01pfq6trlgqtagwj
Tags: 3.02.02-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
namespace tesseract {
30
30
 
31
31
// Writes to the given file. Returns false in case of error.
32
 
bool UnicharAndFonts::Serialize(FILE* fp) {
33
 
  inT32 uni_id = unichar_id;
34
 
  if (fwrite(&uni_id, sizeof(uni_id), 1, fp) != 1) return false;
 
32
bool UnicharAndFonts::Serialize(FILE* fp) const {
 
33
  if (fwrite(&unichar_id, sizeof(unichar_id), 1, fp) != 1) return false;
35
34
  if (!font_ids.Serialize(fp)) return false;
36
35
  return true;
37
36
}
38
37
// Reads from the given file. Returns false in case of error.
39
38
// If swap is true, assumes a big/little-endian swap is needed.
40
39
bool UnicharAndFonts::DeSerialize(bool swap, FILE* fp) {
41
 
  inT32 uni_id;
42
 
  if (fread(&uni_id, sizeof(uni_id), 1, fp) != 1) return false;
 
40
  if (fread(&unichar_id, sizeof(unichar_id), 1, fp) != 1) return false;
43
41
  if (swap)
44
 
    ReverseN(&uni_id, sizeof(uni_id));
45
 
  unichar_id = uni_id;
 
42
    ReverseN(&unichar_id, sizeof(unichar_id));
46
43
  if (!font_ids.DeSerialize(swap, fp)) return false;
47
44
  return true;
48
45
}
55
52
}
56
53
 
57
54
// Writes to the given file. Returns false in case of error.
58
 
bool Shape::Serialize(FILE* fp) {
59
 
  if (fwrite(&unichars_sorted_, sizeof(unichars_sorted_), 1, fp) != 1)
 
55
bool Shape::Serialize(FILE* fp) const {
 
56
  uinT8 sorted = unichars_sorted_;
 
57
  if (fwrite(&sorted, sizeof(sorted), 1, fp) != 1)
60
58
    return false;
61
59
  if (!unichars_.SerializeClasses(fp)) return false;
62
60
  return true;
64
62
// Reads from the given file. Returns false in case of error.
65
63
// If swap is true, assumes a big/little-endian swap is needed.
66
64
bool Shape::DeSerialize(bool swap, FILE* fp) {
67
 
  if (fread(&unichars_sorted_, sizeof(unichars_sorted_), 1, fp) != 1)
 
65
  uinT8 sorted;
 
66
  if (fread(&sorted, sizeof(sorted), 1, fp) != 1)
68
67
    return false;
 
68
  unichars_sorted_ = sorted != 0;
69
69
  if (!unichars_.DeSerializeClasses(swap, fp)) return false;
70
70
  return true;
71
71
}
197
197
  const Shape& shape = GetShape(shape_id);
198
198
  STRING result;
199
199
  result.add_str_int("Shape", shape_id);
 
200
  if (shape.size() > 100) {
 
201
    result.add_str_int(" Num unichars=", shape.size());
 
202
    return result;
 
203
  }
200
204
  for (int c = 0; c < shape.size(); ++c) {
201
205
    result.add_str_int(" c_id=", shape[c].unichar_id);
202
206
    result += "=";
203
207
    result += unicharset_->id_to_unichar(shape[c].unichar_id);
204
 
    result.add_str_int(", ", shape[c].font_ids.size());
205
 
    result += " fonts =";
206
 
    for (int f = 0; f < shape[c].font_ids.size(); ++f) {
207
 
      result.add_str_int(" ", shape[c].font_ids[f]);
 
208
    if (shape.size() < 10) {
 
209
      result.add_str_int(", ", shape[c].font_ids.size());
 
210
      result += " fonts =";
 
211
      int num_fonts = shape[c].font_ids.size();
 
212
      if (num_fonts > 10) {
 
213
        result.add_str_int(" ", shape[c].font_ids[0]);
 
214
        result.add_str_int(" ... ", shape[c].font_ids[num_fonts - 1]);
 
215
      } else {
 
216
        for (int f = 0; f < num_fonts; ++f) {
 
217
          result.add_str_int(" ", shape[c].font_ids[f]);
 
218
        }
 
219
      }
208
220
    }
209
221
  }
210
222
  return result;
327
339
}
328
340
 
329
341
// Returns true if the shapes are already merged.
330
 
bool ShapeTable::AlreadyMerged(int shape_id1, int shape_id2) {
 
342
bool ShapeTable::AlreadyMerged(int shape_id1, int shape_id2) const {
331
343
  return MasterDestinationIndex(shape_id1) == MasterDestinationIndex(shape_id2);
332
344
}
333
345
 
334
346
// Returns true if any shape contains multiple unichars.
335
 
bool ShapeTable::AnyMultipleUnichars() {
 
347
bool ShapeTable::AnyMultipleUnichars() const {
336
348
  int num_shapes = NumShapes();
337
349
  for (int s1 = 0; s1 < num_shapes; ++s1) {
338
350
    if (MasterDestinationIndex(s1) != s1) continue;
408
420
  shape_table_[master_id2]->set_destination_index(master_id1);
409
421
  // Add all the shapes of master_id2 to master_id1.
410
422
  shape_table_[master_id1]->AddShape(*shape_table_[master_id2]);
411
 
  tprintf("Merged shape %d->%d, %d->%d, now with %d unichars: %s\n",
412
 
          shape_id1, master_id1, shape_id2, master_id2,
413
 
          shape_table_[master_id1]->size(),
414
 
          DebugStr(master_id1).string());
415
423
}
416
424
 
417
425
// Returns the destination of this shape, (if merged), taking into account