Introduce img_preview_buffer
[colorize.git] / gui / image.c
index 49f7ec8..afd572e 100644 (file)
@@ -18,7 +18,7 @@ GdkPixbuf *img_pixbuf = NULL;
 int img_width, img_height;
 int copy_width, copy_height;
 char img_name[256];
-unsigned short *img_grey_buffer = NULL;
+unsigned short *img_grey_buffer = NULL, *img_preview_buffer = NULL;
 unsigned char *img_mark_buffer = NULL, *img_mark_buffer_undo[UNDO_MAX], *img_mark_buffer_copy;
 int button_down_x_undo[UNDO_MAX], button_down_y_undo[UNDO_MAX];
 int undo_current, undo_num;
@@ -29,40 +29,25 @@ extern int button_down_x, button_down_y;
 /* load image and create pixbuf */
 void create_image(const char *filename, int resize)
 {
-       static char imgfile[256];
-
        destroy_image();
 
        anything_modified = 0;
 
        strcpy(img_name, filename);
-       if (rendered) {
-               const char *p, *q;
-               p = filename;
-               while((q = strchr(p, DIR_SEPERATOR)))
-               p = q + 1;
-               strcpy(imgfile, filename);
-               imgfile[p - filename] = '\0';
-               strcat(imgfile, "colorized_");
-               strcat(imgfile, p);
-       } else
-               strcpy(imgfile, filename);
-       img_grey_buffer = load_img(&img_width, &img_height, imgfile, 0);
+       img_grey_buffer = load_img(&img_width, &img_height, filename, 0);
        if (!img_grey_buffer) {
-               img_grey_buffer = NULL;
-               if (!rendered)
-                       printerror("Failed to load grey image '%s'", imgfile);
-               else {
-                       img_width = 16;
-                       img_height = 16;
-                       goto no_mark;
-               }
+               printerror("Failed to load grey image '%s'", filename);
                return;
        }
+       if (preview)
+               create_preview();
+       else if (rendered)
+               create_rendered(filename);
        img_mark_buffer = malloc(img_width*img_height);
        if (!img_mark_buffer) {
                free(img_grey_buffer);
                img_grey_buffer = NULL;
+               destroy_preview();
                return;
        }
        memset(img_mark_buffer, 0, img_width*img_height);
@@ -75,11 +60,62 @@ void create_image(const char *filename, int resize)
                        frame_list[timeline_selected].marked = 0;
        }
 
-no_mark:
        /* create pixbuf */
        create_or_reset_pixbuf(resize);
 }
 
+void create_preview()
+{
+       size_t size = img_width*img_height*3*sizeof(*img_preview_buffer);
+
+       destroy_preview();
+
+       /* no grey image, so no preview */
+       if (!img_grey_buffer) {
+               return;
+       }
+
+       img_preview_buffer = malloc(size);
+       if (!img_preview_buffer) {
+               return;
+       }
+
+       memcpy(img_preview_buffer, img_grey_buffer, size);
+}
+
+void create_rendered(const char *filename)
+{
+       static char imgfile[256];
+       const char *p, *q;
+       int width, height;
+
+       destroy_preview();
+
+       /* no grey image, so no preview */
+       if (!img_grey_buffer) {
+               return;
+       }
+
+       p = filename;
+       while((q = strchr(p, DIR_SEPERATOR)))
+       p = q + 1;
+       strcpy(imgfile, filename);
+       imgfile[p - filename] = '\0';
+       strcat(imgfile, "colorized_");
+       strcat(imgfile, p);
+
+       img_preview_buffer = load_img(&width, &height, imgfile, 0);
+       if (!img_preview_buffer) {
+               printerror("Failed to load rendered image '%s'", imgfile);
+               return;
+       }
+       if (width != img_width || height != img_height) {
+               printerror("Failed to load: Rendered image '%s' has different size", imgfile);
+               destroy_preview();
+               return;
+       }
+}
+
 void create_or_reset_pixbuf(int resize)
 {
        int w, h, dw = 0, dh = 0;
@@ -161,6 +197,7 @@ void destroy_image(void)
 
        free(img_grey_buffer);
        img_grey_buffer = NULL;
+       destroy_preview();
        free(img_mark_buffer);
        img_mark_buffer = NULL;
        for (i = 0; i < UNDO_MAX; i++) {
@@ -171,15 +208,20 @@ void destroy_image(void)
        }
 }
 
