feat: re-encode cover images for rockbox compatibility

This commit is contained in:
Markury
2026-06-03 20:39:20 -04:00
parent 2beae8e327
commit 3d7d3ded1c
3 changed files with 37 additions and 3 deletions

View File

@@ -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())
}