contrek 1.4.7 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d28d3b71b3d867f2899e2f49654decf11a0da8d5d7fa2fff5e08cb3eba265ab
4
- data.tar.gz: acee94a59b33f775cd35f0bd28fa1f76b9cf1bce254cb51ff728c7f20b56dcb3
3
+ metadata.gz: a01ebf8c61e86b070d300b47d3e0f82cc8a4912bbaf738af24c080bd81d2763d
4
+ data.tar.gz: 5661528101dc8492975b3a4dc7765f02b0ae884d6ba7fecbaf132676b53f4b9f
5
5
  SHA512:
6
- metadata.gz: f43fbf8121cabf433296aaf9a2ea52a77a9564271a3a34a6954600ebc06004602b9a81a53ec784de49ec32e0a5dd70e015d405359627ce97c28df969d29c5c71
7
- data.tar.gz: de2c37ca93f6096d202360995b752300a7c69680a50e9a06f2f4e23f9b983afc3ed51f06b05ef6e2a96f2066e1c477896b83cb1e6c0c184237ba8ba9a80ea31c
6
+ metadata.gz: 702ea4857ad48e5817d4baa3c33b1183094720a1a8ffa80b7830e85f8b7a80f4e1a4b994f71eb1f5f1ccc91bf71cab874e90e7196f768dd6fca9e28222e7a106
7
+ data.tar.gz: 1bec64c8a85057c87b0b46920ff09503c815cfef8992c0e06d1eace83c43ae8def25dab9529464f12c7c65085479514756fe924b0e1ad9c1e0a8f8fbd56fb3a7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- contrek (1.4.7)
4
+ contrek (1.4.8)
5
5
  chunky_png (~> 1.4)
6
6
  concurrent-ruby (~> 1.3.5)
7
7
  ffi (~> 1.17)
@@ -23,6 +23,8 @@ else()
23
23
  endif()
24
24
  find_package(ZLIB REQUIRED)
25
25
  message(STATUS "Contrek: ZLIB path found at ${ZLIB_LIBRARIES}")
26
+ find_package(TIFF REQUIRED)
27
+ find_library(GEOTIFF_LIBRARY NAMES geotiff REQUIRED)
26
28
  file(GLOB_RECURSE CPP_SOURCES "*.cpp")
27
29
  file(GLOB_RECURSE C_SOURCES "*.c")
28
30
  list(FILTER CPP_SOURCES EXCLUDE REGEX "examples/.*\\.cpp")
@@ -37,8 +39,10 @@ target_include_directories(ContrekLib PUBLIC
37
39
  ${ALL_INCLUDE_DIRS}
38
40
  ${ZLIB_INCLUDE_DIRS}
39
41
  )
