Skip to content

Commit

Permalink
#512 Changed algorithm for risizing non square cover art
Browse files Browse the repository at this point in the history
  • Loading branch information
kagemomiji committed Jun 24, 2024
1 parent 123c884 commit f9b12cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,28 @@
* @author Sindre Mehus
*/
@Controller
@RequestMapping({"/coverArt", "/ext/coverArt", "/coverArt.view", "/ext/coverArt.view"})
@RequestMapping({ "/coverArt", "/ext/coverArt", "/coverArt.view", "/ext/coverArt.view" })
public class CoverArtController {

public static final String ALBUM_COVERART_PREFIX = "al-";
public static final String ARTIST_COVERART_PREFIX = "ar-";
public static final String PLAYLIST_COVERART_PREFIX = "pl-";
public static final String PODCAST_COVERART_PREFIX = "pod-";

// Version of the cover art generation algorithm.
// Increment this if the algorithm changes.
private static final int COVERART_VERSION = 1;

static final Logger LOG = LoggerFactory.getLogger(CoverArtController.class);

@Autowired MediaFileService mediaFileService;
@Autowired CoverArtService coverArtService;
@Autowired
MediaFileService mediaFileService;
@Autowired
CoverArtService coverArtService;
@Autowired
private SettingsService settingsService;
@Autowired PlaylistService playlistService;
@Autowired
PlaylistService playlistService;
@Autowired
private CoverArtCreateService coverArtCreateService;
@Autowired
Expand All @@ -93,7 +100,8 @@ public void init() {
* get last modified time epoch millisecond
*
* @param coverArtRequest target coverArtRequest
* @return last modified time in epoch milliseconds. if coverArtRequest id null, then return -1L
* @return last modified time in epoch milliseconds. if coverArtRequest id null,
* then return -1L
*/
/*
private long getLastModifiedMiili(CoverArtRequest coverArtRequest) {
Expand Down Expand Up @@ -198,7 +206,7 @@ private void sendUnscaled(CoverArtRequest coverArtRequest, HttpServletResponse r
}

private Path getCachedImage(CoverArtRequest request, int size) throws IOException {
String hash = DigestUtils.md5Hex(request.getKey());
String hash = DigestUtils.md5Hex(request.getKey() + "-" + COVERART_VERSION);
String encoding = request.getCoverArt() != null ? "jpeg" : "png";
Path cachedImage = getImageCacheDirectory(size).resolve(hash + "." + encoding);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public BufferedImage createImage(CoverArtRequest coverArtRequest, int size) {
if (bimg == null) {
reason = "ImageIO.read";
} else {
return ImageUtil.scale(bimg, size, size);
return ImageUtil.scaleToSquare(bimg, size);
}
}
LOG.warn("Failed to process cover art {}: {} failed", coverArt, reason);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,30 @@ public static BufferedImage scale(BufferedImage image, int width, int height) {
return thumb;
}

public static BufferedImage scaleToSquare(BufferedImage image, int size) {
int w = image.getWidth();
int h = image.getHeight();
int scale = Math.max(w, h);

BufferedImage squareImage = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_RGB);
squareImage.getGraphics().drawImage(image, (scale - w) / 2, (scale - h) / 2, null);

do {
scale /= 2;
if (scale < size) {
scale = size;
}
BufferedImage temp = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(squareImage, 0, 0, temp.getWidth(), temp.getHeight(), null);
g2.dispose();

squareImage = temp;
} while (scale != size);

return squareImage;
}

}

0 comments on commit f9b12cc

Please sign in to comment.