+
+ trim = false;
+
+ if (getenv("SYNFIG_DISABLE_CROP_IMPORTED_IMAGES"))
+ return;
+
+ switch(color_type)
+ {
+ case PNG_COLOR_TYPE_RGB_ALPHA:
+ case PNG_COLOR_TYPE_GRAY_ALPHA:
+ case PNG_COLOR_TYPE_PALETTE:
+ for(y=0;y<height;y++)
+ {
+ for(x=0;x<width;x++)
+ if (surface_buffer[y][x].get_a()) break;
+ if (x != width) break;
+ }
+
+ if (y != height)
+ {
+#define BORDER 1 // don't chop off all the transparent space - leave a border this many pixels wide for the interpolation
+
+ png_uint_32 min_x, min_y, max_x, max_y;
+ if (y>BORDER) min_y = y-BORDER; else min_y = 0;
+
+ for(y=height-1;y>0;y--)
+ {
+ for(x=0;x<width;x++)
+ if (surface_buffer[y][x].get_a()) break;
+ if (x != width) break;
+ }
+ max_y = std::min(y+BORDER,height-1);
+
+ for(x=0;x<width;x++)
+ {
+ for(y=0;y<height;y++)
+ if (surface_buffer[y][x].get_a()) break;
+ if (y != height) break;
+ }
+ if (x>BORDER) min_x = x-BORDER; else min_x = 0;
+
+ for(x=width-1;x>0;x--)
+ {
+ for(y=0;y<height;y++)
+ if (surface_buffer[y][x].get_a()) break;
+ if (y != height) break;
+ }
+ max_x = std::min(x+BORDER,width-1);
+
+ if (min_x != 0 || max_x != width-1 ||
+ min_y != 0 || max_y != height-1)
+ {
+ trim = true;
+ orig_width = width;
+ orig_height = height;
+ trimmed_x = min_x;
+ trimmed_y = min_y;
+
+ width=max_x-min_x+1;
+ height=max_y-min_y+1;
+ synfig::Surface tmp_buffer;
+ tmp_buffer.set_wh(width,height);
+ for(y=0;y<height;y++)
+ for(x=0;x<width;x++)
+ tmp_buffer[y][x] = surface_buffer[y+min_y][x+min_x];
+ surface_buffer = tmp_buffer;
+ }
+ }
+ }