@@ -43,32 +43,53 @@ static void page_view_resized_cb (GtkWidget *, GtkAllocation *, gpointer);
4343static gboolean page_view_scrolled_cb (GtkEventControllerScroll *, gdouble, gdouble, gpointer);
4444static gboolean page_view_keypress_cb (GtkEventControllerKey *, guint, guint, GdkModifierType, gpointer);
4545
46- static void gdkpixbuf_invert (GdkPixbuf *pb) {// krogan edit
47- int width, height, rowlength, n_channels;
48- guchar *pixels, *p;
49-
50- n_channels = gdk_pixbuf_get_n_channels (pb);
51-
52- g_assert (gdk_pixbuf_get_colorspace (pb) == GDK_COLORSPACE_RGB);
53- g_assert (gdk_pixbuf_get_bits_per_sample (pb) == 8 );
54- g_assert (gdk_pixbuf_get_has_alpha (pb));
55- g_assert (n_channels == 4 );
46+ // /
47+ // / @brief Inverts the colors of a GdkPixbuf
48+ // /
49+ // / This function inverts the RGB values of a GdkPixbuf while preserving the alpha channel.
50+ // / @param pb The GdkPixbuf to invert. If NULL or invalid, the function does nothing.
51+ // /
52+ static void gdkpixbuf_invert (GdkPixbuf *pb) {
53+ // Check if the pixbuf is valid
54+ if (pb == NULL ) {
55+ g_warning (" gdkpixbuf_invert: NULL pixbuf provided" );
56+ return ;
57+ }
5658
57- width = gdk_pixbuf_get_width (pb);
58- height = gdk_pixbuf_get_height (pb);
59+ // Get pixbuf properties
60+ int n_channels = gdk_pixbuf_get_n_channels (pb);
61+
62+ // Validate pixbuf format
63+ if (gdk_pixbuf_get_colorspace (pb) != GDK_COLORSPACE_RGB ||
64+ gdk_pixbuf_get_bits_per_sample (pb) != 8 ||
65+ !gdk_pixbuf_get_has_alpha (pb) ||
66+ n_channels != 4 ) {
67+ g_warning (" gdkpixbuf_invert: Unsupported pixbuf format" );
68+ return ;
69+ }
5970
60- rowlength = width*n_channels;
61-
62- pixels = gdk_pixbuf_get_pixels (pb);
63-
64- int i;
65- int max = rowlength*height;
66- for (i=0 ;i<max;i+=n_channels) {
67- p = pixels + i;
68- p[0 ] = 255 - p[0 ];
69- p[1 ] = 255 - p[1 ];
70- p[2 ] = 255 - p[2 ];
71- }
71+ int width = gdk_pixbuf_get_width (pb);
72+ int height = gdk_pixbuf_get_height (pb);
73+ int rowstride = gdk_pixbuf_get_rowstride (pb);
74+ guchar *pixels = gdk_pixbuf_get_pixels (pb);
75+
76+ // Check if we have valid dimensions and pixel data
77+ if (width <= 0 || height <= 0 || pixels == NULL ) {
78+ g_warning (" gdkpixbuf_invert: Invalid pixbuf dimensions or pixel data" );
79+ return ;
80+ }
81+
82+ // Invert each pixel
83+ for (int y = 0 ; y < height; y++) {
84+ guchar *row = pixels + y * rowstride;
85+ for (int x = 0 ; x < width; x++) {
86+ guchar *p = row + x * n_channels;
87+ p[0 ] = 255 - p[0 ]; // Red
88+ p[1 ] = 255 - p[1 ]; // Green
89+ p[2 ] = 255 - p[2 ]; // Blue
90+ // p[3] is alpha, leave it unchanged
91+ }
92+ }
7293}
7394
7495PageView::PageView ():
@@ -519,7 +540,10 @@ PageView::showPage (DocumentPage *page, PageScroll scroll)
519540 gdk_pixbuf_get_width (m_CurrentPixbuf),
520541 gdk_pixbuf_get_height (m_CurrentPixbuf));
521542
522- if (invertColorToggle) { gdkpixbuf_invert (m_CurrentPixbuf); }
543+ // Invert colors if needed
544+ if (invertColorToggle && m_CurrentPixbuf != NULL ) {
545+ gdkpixbuf_invert (m_CurrentPixbuf);
546+ }
523547
524548 // GTK4: Convert pixbuf to texture for display
525549 GdkTexture *texture = gdk_texture_new_for_pixbuf (m_CurrentPixbuf);
0 commit comments