+void destroy_preview(void)
+{
+       free(img_preview_buffer);
+       img_preview_buffer = NULL;
+}
+
 /* draw (area) of pixbuf */
 void draw_image(int x, int y, int w, int h)
 {
        GdkDrawable *draw = gtk_widget_get_window(img_drawing_area);
        int window_width, window_height, x_offset, y_offset;
        gdk_drawable_get_size (draw, &window_width, &window_height);
-       double _r, _g, _b, _y, _u, _v, u_palette, v_palette, pat, alpha;
+       double _r, _g, _b, pat, alpha;
        int cr, cg, cb;
-       int _c, preview_asis = 0;
 
        if (w < 0)
                w = window_width;
@@ -266,51 +308,16 @@ void draw_image(int x, int y, int w, int h)
        y -= y_offset;
 
        if (img_grey_buffer) {
-               if (preview) {
-                       rgb2yuv_pixle(mark_palette[mark_selected].r / 255.0F, mark_palette[mark_selected].g / 255.0F, mark_palette[mark_selected].b / 255.0F, &_y, &u_palette, &v_palette);
-                       /* check for white color (no change) */
-                       if (mark_palette[mark_selected].r == 255 && mark_palette[mark_selected].g == 255 && mark_palette[mark_selected].r == 255)
-                               preview_asis = 1;
-               }
                /* compose image (segment) line by line */
                for (i = 0; i < h; i++) {
                        for (j = 0; j < w; j++) {
-                               if (preview && !rendered && !flowview) {
-                                       /* apply selected color from palette to all pixles */
-                                       _r = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3] / 65535.0F;
-                                       _g = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1] / 65535.0F;
-                                       _b = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2] / 65535.0F;
-                                       rgb2yuv_pixle(_r, _g, _b, &_y, &_u, &_v);
-                                       _y = (_y - 0.5) * mark_palette[mark_selected].contrast + 0.5;
-                                       _y += mark_palette[mark_selected].bright;
-                                       if (_y < 0)
-                                               _y = 0;
-                                       if (_y > 1)
-                                               _y = 1;
-                                       if (preview_asis)
-                                               yuv2rgb_pixle(_y, _u, _v, &_r, &_g, &_b);
-                                       else
-                                               yuv2rgb_pixle(_y, u_palette, v_palette, &_r, &_g, &_b);
-                                       _c = _r * 255.0F;
-                                       if (_c < 0)
-                                               _c = 0;
-                                       else if (_c > 255)
-                                               _c = 255;
-                                       compose[j*3] = _c;
-                                               _c = _g * 255.0F;
-                                       if (_c < 0)
-                                               _c = 0;
-                                       else if (_c > 255)
-                                               _c = 255;
-                                       compose[j*3+1] = _c;
-                                               _c = _b * 255.0F;
-                                       if (_c < 0)
-                                               _c = 0;
-                                       else if (_c > 255)
-                                               _c = 255;
-                                       compose[j*3+2] = _c;
-
+                               if (img_preview_buffer) {
+                                       /* show preview buffer */
+                                       compose[j*3] = img_preview_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3] >> 8;
+                                       compose[j*3+1] = img_preview_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1] >> 8;
+                                       compose[j*3+2] = img_preview_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2] >> 8;
                                } else {
+                                       /* show grey buffer */
                                        compose[j*3] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3] >> 8;
                                        compose[j*3+1] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1] >> 8;
                                        compose[j*3+2] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2] >> 8;