Skip to content

Commit

Permalink
Added batch resize
Browse files Browse the repository at this point in the history
  • Loading branch information
trikko committed May 12, 2024
1 parent f8beceb commit 0a85022
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 0 deletions.
155 changes: 155 additions & 0 deletions res/layout.glade
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,26 @@
</child>
</object>
</child>
<child>
<object class="GtkMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Tools</property>
<child type="submenu">
<object class="GtkMenu">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkMenuItem" id="mnuResize">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Batch resize...</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem">
<property name="visible">True</property>
Expand Down Expand Up @@ -695,6 +715,141 @@
</object>
</child>
</object>
<object class="GtkWindow" id="wndResize">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Batch resize</property>
<property name="resizable">False</property>
<property name="modal">True</property>
<property name="window-position">center-on-parent</property>
<property name="default-width">400</property>
<property name="destroy-with-parent">True</property>
<property name="type-hint">dialog</property>
<property name="transient-for">mainWindow</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Folder with images to resize</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFileChooserButton" id="fileImagesDir">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="action">select-folder</property>
<property name="title" translatable="yes">Select a directory with images to resize</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-top">10</property>
<property name="label" translatable="yes">Max image dimension</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="maxImageDimension">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-top">10</property>
<property name="max-length">4</property>
<property name="text" translatable="yes">1024</property>
<property name="input-purpose">digits</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="pbResize">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-top">20</property>
<property name="margin-bottom">20</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="spacing">20</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkButton" id="btnResize">
<property name="label" translatable="yes">Resize</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnResizeCancel">
<property name="label" translatable="yes">Cancel</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">5</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkDialog" id="wndAbout">
<property name="width-request">320</property>
<property name="height-request">240</property>
Expand Down
114 changes: 114 additions & 0 deletions source/gui.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import gdk.Cursor, gdk.Event, gdk.Keysyms, gdk.Pixbuf;
import gdk.Cairo : setSourcePixbuf;

import cairo.Context;
import gdk.Atom;