40
- target_link_libraries(ContrekLib PRIVATE
41
- ${ZLIB_LIBRARIES}
42
+ target_link_libraries(ContrekLib PUBLIC
43
+ ZLIB::ZLIB
44
+ TIFF::TIFF
45
+ ${GEOTIFF_LIBRARY}
42
46
  pthread
43
47
  )
44
48
  option(BUILD_EXAMPLES "Build the example application" OFF)
@@ -30,6 +30,7 @@
30
30
  #include "polygon/bitmaps/spng.h"
31
31
  #include "polygon/bitmaps/streaming/RasterStreamer.h"
32
32
  #include "polygon/bitmaps/streaming/PngSource.h"
33
+ #include "polygon/bitmaps/streaming/TiffSource.h"
33
34
  #include "polygon/matchers/Matcher.h"
34
35
  #include "polygon/matchers/RGBMatcher.h"
35
36
  #include "polygon/matchers/RGBNotMatcher.h"
@@ -452,3 +453,72 @@ void Tests::test_l() {
452
453
  // stream_progressive_png_image("../images/test_20480x20480.png", 2000);
453
454
  std::cout << "Memory usage peak: " << get_peak_rss() << " MB" << std::endl;
454
455
  }
456
+
457
+ ProcessResult* stream_progressive_png_image_to_geojson(const std::string& filepath, const std::string& geojson_filepath, uint32_t stripe_height) {
458
+ ProcessResult* merged_result = nullptr;
459
+
460
+ Options varguments = {
461
+ {"unsafe_mode", true},
462
+ {"compress", Options{
463
+ {"linear", true},
464
+ }},
465
+ };
466
+
467
+ TiffSource source(filepath);
468
+ if (stripe_height >= source.height()) {
469
+ throw std::invalid_argument("stripe_height must be smaller than image height");
470
+ }
471
+
472
+ RasterStreamer streamer(source, stripe_height);
473
+ // allocates stripe buffer
474
+ RawBitmap stripe_bitmap(source.width(), stripe_height);
475
+
476
+ // allocates streaming buffer
477
+ std::ofstream shared_stream(geojson_filepath, std::ios::out | std::ios::binary);
478
+ if (!shared_stream) {
479
+ std::cerr << "Error: Unable creating output streaming file!" << std::endl;
480
+ return nullptr;
481
+ }
482
+
483
+ std::vector<char> buffer(4 * 1024 * 1024); // Buffer (4MB)
484
+ shared_stream.rdbuf()->pubsetbuf(buffer.data(), buffer.size());
485
+ GeoJsonStreamingMerger vmerger(0, varguments, &shared_stream);
486
+
487
+ try {
488
+ uint32_t processed_rows = 0;
489
+ bool first = true;
490
+ int stripe_count = 0;
491
+ // main stripes loop
492
+ streamer.each(stripe_bitmap, [&](Bitmap& bitmap, uint32_t buffer_rows, std::size_t, std::size_t) {
493
+ // stripe contour tracing
494
+ Options finder_options = {
495
+ {"processing_height", static_cast<int>(buffer_rows)},
496
+ {"versus", Identifier{"a"}},
497
+ {"bounds", true},
498
+ {"multiclass", true},
499
+ {"connectivity", 8}
500
+ };
501
+ PolygonFinder polygon_finder(&bitmap, nullptr, nullptr, finder_options);
502
+ ProcessResult *result = polygon_finder.process_info();
503
+ if (result) {
504
+ std::cout << "stripe " << stripe_count << ": found polygons " << result->groups << std::endl;
505
+ processed_rows += buffer_rows - (first ? 0 : RasterStreamer::OVERLAP);
506
+ vmerger.add_tile(*result, processed_rows == source.height());
507
+ delete result;
508
+ }
509
+ stripe_count++;
510
+ first = false;
511
+ });
512
+ merged_result = vmerger.process_info();
513
+ std::cout << "total found polygons " << merged_result->groups << std::endl;
514
+ } catch (const std::exception& e) {
515
+ std::cerr << "\n[ERROR] Processing exception: " << e.what() << std::endl;
516
+ if (shared_stream.is_open()) shared_stream.close();
517
+ }
518
+ return merged_result;
519
+ }
520
+
521
+ void Tests::test_m() {
522
+ stream_progressive_png_image_to_geojson("../images/conus_clip_100x100.tif", "output.geojson", 20);
523
+ std::cout << "Memory usage peak: " << get_peak_rss() << " MB" << std::endl;
524
+ }
@@ -21,4 +21,5 @@ class Tests {
21
21
  virtual void test_h();
22
22
  virtual void test_i();
23
23
  virtual void test_l();
24
+ virtual void test_m();
24
25
  };
@@ -14,6 +14,9 @@
14
14
  #include <geotiff/geokeys.h>
15
15
  #include <geotiff/xtiffio.h>
16
16
  #include <tiffio.h>
17
+ #include <algorithm>
18
+ #include <cstring>
19
+ #include <limits>
17
20
  #include <stdexcept>
18
21
  #include <string>
19
22
 
@@ -30,7 +33,12 @@ TiffSource::TiffSource(const std::string& filepath, bool suppress_warnings)
30
33
  photometric_(0),
31
34
  planar_config_(0),
32
35
  sample_format_(0),
33
- current_row_(0) {
36
+ current_row_(0),
37
+ tiled_(false),
38
+ tile_width_(0),
39
+ tile_height_(0),
40
+ cached_tile_start_(0),
41
+ tile_cache_valid_(false) {
34
42
  if (suppress_warnings) {
35
43
  TIFFSetWarningHandler(nullptr);
36
44
  }
@@ -46,8 +54,19 @@ TiffSource::TiffSource(const std::string& filepath, bool suppress_warnings)
46
54
 
47
55
  const tmsize_t scanline_size = TIFFScanlineSize(tiff_);
48
56
  if (scanline_size <= 0) throw std::runtime_error("Invalid TIFF scanline size");
49
-
50
57
  source_row_.resize(static_cast<std::size_t>(scanline_size));
58
+
59
+ if (tiled_) {
60
+ TIFFGetField(tiff_, TIFFTAG_TILEWIDTH, &tile_width_);
61
+ TIFFGetField(tiff_, TIFFTAG_TILELENGTH, &tile_height_);
62
+ if (!tile_width_ || !tile_height_) throw std::runtime_error("Invalid TIFF tile dimensions");
63
+ const std::size_t row_bytes = static_cast<std::size_t>(width_) * samples_per_pixel_ * (bits_per_sample_ / 8);
64
+ if (row_bytes > source_row_.size())
65
+ throw std::runtime_error("TIFF scanline buffer is smaller than the decoded pixel row");
66
+ if (row_bytes > std::numeric_limits<std::size_t>::max() / tile_height_)
67
+ throw std::runtime_error("TIFF tile cache too large");
68
+ tile_band_.resize(row_bytes * tile_height_);
69
+ }
51
70
  }
52
71
  catch (...) {
53
72
  XTIFFClose(tiff_);
@@ -91,7 +110,7 @@ void TiffSource::read_image_info() {
91
110
  TIFFGetFieldDefaulted(tiff_, TIFFTAG_PLANARCONFIG, &planar_config_);
92
111
  TIFFGetFieldDefaulted(tiff_, TIFFTAG_SAMPLEFORMAT, &sample_format_);
93
112
 
94
- if (TIFFIsTiled(tiff_)) throw std::runtime_error("Tiled TIFF is not supported yet");
113
+ tiled_ = TIFFIsTiled(tiff_) != 0;
95
114
  if (planar_config_ != PLANARCONFIG_CONTIG) throw std::runtime_error("Unsupported TIFF planar configuration");
96
115
  if (bits_per_sample_ != 8 && bits_per_sample_ != 16) throw std::runtime_error("Unsupported TIFF bits per sample");
97
116
  if (samples_per_pixel_ != 1 && samples_per_pixel_ != 3 && samples_per_pixel_ != 4) throw std::runtime_error("Unsupported TIFF samples per pixel");
@@ -99,9 +118,13 @@ void TiffSource::read_image_info() {
99
118
 
100
119
  if (photometric_ != PHOTOMETRIC_MINISWHITE &&
101
120
  photometric_ != PHOTOMETRIC_MINISBLACK &&
102
- photometric_ != PHOTOMETRIC_RGB) {
121
+ photometric_ != PHOTOMETRIC_RGB &&
122
+ photometric_ != PHOTOMETRIC_PALETTE) {
103
123
  throw std::runtime_error("Unsupported TIFF photometric");
104
124
  }
125
+ if (photometric_ == PHOTOMETRIC_PALETTE && (samples_per_pixel_ != 1 || bits_per_sample_ != 8)) {
126
+ throw std::runtime_error("Only 8-bit single-band palette TIFF is supported");
127
+ }
105
128
  }
106
129
 
107
130
  void TiffSource::read_geo_localization() {
@@ -191,6 +214,39 @@ void TiffSource::read_crs() {
191
214
  GTIFFree(gtif);
192
215
  }
193
216
 
217
+ void TiffSource::load_tile_band(uint32_t tile_start) {
218
+ const std::size_t bytes_per_pixel = static_cast<std::size_t>(samples_per_pixel_) *
219
+ (bits_per_sample_ / 8);
220
+ const std::size_t row_bytes = static_cast<std::size_t>(width_) * bytes_per_pixel;
221
+ const tmsize_t tile_bytes = TIFFTileSize(tiff_);
222
+ const tmsize_t tile_row_bytes = TIFFTileRowSize(tiff_);
223
+ if (tile_bytes <= 0 || tile_row_bytes <= 0)
224
+ throw std::runtime_error("Invalid TIFF tile buffer size");
225
+
226
+ std::vector<unsigned char> tile(static_cast<std::size_t>(tile_bytes));
227
+ const uint32_t valid_rows = std::min(tile_height_, height_ - tile_start);
228
+ for (uint32_t x = 0; x < width_; x += tile_width_) {
229
+ const ttile_t index = TIFFComputeTile(tiff_, x, tile_start, 0, 0);
230
+ if (TIFFReadEncodedTile(tiff_, index, tile.data(), tile_bytes) < 0) {
231
+ throw std::runtime_error("TIFFReadEncodedTile failed at x=" +
232
+ std::to_string(x) + ", y=" + std::to_string(tile_start));
233
+ }
234
+ const uint32_t valid_columns = std::min(tile_width_, width_ - x);
235
+ const std::size_t copy_bytes = static_cast<std::size_t>(valid_columns) * bytes_per_pixel;
236
+ if (copy_bytes > static_cast<std::size_t>(tile_row_bytes) ||
237
+ (static_cast<std::size_t>(valid_rows - 1) * static_cast<std::size_t>(tile_row_bytes) + copy_bytes) > tile.size())
238
+ throw std::runtime_error("Unexpected TIFF tile layout");
239
+ for (uint32_t y = 0; y < valid_rows; ++y) {
240
+ std::memcpy(tile_band_.data() + static_cast<std::size_t>(y) * row_bytes +
241
+ static_cast<std::size_t>(x) * bytes_per_pixel,
242
+ tile.data() + static_cast<std::size_t>(y) * static_cast<std::size_t>(tile_row_bytes),
243
+ copy_bytes);
244
+ }
245
+ }
246
+ cached_tile_start_ = tile_start;
247
+ tile_cache_valid_ = true;
248
+ }
249
+
194
250
  bool TiffSource::read_next_row(unsigned char* destination, std::size_t row_size) {
195
251
  if (current_row_ >= height_) return false;
196
252
 
@@ -200,7 +256,16 @@ bool TiffSource::read_next_row(unsigned char* destination, std::size_t row_size)
200
256
  throw std::runtime_error("Invalid TIFF destination row size");
201
257
  }
202
258
 
203
- if (TIFFReadScanline(tiff_, source_row_.data(), current_row_, 0) < 0) {
259
+ if (tiled_) {
260
+ const uint32_t tile_start = (current_row_ / tile_height_) * tile_height_;
261
+ if (!tile_cache_valid_ || tile_start != cached_tile_start_) {
262
+ load_tile_band(tile_start);
263
+ }
264
+ const std::size_t raw_row_bytes = static_cast<std::size_t>(width_) *
265
+ samples_per_pixel_ * (bits_per_sample_ / 8);
266
+ const std::size_t offset = static_cast<std::size_t>(current_row_ - tile_start) * raw_row_bytes;
267
+ std::memcpy(source_row_.data(), tile_band_.data() + offset, raw_row_bytes);
268
+ } else if (TIFFReadScanline(tiff_, source_row_.data(), current_row_, 0) < 0) {
204
269
  throw std::runtime_error("TIFFReadScanline failed at row " + std::to_string(current_row_));
205
270
  }
206
271
 
@@ -237,6 +302,20 @@ void TiffSource::convert_row(unsigned char* destination) {
237
302
  return;
238
303
  }
239
304
 
305
+ // Palette TIFF: preserve the original numeric class index, not its display RGB color.
306
+ // Alpha stays opaque; NoData (e.g. 0) must be handled separately by the caller.
307
+ if (photometric_ == PHOTOMETRIC_PALETTE) {
308
+ for (uint32_t x = 0; x < width_; ++x) {
309
+ const unsigned char value = source_row_[x];
310
+ const std::size_t offset = static_cast<std::size_t>(x) * 4;
311
+ destination[offset] = value;
312
+ destination[offset + 1] = value;
313
+ destination[offset + 2] = value;
314
+ destination[offset + 3] = 255;
315
+ }
316
+ return;
317
+ }
318
+
240
319
  if (samples_per_pixel_ == 1 && bits_per_sample_ == 8) {
241
320
  for (uint32_t x = 0; x < width_; ++x) {
242
321
  unsigned char value = source_row_[x];
@@ -37,6 +37,7 @@ class TiffSource : public RasterSource {
37
37
  void read_model_transformation(double* matrix, uint16_t count);
38
38
  void read_crs();
39
39
  void convert_row(unsigned char* destination);
40
+ void load_tile_band(uint32_t tile_start);
40
41
  TIFF* tiff_;
41
42
  uint32_t width_;
42
43
  uint32_t height_;
@@ -48,4 +49,10 @@ class TiffSource : public RasterSource {
48
49
  uint32_t current_row_;
49
50
  std::vector<unsigned char> source_row_;
50
51
  GeoLocalization geo_localization_;
52
+ bool tiled_;
53
+ uint32_t tile_width_;
54
+ uint32_t tile_height_;
55
+ uint32_t cached_tile_start_;
56
+ bool tile_cache_valid_;
57
+ std::vector<unsigned char> tile_band_;
51
58
  };
@@ -214,7 +214,7 @@ void NodeCluster::plot(int versus) {
214
214
 
215
215
  if (next_node != nullptr)
216
216
  { inner_sequence.push_back(next_node->coords_entering_to(first, inner_v, Node::INNER));
217
- Node* last_node = plot_inner_node(inner_sequence, next_node, inner_v, first, root_node);
217
+ Node* last_node = plot_inner_node(inner_sequence, next_node, inner_v, first, root_node, next_node);
218
218
  Point last_coord = last_node->coords_entering_to(first, versus_inverter[inner_v], Node::INNER);
219
219
  if (inner_sequence.back() != last_coord) {
220
220
  inner_sequence.push_back(last_coord);
@@ -283,7 +283,7 @@ std::pair<int, int> NodeCluster::test_in_hole_o(Node* node)
283
283
  return {-1, -1};
284
284
  }
285
285
 
286
- Node* NodeCluster::plot_inner_node(std::vector<Point>& sequence_coords, Node *node, int versus, Node *stop_at, Node *start_node) {
286
+ Node* NodeCluster::plot_inner_node(std::vector<Point>& sequence_coords, Node *node, int versus, Node *stop_at, Node *start_node, Node *initial_node) {
287
287
  Node *current_node = node;
288
288
 
289
289
  while (current_node != nullptr) {
@@ -335,7 +335,7 @@ Node* NodeCluster::plot_inner_node(std::vector<Point>& sequence_coords, Node *no
335
335
  } else {
336
336
  inner_new->remove(current_node);
337
337
  }
338
- if (next_node == stop_at) {
338
+ if (next_node == stop_at && stop_at->my_next_inner(current_node, versus) == initial_node) {
339
339
  if (plot) {
340
340
  next_node->assign_lateral_inner_index(current_node, stop_at, versus);
341
341
  }
@@ -25,7 +25,7 @@ struct pf_Options;
25
25
  class NodeCluster {
26
26
  private:
27
27
  void plot_node(std::vector<Point>& sequence_coords, Node *node, Node *start_node, int versus, RectBounds& bounds);
28
- Node* plot_inner_node(std::vector<Point>& sequence_coords, Node *node, int versus, Node *stop_at, Node *start_node);
28
+ Node* plot_inner_node(std::vector<Point>& sequence_coords, Node *node, int versus, Node *stop_at, Node *start_node, Node *initial_node);
29
29
  std::vector<Node*> plot_sequence;
30
30
  List *inner_plot;
31
31
  List *inner_new;
@@ -156,11 +156,16 @@ void Cursor::traverse_inner(Part* act_part, std::vector<Part*>& all_parts, std::
156
156
  { EndPoint* current_end_point = static_cast<Position*>(act_part->head)->end_point();
157
157
  for (auto dest_part_p : current_end_point->queues()) {
158
158
  Part* dest_part = static_cast<Part*>(dest_part_p);
159
- if (dest_part->polyline()->tile == act_part->polyline()->tile) {
159
+ auto* source_polyline = act_part->polyline();
160
+ auto* dest_polyline = dest_part->polyline();
161
+ if ((dest_polyline->tile == source_polyline->tile) ||
162
+ dest_polyline->class_id() != source_polyline->class_id()) {
160
163
  continue;
161
164
  }
162
165
  int dest_part_versus = dest_part->versus();
163
- if (dest_part_versus != 0 && dest_part_versus == act_part->versus()) continue;
166
+ if (dest_part_versus != 0 && dest_part_versus == act_part->versus()) {
167
+ continue;
168
+ }
164
169
 
165
170
  tracked_end_points.push_back(current_end_point);
166
171
  Shape* shape = dest_part->polyline()->shape;
@@ -130,7 +130,8 @@ module Contrek
130
130
  eligibles = act_part.head.end_point.queues.select { |p| p.polyline.tile != act_part.polyline.tile }
131
131
  eligibles.each do |dest_part|
132
132
  dest_part_versus = dest_part.versus
133
- next if dest_part_versus != 0 && dest_part_versus == act_part.versus
133
+ next if (dest_part_versus != 0 && dest_part_versus == act_part.versus) ||
134
+ dest_part.polyline.class_id != act_part.polyline.class_id
134
135
 
135
136
  tracked_end_points << act_part.head.end_point
136
137
  shape = dest_part.polyline.shape
@@ -154,7 +154,7 @@ module Contrek
154
154
 
155
155
  if !next_node.nil?
156
156
  @sequence_coords << next_node.coords_entering_to(first, inner_v, Contrek::Finder::Node::INNER)
157
- last_node = plot_inner_node(next_node, inner_v, first, root_node)
157
+ last_node = plot_inner_node(next_node, inner_v, first, root_node, next_node)
158
158
  last_coord = last_node.coords_entering_to(first, VERSUS_INVERTER[inner_v], Contrek::Finder::Node::INNER)
159
159
  @sequence_coords << last_coord if @sequence_coords.last != last_coord
160
160
  end
@@ -232,7 +232,7 @@ module Contrek
232
232
  # inner way
233
233
  # nodes in @plot_sequence
234
234
  # coordinates in @sequence_coords
235
- def plot_inner_node(node, versus, stop_at, start_node)
235
+ def plot_inner_node(node, versus, stop_at, start_node, initial_node)
236
236
  node.outer_index = start_node.outer_index
237
237
  @root_nodes.delete(node)
238
238
  @inner_plot.delete(node)
@@ -272,11 +272,11 @@ module Contrek
272
272
  else
273
273
  @inner_new.delete(node)
274
274
  end
275
- if next_node == stop_at
275
+ if next_node == stop_at && stop_at.my_next(node, versus, :inner) == initial_node
276
276
  next_node.assign_lateral_inner_index(node, stop_at, versus) if plot
277
277
  return node
278
278
  end
279
- plot_inner_node(next_node, versus, stop_at, start_node)
279
+ plot_inner_node(next_node, versus, stop_at, start_node, initial_node)
280
280
  end
281
281
 
282
282
  # contour tracing core logic loop
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Contrek
4
- VERSION = "1.4.7"
4
+ VERSION = "1.4.8"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contrek
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.7
4
+ version: 1.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emanuele Cesaroni