mirror of
https://github.com/markuryy/shark.git
synced 2026-06-18 18:41:03 +00:00
feat: re-encode cover images for rockbox compatibility
This commit is contained in:
@@ -2,8 +2,10 @@ use id3::{
|
||||
frame::{Picture, PictureType},
|
||||
Tag as ID3Tag, TagLike, Version,
|
||||
};
|
||||
use image::codecs::jpeg::JpegEncoder;
|
||||
use metaflac::Tag as FlacTag;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::Cursor;
|
||||
use std::path::Path;
|
||||
|
||||
/// Metadata structure for audio file tagging
|
||||
@@ -356,3 +358,20 @@ fn detect_mime_type_str(data: &[u8]) -> &'static str {
|
||||
"image/jpeg"
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-encode image data as a baseline (non-progressive) JPEG in sRGB.
|
||||
/// This ensures compatibility with players like Rockbox that can't
|
||||
/// decode progressive JPEGs or non-sRGB colorspaces.
|
||||
pub fn reencode_cover_image(data: &[u8], quality: u8) -> Result<Vec<u8>, String> {
|
||||
let img = image::load_from_memory(data)
|
||||
.map_err(|e| format!("Failed to decode image: {}", e))?;
|
||||
|
||||
let rgb = img.to_rgb8();
|
||||
|
||||
let mut buf = Cursor::new(Vec::new());
|
||||
let encoder = JpegEncoder::new_with_quality(&mut buf, quality);
|
||||
rgb.write_with_encoder(encoder)
|
||||
.map_err(|e| format!("Failed to encode JPEG: {}", e))?;
|
||||
|
||||
Ok(buf.into_inner())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user