Skip to content

Commit 07d5a31

Browse files
committed
Merge branch 'webp'
2 parents 935cb65 + 1c10dd7 commit 07d5a31

File tree

7 files changed

+113
-10
lines changed

7 files changed

+113
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Annotate your images and let a picture say 1000 words.
2222
- Color picker support within a loaded image.
2323
- Unlimited undo/redo of any change.
2424
- Drag-and-drop PNG copies of the annotated image.
25-
- Export to JPEG, PNG, TIFF, BMP, PDF and SVG image formats.
25+
- Export to JPEG, PNG, TIFF, BMP, PDF, SVG and WebP image formats.
2626
- Support for copying annotated image to clipboard.
2727
- Printer support.
2828

@@ -42,6 +42,7 @@ You will need the following dependencies to build Annotator:
4242
- libxml2-dev
4343
- libgtk-3-dev
4444
- libhandy-1-dev
45+
- libwebp-dev
4546

4647
To install Annotator from source, run `./app install`.
4748

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cc = meson.get_compiler('c')
4242
math_dep = cc.find_library('m', required: false)
4343

4444
add_project_arguments([
45-
# '--vapidir', join_paths(meson.current_source_dir(), 'vapi'),
45+
'--vapidir', join_paths(meson.current_source_dir(), 'vapi'),
4646
'--disable-warnings'
4747
],
4848
language: 'vala',
@@ -57,6 +57,7 @@ dependencies = [
5757
dependency('libxml-2.0'),
5858
dependency('libportal-gtk4'),
5959
dependency('pangocairo'),
60+
dependency('libwebp'),
6061
math_dep
6162
]
6263

src/Exports.vala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Exports {
3838
add( new ExportPDF( canvas ) );
3939
add( new ExportPNG( canvas ) );
4040
add( new ExportSVG( canvas ) );
41+
add( new ExportWebP( canvas ) );
4142

4243
clipboard = new ExportClipboard( canvas );
4344
printer = new ExportPrint( canvas );

src/exports/ExportWebP.vala

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

src/exports/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ sources += files(
44
'ExportPDF.vala',
55
'ExportPNG.vala',
66
'ExportPrint.vala',
7-
'ExportSVG.vala'
7+
'ExportSVG.vala',
8+
'ExportWebP.vala',
89
)

vapi/libportal-gtk4.vapi

Lines changed: 0 additions & 7 deletions
This file was deleted.

vapi/libwebp.vapi

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[CCode (cheader_filename = "webp/encode.h")]
2+
namespace WebP {
3+
4+
[CCode (cname = "WebPEncodeLosslessRGB")]
5+
public extern size_t encode_lossless_rgb(
6+
uint8* rgb,
7+
int width,
8+
int height,
9+
int stride,
10+
out uint8* output_buffer
11+
);
12+
13+
[CCode (cname = "WebPEncodeLosslessRGBA")]
14+
public extern size_t encode_lossless_rgba(
15+
uint8* rgb,
16+
int width,
17+
int height,
18+
int stride,
19+
out uint8* output_buffer
20+
);
21+
22+
[CCode (cname = "WebPFree")]
23+
public extern void free(void* ptr);
24+
25+
}

0 commit comments

Comments
 (0)