Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkAndshark committed Nov 29, 2024
1 parent a2e4ba2 commit 1c51f29
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 56 deletions.
5 changes: 5 additions & 0 deletions martin/src/args/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ impl Args {
config.mbtiles = parse_file_args(&mut cli_strings, "mbtiles", false);
}

#[cfg(feature = "cog")]
if !cli_strings.is_empty() {
config.cog = parse_file_args(&mut cli_strings, "tif", false);
}

#[cfg(feature = "sprites")]
if !self.extras.sprite.is_empty() {
config.sprites = FileConfigEnum::new(self.extras.sprite);
Expand Down
117 changes: 62 additions & 55 deletions martin/src/cog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use regex::Regex;
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use std::vec;
use std::{fmt::Debug, path::PathBuf};
use std::{u8, vec};

use std::io::BufWriter;
use tiff::decoder::{Decoder, DecodingResult};
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::U8(vec), tiff::ColorType::RGB(_)) => to_png(
Expand All @@ -141,7 +141,7 @@ impl Source for CogSource {
data_width,
data_height,
3,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::U8(vec), tiff::ColorType::RGBA(_)) => to_png(
Expand All @@ -153,7 +153,7 @@ impl Source for CogSource {
data_width,
data_height,
4,
false,
(false, u8::MAX),
&self.path,
),
(DecodingResult::U16(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -165,7 +165,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u16::MAX),
&self.path,
),
(DecodingResult::U16(vec), tiff::ColorType::RGB(_)) => to_png(
Expand All @@ -177,7 +177,7 @@ impl Source for CogSource {
data_width,
data_height,
3,
true,
(true, u16::MAX),
&self.path,
),
(DecodingResult::U16(vec), tiff::ColorType::RGBA(_)) => to_png(
Expand All @@ -189,7 +189,7 @@ impl Source for CogSource {
data_width,
data_height,
4,
false,
(false, u16::MAX),
&self.path,
),
(DecodingResult::U32(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -208,7 +208,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::F32(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -227,7 +227,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::F64(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -246,7 +246,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::I8(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -265,7 +265,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::I16(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -284,7 +284,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(DecodingResult::I32(vec), tiff::ColorType::Gray(_)) => to_png(
Expand All @@ -303,7 +303,7 @@ impl Source for CogSource {
data_width,
data_height,
1,
true,
(true, u8::MAX),
&self.path,
),
(_, _) => Err(CogError::NotSupportedColorTypeAndBitDepth(
Expand All @@ -329,7 +329,7 @@ where
vec.iter()
.enumerate()
.map(|(i, &value)| {
let sample_index = (i % samples_count as usize);
let sample_index = i % samples_count as usize;
let min = min_values
.get(sample_index)
.copied()
Expand All @@ -354,7 +354,7 @@ fn to_png<T: Copy + NoUninit + From<u8>>(
data_width: u32,
data_height: u32,
components_count: u32,
need_extra_alpha: bool,
extra_alpha_info: (bool, T),
path: &Path,
) -> Result<Vec<u8>, CogError> {
let is_padded = data_width != tile_width;
Expand All @@ -370,23 +370,27 @@ fn to_png<T: Copy + NoUninit + From<u8>>(

let no_data = T::from(0);

let data: Vec<T> = if let (false, false) = (is_padded, need_extra_alpha) {
let data: Vec<T> = if let (false, false) = (is_padded, extra_alpha_info.0) {
vec
} else {
let components_count_of_result = if need_extra_alpha {
let components_count_for_result = if extra_alpha_info.0 {
components_count + 1
} else {
components_count
};
let mut result =
vec![no_data; (tile_width * tile_height * (components_count_of_result)) as usize];
vec![no_data; (tile_width * tile_height * (components_count_for_result)) as usize];
for row in 0..data_height {
for col in 0..data_width {
let idx = row * data_width * components_count + col * components_count;
let arr_idx = row * tile_width * components_count + col * components_count;
let idx_of_chunk = row * data_width * components_count + col * components_count;
let idx_of_result = row * tile_width * components_count_for_result
+ col * components_count_for_result;
for component_idx in 0..components_count {
result[(arr_idx + component_idx) as usize] =
vec[(idx + component_idx) as usize];
result[(idx_of_result + component_idx) as usize] =
vec[(idx_of_chunk + component_idx) as usize];
}
if extra_alpha_info.0 {
result[(idx_of_result + components_count) as usize] = extra_alpha_info.1;
}
}
}
Expand Down Expand Up @@ -606,36 +610,39 @@ fn get_images_ifd(decoder: &mut Decoder<File>) -> Vec<usize> {
res
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use martin_tile_utils::{TileCoord, TileInfo};
use tilejson::tilejson;

use crate::Source;

use super::get_meta;

#[actix_rt::test]
async fn can_get_tile() -> () {
let path =
PathBuf::from("/home/zettakit/repos/martin/tests/fixtures/cog/tiled_cog_chengdu.tif");
let meta = get_meta(&path).unwrap();
let source = super::CogSource {
id: "test".to_string(),
path,
meta,
tilejson: tilejson! {tiles: vec![] },
tileinfo: TileInfo {
format: martin_tile_utils::Format::Png,
encoding: martin_tile_utils::Encoding::Uncompressed,
},
};
let query = None;
let _tile = source.get_tile(TileCoord { z: 2, x: 1, y: 1 }, query).await;
let _bytes = _tile.unwrap();

todo!()
}
}
// #[cfg(test)]
// mod tests {
// use std::{fs::File, io::Write, path::PathBuf};

// use martin_tile_utils::{TileCoord, TileInfo};
// use tilejson::tilejson;

// use crate::Source;

// use super::get_meta;

// #[actix_rt::test]
// async fn can_get_tile() -> () {
// let path = PathBuf::from("../tests/fixtures/cog/rgb8.tif");
// let meta = get_meta(&path).unwrap();
// let source = super::CogSource {
// id: "test".to_string(),
// path,
// meta,
// tilejson: tilejson! {tiles: vec![] },
// tileinfo: TileInfo {
// format: martin_tile_utils::Format::Png,
// encoding: martin_tile_utils::Encoding::Uncompressed,
// },
// };
// let query = None;
// let _tile = source.get_tile(TileCoord { z: 3, x: 1, y: 1 }, query).await;
// let _bytes = _tile.unwrap();
// //write this bytes to a "result.png" file

// let mut file = File::create("result.png").unwrap();
// file.write_all(&_bytes).unwrap();

// todo!()
// }
// }
Binary file modified tests/fixtures/cog/rgb8.tif
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ LOG_FILE="${LOG_DIR}/${TEST_NAME}.txt"
TEST_OUT_DIR="${TEST_OUT_BASE_DIR}/${TEST_NAME}"
mkdir -p "$TEST_OUT_DIR"

ARG=(--default-srid 900913 --auto-bounds calc --save-config "${TEST_OUT_DIR}/save_config.yaml" tests/fixtures/mbtiles tests/fixtures/pmtiles "$STATICS_URL/webp2.pmtiles" --sprite tests/fixtures/sprites/src1 --font tests/fixtures/fonts/overpass-mono-regular.ttf --font tests/fixtures/fonts)
ARG=(--default-srid 900913 --auto-bounds calc --save-config "${TEST_OUT_DIR}/save_config.yaml" tests/fixtures/mbtiles tests/fixtures/pmtiles tests/fixtures/cog "$STATICS_URL/webp2.pmtiles" --sprite tests/fixtures/sprites/src1 --font tests/fixtures/fonts/overpass-mono-regular.ttf --font tests/fixtures/fonts)
export DATABASE_URL="$MARTIN_DATABASE_URL"

set -x
Expand Down

0 comments on commit 1c51f29

Please sign in to comment.