hilight color tweak, listbox drag fixes, add 10bit to spec
[goodguy/history.git] / cinelerra-5.1 / guicast / vframe.C
index 0ae65a66a62eaf06b97a009792a9e8b758d69b73..fdd24ce02a35f85e8a6b46552961c9272a203057 100644 (file)
@@ -127,15 +127,13 @@ VFrame *VFramePng::vframe_png(const char *png_path, double xs, double ys)
        return vframe;
 }
 
-
 VFrame::VFrame(VFrame &frame)
 {
        reset_parameters(1);
        params = new BC_Hash;
        allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
                frame.color_model, frame.bytes_per_line);
-       memcpy(data, frame.data, bytes_per_line * h);
-       copy_stacks(&frame);
+       copy_from(&frame);
 }
 
 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
@@ -306,6 +304,11 @@ int VFrame::clear_objects(int do_opengl)
                pbuffer = 0;
        }
 
+#ifdef LEAKER
+if( memory_type != VFrame::SHARED )
+  printf("==del %p from %p\n", data, __builtin_return_address(0));
+#endif
+
 // Delete data
        switch(memory_type)
        {
@@ -524,6 +527,9 @@ int VFrame::allocate_data(unsigned char *data, int shmid,
 
                if(!this->data)
                        printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
+#ifdef LEAKER
+printf("==new %p from %p sz %d\n", this->data, __builtin_return_address(0), size);
+#endif
 
 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
@@ -807,49 +813,41 @@ int VFramePng::read_png(const unsigned char *data, long sz, double xscale, doubl
 
 int VFrame::write_png(const char *path)
 {
+       VFrame *vframe = this;
        png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
        png_infop info_ptr = png_create_info_struct(png_ptr);
        FILE *out_fd = fopen(path, "w");
-       if(!out_fd)
-       {
+       if(!out_fd) {
                printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
                return 1;
        }
 
        int png_cmodel = PNG_COLOR_TYPE_RGB;
-       switch(get_color_model())
-       {
-               case BC_RGB888:
-               case BC_YUV888:
-                       png_cmodel = PNG_COLOR_TYPE_RGB;
-                       break;
-
-               case BC_RGBA8888:
-               case BC_YUVA8888:
+       int bc_cmodel = get_color_model();
+       switch( bc_cmodel ) {
+       case BC_RGB888:                                          break;
+       case BC_RGBA8888: png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA; break;
+       case BC_A8:       png_cmodel = PNG_COLOR_TYPE_GRAY;      break;
+       default:
+               bc_cmodel = BC_RGB888;
+               if( BC_CModels::has_alpha(bc_cmodel) ) {
+                       bc_cmodel = BC_RGBA8888;
                        png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA;
-                       break;
-
-               case BC_A8:
-                       png_cmodel = PNG_COLOR_TYPE_GRAY;
-                       break;
+               }
+               vframe = new VFrame(get_w(), get_h(), bc_cmodel, -1);
+               vframe->transfer_from(this);
+               break;
        }
-
        png_init_io(png_ptr, out_fd);
        png_set_compression_level(png_ptr, 9);
-       png_set_IHDR(png_ptr,
-               info_ptr,
-               get_w(),
-               get_h(),
-       8,
-               png_cmodel,
-               PNG_INTERLACE_NONE,
-               PNG_COMPRESSION_TYPE_DEFAULT,
-               PNG_FILTER_TYPE_DEFAULT);
+       png_set_IHDR(png_ptr, info_ptr, get_w(), get_h(), 8, png_cmodel,
+               PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
        png_write_info(png_ptr, info_ptr);
-       png_write_image(png_ptr, get_rows());
+       png_write_image(png_ptr, vframe->get_rows());
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(out_fd);
+       if( vframe != this ) delete vframe;
        return 0;
 }
 
@@ -940,12 +938,9 @@ int VFrame::clear_frame()
 void VFrame::rotate90()
 {
 // Allocate new frame
-       int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
-       unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
-       unsigned char **new_rows = new unsigned char*[new_h];
-       for(int i = 0; i < new_h; i++)
-               new_rows[i] = &new_data[new_bytes_per_line * i];
-
+       int new_w = h, new_h = w;
+       VFrame new_frame(new_w, new_h, color_model);
+       unsigned char **new_rows = new_frame.get_rows();
 // Copy data
        for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
        {
@@ -960,23 +955,34 @@ void VFrame::rotate90()
        }
 
 // Swap frames
-       clear_objects(0);
+// swap memory
+       unsigned char *new_data = new_frame.data;
+       new_frame.data = data;
        data = new_data;
+// swap rows
+       new_rows = new_frame.rows;
+       new_frame.rows = rows;
        rows = new_rows;
-       bytes_per_line = new_bytes_per_line;
-       w = new_w;
-       h = new_h;
+// swap shmid
+       int new_shmid = new_frame.shmid;
+       new_frame.shmid = shmid;
+       shmid = new_shmid;
+// swap bytes_per_line
+       int new_bpl = new_frame.bytes_per_line;
+       new_frame.bytes_per_line = bytes_per_line;
+       bytes_per_line = new_bpl;
+       new_frame.clear_objects(0);
+
+       w = new_frame.w;
+       h = new_frame.h;
 }
 
 void VFrame::rotate270()
 {
 // Allocate new frame
-       int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
-       unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
-       unsigned char **new_rows = new unsigned char*[new_h];
-       for(int i = 0; i < new_h; i++)
-               new_rows[i] = &new_data[new_bytes_per_line * i];
-
+       int new_w = h, new_h = w;
+       VFrame new_frame(new_w, new_h, color_model);
+       unsigned char **new_rows = new_frame.get_rows();
 // Copy data
        for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
        {
@@ -991,12 +997,26 @@ void VFrame::rotate270()
        }
 
 // Swap frames
-       clear_objects(0);
+// swap memory
+       unsigned char *new_data = new_frame.data;
+       new_frame.data = data;
        data = new_data;
+// swap rows
+       new_rows = new_frame.rows;
+       new_frame.rows = rows;
        rows = new_rows;
-       bytes_per_line = new_bytes_per_line;
-       w = new_w;
-       h = new_h;
+// swap shmid
+       int new_shmid = new_frame.shmid;
+       new_frame.shmid = shmid;
+       shmid = new_shmid;
+// swap bytes_per_line
+       int new_bpl = new_frame.bytes_per_line;
+       new_frame.bytes_per_line = bytes_per_line;
+       bytes_per_line = new_bpl;
+       new_frame.clear_objects(0);
+
+       w = new_frame.w;
+       h = new_frame.h;
 }
 
 void VFrame::flip_vert()
@@ -1080,6 +1100,7 @@ int VFrame::copy_from(VFrame *frame)
                        break;
        }
 
+       params->copy_from(frame->params);
        return 0;
 }
 
@@ -1131,6 +1152,7 @@ int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in
                        that->get_bytes_per_line(),
                bg_color);
 #endif
+       params->copy_from(that->params);
        return 0;
 }
 
@@ -1298,9 +1320,6 @@ void VFrame::set_pixel_color(int rgb)
        int ig = 0xff & (pixel_rgb >> 8);
        int ib = 0xff & (pixel_rgb >> 0);
        bc_rgb2yuv(ir,ig,ib, ir,ig,ib);
-       bclamp(ir,0,255);
-       bclamp(ig,0,255);
-       bclamp(ib,0,255);
        pixel_yuv =  (ir<<16) | (ig<<8) | (ib<<0);
 }