1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim 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 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 Shows how to register custom image loader/saver.
27 Feed it with small image (cca 60x60 pixels) to produce ASCII art version.
38 * Saves 2 bpp grayscale image as ASCII Art
40 static int write_data(const gp_pixmap
*img
, gp_io
*io
,
41 gp_progress_cb
*callback
)
46 if (img
->pixel_type
!= GP_PIXEL_G4
) {
51 /* Create buffered I/O */
52 bio
= gp_io_wbuffer(io
, 0);
58 const char *const table
[] = {" ", ".", ",", "-", "=", "#", "O", "$"};
60 for (j
= 0; j
< img
->h
; j
++) {
61 for (i
= 0; i
< img
->w
; i
++) {
62 gp_pixel p
= gp_getpixel_raw(img
, i
, j
);
63 const char *c
= table
[p
>>1];
65 err
= gp_io_flush(bio
, c
, 1);
70 if (gp_io_flush(bio
, "\n", 1))
73 if (gp_progress_cb_report(callback
, j
, img
->h
, img
->w
)) {
80 gp_progress_cb_done(callback
);
84 static gp_pixel_type save_ptypes
[] = {
89 const gp_loader loader
= {
91 .save_ptypes
= save_ptypes
,
92 .fmt_name
= "ASCII Art",
93 .extensions
= {"txt", NULL
},
96 int main(int argc
, char *argv
[])
98 gp_pixmap
*c
, *gc
, *sc
;
100 gp_loader_register(&loader
);
102 /* List all loaders */
107 fprintf(stderr
, "ERROR: Takes image as an argument\n");
111 /* Now load image and save it using our loader */
112 c
= gp_load_image(argv
[1], NULL
);
114 fprintf(stderr
, "Failed to load image: %s\n", strerror(errno
));
119 * Font letters are not square resize the image so that it's twice it
123 gp_size h
= (w
/2 * c
->h
+ c
->w
/2)/c
->w
;
125 sc
= gp_filter_resize_alloc(c
, w
, h
, GP_INTERP_LINEAR_LF_INT
, NULL
);
126 gc
= gp_filter_floyd_steinberg_alloc(sc
, GP_PIXEL_G4
, NULL
);
129 fprintf(stderr
, "FloydSteinberg: %s\n", strerror(errno
));
133 printf("Saving to test.txt\n");
135 if (gp_save_image(gc
, "test.txt", NULL
)) {
136 fprintf(stderr
, "Failed to save image: %s\n", strerror(errno
));