Skip to content

Commit 26d669f

Browse files
committed
Check for maximum texture and downscale image if bigger
This is temporary fix to prevent app entirely crashing or rendering nothing. This has to be fixed eventually and work around it.
1 parent 4d4e628 commit 26d669f

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/rendering/image-processing.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ gpointer process_image(gpointer data) {
4343
self->con.image_data = calloc(image->width * image->height * 3, sizeof(uint16_t));
4444
memcpy(self->con.image_data, image->data, image->width * image->height * 3 * sizeof(uint16_t));
4545

46-
self->con.width = image->width;
47-
self->con.height = image->height;
46+
self->con.original_width = image->width;
47+
self->con.original_height = image->height;
4848
self->con.bit_depth = image->bits;
4949
} else {
5050
fprintf(stderr, "Couldn't open file using libraw (errno: %d), trying gdk fallback\n",
@@ -63,18 +63,29 @@ gpointer process_image(gpointer data) {
6363
free(self->con.image_data);
6464
self->con.image_data = calloc(len, sizeof(guchar));
6565
memcpy(self->con.image_data, pix, len * sizeof(guchar));
66-
self->con.width = gdk_pixbuf_get_width(pixbuf);
67-
self->con.height = gdk_pixbuf_get_height(pixbuf);
66+
self->con.original_width = gdk_pixbuf_get_width(pixbuf);
67+
self->con.original_height = gdk_pixbuf_get_height(pixbuf);
6868
self->con.bit_depth = 8;
6969
}
7070
libraw_close(libraw_handle);
7171

72-
if (self->con.height > 1080) {
73-
self->con.preview_height = self->con.height / 4;
74-
self->con.preview_width = self->con.width / 4;
72+
73+
if (self->con.original_height > self->con.max_tex_size || self->con.original_width > self->con.max_tex_size) {
74+
self->con.width = self->con.max_tex_size;
75+
self->con.height = ((gdouble)self->con.original_height/self->con.original_width)*self->con.max_tex_size;
76+
if (self->con.height > self->con.max_tex_size) {
77+
self->con.width = ((gdouble)self->con.original_width/self->con.original_height)*self->con.max_tex_size;
78+
self->con.height = self->con.max_tex_size;
79+
}
80+
printf("Image is bigger than what OGL can render new dimensions: %dx%d\n", self->con.width, self->con.height);
81+
}
82+
83+
if (self->con.original_height > 1080) {
84+
self->con.preview_height = self->con.original_height / 4;
85+
self->con.preview_width = self->con.original_width / 4;
7586
} else {
76-
self->con.preview_height = self->con.height;
77-
self->con.preview_width = self->con.width;
87+
self->con.preview_height = self->con.original_height;
88+
self->con.preview_width = self->con.original_width;
7889
}
7990

8091
self->con.texture_refresh_pending = true;

src/rendering/image-renderer.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void refresh_textures(RendererControl* con) {
148148
// won't render correctly
149149
glBindTexture(GL_TEXTURE_2D, con->tex_base);
150150
glTexImage2D(GL_TEXTURE_2D, 0, con->bit_depth == 16 ? GL_RGB16 : GL_RGB8,
151-
con->width, con->height, 0, GL_RGB,
151+
con->original_width, con->original_height, 0, GL_RGB,
152152
con->bit_depth == 16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE, con->image_data);
153153
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
154154
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -251,6 +251,9 @@ void realize(GtkWidget *widget, RendererControl* con) {
251251
prepare_textures(con);
252252

253253
printf("Initialized OpenGL\n");
254+
con->max_tex_size = 0;
255+
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &con->max_tex_size);
256+
printf("Max texture size: %d\n", con->max_tex_size);
254257
}
255258

256259
void unrealize(GtkWidget *widget) {
@@ -431,6 +434,7 @@ gboolean render(GtkGLArea* area, GdkGLContext* context, RendererControl* con) {
431434
gboolean export(RendererControl* con, char* path) {
432435
int width = con->width;
433436
int height = con->height;
437+
434438

435439
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
436440
glViewport(0, 0, width, height);
@@ -475,7 +479,7 @@ gboolean export(RendererControl* con, char* path) {
475479

476480
uint8_t* fb_data = calloc(width*height, sizeof(uint8_t)*3);
477481

478-
glReadnPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, sizeof(uint8_t)*3*width*height, fb_data);
482+
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, fb_data);
479483

480484
// Flush the contents of the pipeline
481485
glFlush();

src/rendering/image-renderer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef struct RendererControl_ {
5050
GtkWidget* ogl_frame;
5151
GdkPixbuf* pxb_original;
5252
void* image_data;
53+
int original_height;
54+
int original_width;
5355
int preview_height;
5456
int preview_width;
5557
int height;
@@ -73,6 +75,7 @@ typedef struct RendererControl_ {
7375
GLuint VAO;
7476
GLuint VBO;
7577
GLuint EBO;
78+
GLint max_tex_size;
7679

7780
int processed_fbs_count;
7881

0 commit comments

Comments
 (0)