From eb5ed757ee5c1917e30bd7b48f680ef4def54fe1 Mon Sep 17 00:00:00 2001 From: sharkAndshark Date: Fri, 29 Nov 2024 01:03:47 +0800 Subject: [PATCH] wip --- martin/src/cog/mod.rs | 59 ++++++++++++--------- tests/fixtures/cog/{rgb8.tif => rgb_8.tif} | Bin 43968 -> 44928 bytes 2 files changed, 33 insertions(+), 26 deletions(-) rename tests/fixtures/cog/{rgb8.tif => rgb_8.tif} (96%) diff --git a/martin/src/cog/mod.rs b/martin/src/cog/mod.rs index 4ecbe4a0b..fc774e3dc 100644 --- a/martin/src/cog/mod.rs +++ b/martin/src/cog/mod.rs @@ -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}; @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -303,7 +303,7 @@ impl Source for CogSource { data_width, data_height, 1, - true, + (true, u8::MAX), &self.path, ), (_, _) => Err(CogError::NotSupportedColorTypeAndBitDepth( @@ -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() @@ -354,7 +354,7 @@ fn to_png>( data_width: u32, data_height: u32, components_count: u32, - need_extra_alpha: bool, + extra_alpha_info: (bool, T), path: &Path, ) -> Result, CogError> { let is_padded = data_width != tile_width; @@ -370,23 +370,27 @@ fn to_png>( let no_data = T::from(0); - let data: Vec = if let (false, false) = (is_padded, need_extra_alpha) { + let data: Vec = 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; } } } @@ -608,7 +612,7 @@ fn get_images_ifd(decoder: &mut Decoder) -> Vec { #[cfg(test)] mod tests { - use std::path::PathBuf; + use std::{fs::File, io::Write, path::PathBuf}; use martin_tile_utils::{TileCoord, TileInfo}; use tilejson::tilejson; @@ -619,8 +623,7 @@ mod tests { #[actix_rt::test] async fn can_get_tile() -> () { - let path = - PathBuf::from("/home/zettakit/repos/martin/tests/fixtures/cog/tiled_cog_chengdu.tif"); + let path = PathBuf::from("../tests/fixtures/cog/output1.tif"); let meta = get_meta(&path).unwrap(); let source = super::CogSource { id: "test".to_string(), @@ -633,8 +636,12 @@ mod tests { }, }; let query = None; - let _tile = source.get_tile(TileCoord { z: 2, x: 1, y: 1 }, query).await; + let _tile = source.get_tile(TileCoord { z: 0, x: 0, y: 0 }, 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!() } diff --git a/tests/fixtures/cog/rgb8.tif b/tests/fixtures/cog/rgb_8.tif similarity index 96% rename from tests/fixtures/cog/rgb8.tif rename to tests/fixtures/cog/rgb_8.tif index 44c16b96fab41dcfc44638d8649db2f5e47e44cd..e4a1c050812f5cb7e0612a1a162a454610deed04 100644 GIT binary patch delta 1083 zcma)*%SyvQ7={0|Dx$@OPY~-qoog;o3!x1XXo{FvTv-BkXGO8Nji}JAK7g)@U^gQA z0QD(cx)6L0|CG|Ewl+;Rhh)w|0M4HQ zd?gE6)Ls0rag!V9qh)~W+Z^CfxJ3QZ$%2ad3H56y16!znP+xU3@Q&tQ9EQ*8;8ieN zf#Pn(u6oUOcpSFF(weFk-FCC3MyFw`xs&$;J8=ELE&C16K6JeUFRxyNt+NySNAe{m zWw8*C9~-BAj$NBDQG;lvDT&2()3o&20a3EqtxYwXVzw_~^MgvosZUg!8kK|-VKHHH zoz02B{&>-KyXsaNd(M8@sRa{Jl#n_0e~{6Ikwpn+v5QiT3Tf(08Wvvw`d?(2L5Yz% z;}NrHxXI`f8ka2Jg(=44!PK70C`g!q^%%b>& tV~9>u*E?c1L>5=UrB9N^HGzSDYz>pDW9^>X%R^?Ryr-&2;