aboutsummaryrefslogtreecommitdiff
path: root/vendor/png/examples/png-generate.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/png/examples/png-generate.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/png/examples/png-generate.rs')
-rw-r--r--vendor/png/examples/png-generate.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/vendor/png/examples/png-generate.rs b/vendor/png/examples/png-generate.rs
new file mode 100644
index 0000000..9036a04
--- /dev/null
+++ b/vendor/png/examples/png-generate.rs
@@ -0,0 +1,55 @@
+// For reading and opening files
+use png::text_metadata::{ITXtChunk, ZTXtChunk};
+use std::env;
+use std::fs::File;
+use std::io::BufWriter;
+
+fn main() {
+ let path = env::args()
+ .nth(1)
+ .expect("Expected a filename to output to.");
+ let file = File::create(path).unwrap();
+ let w = &mut BufWriter::new(file);
+
+ let mut encoder = png::Encoder::new(w, 2, 1); // Width is 2 pixels and height is 1.
+ encoder.set_color(png::ColorType::Rgba);
+ encoder.set_depth(png::BitDepth::Eight);
+ // Adding text chunks to the header
+ encoder
+ .add_text_chunk(
+ "Testing tEXt".to_string(),
+ "This is a tEXt chunk that will appear before the IDAT chunks.".to_string(),
+ )
+ .unwrap();
+ encoder
+ .add_ztxt_chunk(
+ "Testing zTXt".to_string(),
+ "This is a zTXt chunk that is compressed in the png file.".to_string(),
+ )
+ .unwrap();
+ encoder
+ .add_itxt_chunk(
+ "Testing iTXt".to_string(),
+ "iTXt chunks support all of UTF8. Example: हिंदी.".to_string(),
+ )
+ .unwrap();
+
+ let mut writer = encoder.write_header().unwrap();
+
+ let data = [255, 0, 0, 255, 0, 0, 0, 255]; // An array containing a RGBA sequence. First pixel is red and second pixel is black.
+ writer.write_image_data(&data).unwrap(); // Save
+
+ // We can add a tEXt/zTXt/iTXt at any point before the encoder is dropped from scope. These chunks will be at the end of the png file.
+ let tail_ztxt_chunk = ZTXtChunk::new(
+ "Comment".to_string(),
+ "A zTXt chunk after the image data.".to_string(),
+ );
+ writer.write_text_chunk(&tail_ztxt_chunk).unwrap();
+
+ // The fields of the text chunk are public, so they can be mutated before being written to the file.
+ let mut tail_itxt_chunk = ITXtChunk::new("Author".to_string(), "सायंतन खान".to_string());
+ tail_itxt_chunk.compressed = true;
+ tail_itxt_chunk.language_tag = "hi".to_string();
+ tail_itxt_chunk.translated_keyword = "लेखक".to_string();
+ writer.write_text_chunk(&tail_itxt_chunk).unwrap();
+}