Skip to content

Commit

Permalink
load image
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Dec 29, 2023
1 parent fb0cc14 commit 02be36d
Showing 1 changed file with 70 additions and 46 deletions.
116 changes: 70 additions & 46 deletions plugins/notify/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,45 @@ _buffer_from_file (const char *fname, long *psize) {
}

static GdkPixbuf *
_load_image_from_cover (const char *image_filename) {
_create_scaled_image (GdkPixbuf *image, int width, int height) {
int originalWidth = gdk_pixbuf_get_width (image);
int originalHeight = gdk_pixbuf_get_height (image);

if (originalWidth <= width && originalHeight <= height) {
gobj_ref (image);
return image;
}

gboolean has_alpha = gdk_pixbuf_get_has_alpha (image);
int bits_per_sample = gdk_pixbuf_get_bits_per_sample (image);

GdkPixbuf *scaled_image = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, bits_per_sample, width, height);

double scale_x = (double)width / (double)originalWidth;
double scale_y = (double)height / (double)originalHeight;

gdk_pixbuf_scale (image, scaled_image, 0, 0, width, height, 0, 0, scale_x, scale_y, GDK_INTERP_BILINEAR);

return scaled_image;
}

static void
_desired_size_for_image_size (
covermanager_t *manager,
int image_width,
int image_height,
int avail_width,
int avail_height,
int *result_width,
int *result_height) {
double scale = min ((double)avail_width / (double)image_width, (double)avail_height / (double)image_height);

*result_width = image_width * scale;
*result_height = image_height * scale;
}

static GdkPixbuf *
_load_image (const char *image_filename) {
GdkPixbuf *img = NULL;

long size = 0;
Expand All @@ -217,34 +255,29 @@ _load_image_from_cover (const char *image_filename) {
free (buf);
}

// if (img) {
// const int max_image_size = impl->image_size;
//
// // downscale
// GtkAllocation size = {
// .width = gdk_pixbuf_get_width (img),
// .height = gdk_pixbuf_get_height (img),
// };
//
// if (size.width > max_image_size || size.height > max_image_size) {
// GtkAllocation new_size = {
// .width = max_image_size,
// .height = max_image_size,
// };
// new_size = covermanager_desired_size_for_image_size (impl, size, new_size);
//
// GdkPixbuf *scaled_img = covermanager_create_scaled_image (impl, img, new_size);
// gobj_unref (img);
// img = scaled_img;
// }
// }
//
// if (!img && want_default) {
// img = impl->default_cover;
// if (img != NULL) {
// gobj_ref (img);
// }
// }
if (img == NULL) {
return NULL;
}

const int max_image_size = 64; // FIXME: config

// downscale
int orig_width = gdk_pixbuf_get_width (img);
int orig_height = gdk_pixbuf_get_height (img),

if (orig_width > max_image_size || orig_height > max_image_size) {
int new_width = max_image_size;
int new_height = max_image_size;

int result_width;
int result_height;

_desired_size_for_image_size (orig_width, orig_height, new_width, new_height, &result_width, &result_height);

GdkPixbuf *scaled_img = _create_scaled_image (img, result_width, result_height);
gobj_unref (img);
img = scaled_img;
}

return img;
}
Expand Down Expand Up @@ -299,23 +332,14 @@ show_notification (DB_playItem_t *track, char *image_filename, dbus_uint32_t rep
const char *v_body = esc_content;
dbus_int32_t v_timeout = -1;

// GdkPixbuf *img = _load_image_from_cover(image_filename);

struct {
dbus_int32_t width;
dbus_int32_t height;
dbus_int32_t stride;
dbus_bool_t has_alpha;
dbus_int32_t bits_per_sample;
dbus_int32_t channels;
} image_data;

image_data.width = 64;
image_data.height = 64;
image_data.stride = image_data.width * 4;
image_data.has_alpha = 1; // has to be 1, otherwise channels=4 is ignored and interpreted as 3.
image_data.bits_per_sample = 8;
image_data.channels = 4;
GdkPixbuf *img = _load_image (image_filename);

dbus_int32_t width = 64;
dbus_int32_t height = 64;
dbus_int32_t stride = image_data.width * 4;
dbus_bool_t has_alpha = 1; // has to be 1, otherwise channels=4 is ignored and interpreted as 3.
dbus_int32_t bits_per_sample = 8;
dbus_int32_t channels = 4;

uint32_t *image_bytes = malloc (image_data.width * image_data.width * sizeof (uint32_t));
for (int x = 0; x < image_data.width; x++) {
Expand Down

0 comments on commit 02be36d

Please sign in to comment.