struct GUI
{
static:

__gshared bool cancelResize = true;
TreeStore store;

string[] labels;
Expand Down Expand Up @@ -516,6 +518,9 @@ struct GUI
actionSearchLabel("");
wndLabels.showAll();
break;
case GdkKeysyms.GDK_w, GdkKeysyms.GDK_W:
if (isCtrlPressed) Picture.writeAnnotations();
break;

case GdkKeysyms.GDK_z, GdkKeysyms.GDK_Z:
if (isCtrlPressed) actionUndo();
Expand Down Expand Up @@ -1176,6 +1181,114 @@ struct GUI
mainWindow.addOnDelete( (Event e, Widget w){ Main.quit(); return true; } );
mainWindow.showAll();

wndResize.setIcon(logo);
wndResize.addOnDelete( (Event e, Widget w){ wndResize.hide(); return true; } );
btnResizeCancel.addOnButtonPress( (Event e, Widget w){ btnResize.setSensitive = true; cancelResize = true; wndResize.hide(); return true; } );

btnResize.addOnButtonPress( (Event e, Widget w){

import core.thread;
auto folder = (fileImagesDir.getFile)? fileImagesDir.getFile.getPath : "";
int size = 0;

try {size = std.conv.to!int(maxImageDimension.getText);} catch(Exception e) { size = 0; }

if (folder.empty || size == 0)
{
import gtk.MessageDialog;
auto dialog = new MessageDialog(wndResize, DialogFlags.MODAL, MessageType.WARNING, ButtonsType.CLOSE, "Please select a folder and a size");
dialog.setModal(true);
dialog.run();
dialog.destroy();
return true;
}
else
{
btnResize.setSensitive = false;
cancelResize = false;
new Thread({
import glib.Idle;

extern(C) int resize (void* data)
{
double fraction = *(cast(double*)data);

if (fraction < 0) wndResize.hide();
else pbResize.setFraction(fraction);
return 0;
}

auto newFolder = buildPath(folder, "..", folder.baseName ~ "-" ~ std.conv.to!string(size) ~ "px-" ~ randomUUID.toString);
auto entries = dirEntries(folder, "*.{jpg,jpeg,png}", SpanMode.shallow).array;

double rp = 0;

if (entries.length > 0)
{

try {mkdirRecurse(newFolder);} catch(Exception e) { }

foreach(idx, f; entries)
{
if (cancelResize)
{
warning("Resize cancelled");
break;
}

Pixbuf p = new Pixbuf(f.name);

// Letterbox scaling into size*size square
auto ratio = cast(float)p.getWidth / p.getHeight;
auto newWidth = size;
auto newHeight = size;

if (ratio > 1) newHeight = cast(int)(size / ratio);
else newWidth = cast(int)(size * ratio);

auto newPixbuf = p.scaleSimple(newWidth, newHeight, InterpType.BILINEAR);

try {
string ext;
string[] keys;
string[] values;

if (f.name.extension == ".png")
{
ext = "png";
keys = ["compression"];
values = ["9"];
}
else
{
ext = "jpeg";
keys = ["quality"];
values = ["100"];
}

newPixbuf.savev(std.conv.to!string(buildPath(newFolder, f.name.baseName).asNormalizedPath), ext, keys, values);
}
catch(Exception e ) { warning("Can't save ", f.name, " to ", newFolder, ". Exception: ", e.msg); }

destroy(newPixbuf);
destroy(p);

rp = 1.0*idx/entries.length;

Idle.add(&resize, &rp);
}

rp = -1;
Idle.add(&resize, &rp);
Thread.sleep(1000.msecs);
}
}).start();
}

return true;
} );


wndAbout.setIcon(logo);
wndAbout.addOnDelete( (Event e, Widget w){ wndAbout.hide(); return true; } );
imgLogo.setFromPixbuf(logo);
Expand Down Expand Up @@ -1267,6 +1380,7 @@ struct GUI
mainWindow.addOnKeyRelease(toDelegate(&onKeyRelease)); // Key release
mainWindow.addOnKeyPress(toDelegate(&onKeyPress)); // Key press

mnuResize.addOnButtonPress((Event e, Widget w){ btnResize.setSensitive = true; wndResize.showAll(); return true; }); // Resize images
mnuOpen.addOnButtonPress((Event e, Widget w){ actionOpenDir(); return true; }); // Open a directory
mnuReload.addOnButtonPress((Event e, Widget w){ reloadDirectory(); return true; }); // Reload the current directory

Expand Down
10 changes: 10 additions & 0 deletions source/widgets.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ import gtk.Button;
import gtk.Main, gtk.Builder, gtk.Window, gtk.DrawingArea, gtk.EventBox, gtk.Button, gtk.CheckButton, gtk.Adjustment,
gtk.CheckMenuItem, gtk.MenuItem, gtk.Entry, gtk.TreeView, gtk.Dialog, gtk.Image, gtk.FileChooserButton;
import gtk.Adjustment;
import gtk.ProgressBar;

mixin GtkAttributes;

@ui Window wndLabels;
@ui Dialog wndAbout;
@ui Window mainWindow;
@ui Dialog wndAI;
@ui Window wndResize;

@ui ProgressBar pbResize;
@ui Button btnResizeCancel;
@ui Button btnResize;
@ui FileChooserButton fileImagesDir;
@ui Entry maxImageDimension;

@ui DrawingArea canvas;

Expand Down Expand Up @@ -72,6 +80,8 @@ mixin GtkAttributes;
@ui MenuItem mnuAuto;
@ui MenuItem mnuAI;

@ui MenuItem mnuResize;

@ui Button btnAICancel;
@ui Button btnAIOk;
@ui Button btnWebsite;
Expand Down

0 comments on commit 0a85022

Please sign in to comment.