Added image resizing via imagemagick

This commit is contained in:
DWW 2022-07-20 18:10:16 +03:00
parent 673e8b779f
commit 74598813de
3 changed files with 73 additions and 8 deletions

8
README
View File

@ -42,3 +42,11 @@ metaflac
--dont-use-padding --dont-use-padding
file.flac file.flac
--- imagemagick ---
https://imagemagick.org/script/download.php
For Image Resizing
magick
Cover
-resize 750x750
Cover_small.jpg

1
TODO
View File

@ -1,2 +1 @@
Add Cover Resize via lib or command ???
Implement main Implement main

View File

@ -26,6 +26,7 @@ constexpr bool IS_FLAC = false;
constexpr bool IS_JPG = true; constexpr bool IS_JPG = true;
constexpr bool IS_PNG = false; constexpr bool IS_PNG = false;
constexpr uint32_t COVER_SIZE = 750; constexpr uint32_t COVER_SIZE = 750;
constexpr const char MAGICK_INVOKE[] = "magick";
#ifdef _WIN32 #ifdef _WIN32
constexpr const char FOLDER_DELIM = '\\'; constexpr const char FOLDER_DELIM = '\\';
@ -349,13 +350,20 @@ string get_track_filename(int track_num, const string& artist,
return filename; return filename;
} }
bool should_resize_JPG(ifstream& file) bool should_resize_JPG(const string& path)
{ {
// Do a resize by default // Do a resize by default
bool result = true; bool result = true;
ifstream file;
string filename;
int marker = 0; int marker = 0;
int symbol; int symbol;
filename = path;
filename += FOLDER_DELIM;
filename += "Cover";
file.open(filename);
// Check Header 1 // Check Header 1
symbol = file.get(); symbol = file.get();
if (symbol != 0xFF) if (symbol != 0xFF)
@ -476,17 +484,21 @@ bool should_resize_JPG(ifstream& file)
} }
} }
bool should_resize_PNG(ifstream& file) bool should_resize_PNG(const string& path)
{ {
ifstream file;
string filename;
uint8_t buf[4]; uint8_t buf[4];
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
filename = path;
filename += FOLDER_DELIM;
filename += "Cover";
file.open(filename);
// Seek // Seek
file.read(reinterpret_cast<char*>(&buf), 4); file.seekg(16);
file.read(reinterpret_cast<char*>(&buf), 4);
file.read(reinterpret_cast<char*>(&buf), 4);
file.read(reinterpret_cast<char*>(&buf), 4);
// Read width // Read width
width = 0; width = 0;
@ -506,6 +518,8 @@ bool should_resize_PNG(ifstream& file)
height += buf[i]; height += buf[i];
} }
file.close();
if ((width > COVER_SIZE) || (height > COVER_SIZE)) if ((width > COVER_SIZE) || (height > COVER_SIZE))
{ {
return true; return true;
@ -543,6 +557,45 @@ bool get_cover_type(const string& path)
throw invalid_argument(filename); throw invalid_argument(filename);
} }
bool resize_cover(const string& path, bool is_jpg)
{
string cmd;
bool should_resize;
// Should we actually resize
if (is_jpg == IS_JPG)
{
should_resize = should_resize_JPG(path);
}
else
{
should_resize = should_resize_PNG(path);
}
cmd = MAGICK_INVOKE;
cmd += "\"";
cmd += path;
cmd += FOLDER_DELIM;
cmd += "Cover";
cmd += "\" ";
if (should_resize)
{
cmd += "-resize ";
cmd += to_string(COVER_SIZE);
cmd += "x";
cmd += to_string(COVER_SIZE);
cmd += " ";
}
cmd += "\"";
cmd += path;
cmd += FOLDER_DELIM;
cmd += "Cover_small.jpg\"";
return system_command(cmd);
}
bool rename_cover(const string& path, bool is_jpg) bool rename_cover(const string& path, bool is_jpg)
{ {
string filename; string filename;
@ -751,7 +804,12 @@ bool full_donwload(const string& path, const string& release_prefix,
return false; return false;
} }
// TODO: Make 750x750 Cover JPEG Image // Make 750x750 Cover JPEG Image
ok = resize_cover(release_dir, cover_is_jpg);
if (!ok)
{
return false;
}
// Rename Cover Image (Proper Extension) // Rename Cover Image (Proper Extension)
ok = rename_cover(release_dir, cover_is_jpg); ok = rename_cover(release_dir, cover_is_jpg);