|
| 1 | +/* |
| 2 | +* Copyright (c) 2018 (https://github.com/phase1geo/Minder) |
| 3 | +* |
| 4 | +* This program is free software; you can redistribute it and/or |
| 5 | +* modify it under the terms of the GNU General Public |
| 6 | +* License as published by the Free Software Foundation; either |
| 7 | +* version 2 of the License, or (at your option) any later version. |
| 8 | +* |
| 9 | +* This program is distributed in the hope that it will be useful, |
| 10 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +* General Public License for more details. |
| 13 | +* |
| 14 | +* You should have received a copy of the GNU General Public |
| 15 | +* License along with this program; if not, write to the |
| 16 | +* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | +* Boston, MA 02110-1301 USA |
| 18 | +* |
| 19 | +* Authored by: Trevor Williams <[email protected]> |
| 20 | +*/ |
| 21 | + |
| 22 | +using WebP; |
| 23 | +using Cairo; |
| 24 | +using Gdk; |
| 25 | +using Gtk; |
| 26 | + |
| 27 | +public class ExportWebP : Export { |
| 28 | + |
| 29 | + public ExportWebP( Canvas canvas ) { |
| 30 | + base( canvas, "webp", _( "WebP" ), { ".webp" } ); |
| 31 | + } |
| 32 | + |
| 33 | + /* Default constructor */ |
| 34 | + public override bool export( string filename, Pixbuf source ) { |
| 35 | + |
| 36 | + var fname = repair_filename( filename ); |
| 37 | + |
| 38 | + /* Create the drawing surface */ |
| 39 | + var surface = new ImageSurface( Format.RGB24, source.width, source.height ); |
| 40 | + var context = new Context( surface ); |
| 41 | + canvas.draw_all( context ); |
| 42 | + |
| 43 | + /* Write the pixbuf to the file */ |
| 44 | + var pixbuf = pixbuf_get_from_surface( surface, 0, 0, surface.get_width(), surface.get_height() ); |
| 45 | + |
| 46 | + uint8* output_buffer = null; |
| 47 | + size_t buffer_size = 0; |
| 48 | + switch( pixbuf.get_n_channels() ) { |
| 49 | + case 3 : |
| 50 | + buffer_size = WebP.encode_lossless_rgb( pixbuf.get_pixels(), pixbuf.get_width(), pixbuf.get_height(), pixbuf.get_rowstride(), out output_buffer ); |
| 51 | + break; |
| 52 | + case 4 : |
| 53 | + buffer_size = WebP.encode_lossless_rgb( pixbuf.get_pixels(), pixbuf.get_width(), pixbuf.get_height(), pixbuf.get_rowstride(), out output_buffer ); |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + if( buffer_size == 0 ) { |
| 58 | + stdout.printf( "Failed to encode WebP image." ); |
| 59 | + return( false ); |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + uint8[] obuf = {}; |
| 64 | + for( int i=0; i<buffer_size; i++ ) { |
| 65 | + obuf += output_buffer[i]; |
| 66 | + } |
| 67 | + WebP.free( output_buffer ); |
| 68 | + var file = File.new_for_path( fname ); |
| 69 | + var output_stream = file.replace( null, false, FileCreateFlags.NONE, null ); |
| 70 | + output_stream.write( obuf ); |
| 71 | + output_stream.close(); |
| 72 | + } catch (IOError e) { |
| 73 | + stdout.printf( "Error saving WebP file: %s", e.message ); |
| 74 | + return( false ); |
| 75 | + } |
| 76 | + |
| 77 | + return( true ); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments