1 /* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
2 
3 /*
4  * XVPICS image saver plugin for GImageView
5  * Copyright (C) 2001 Takuro Ashie
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include <stdio.h>
23 #include <png.h>
24 
25 #include "gimv_image.h"
26 
27 #include "dither.h"
28 #include "gimv_plugin.h"
29 #include "gimv_image_saver.h"
30 
31 static gboolean save_xvpics (GimvImageSaver *saver,
32                              GimvImage *image,
33                              const gchar *filename,
34                              const gchar *format);
35 
36 static GimvImageSaverPlugin plugin_impl[] =
37 {
38    {GIMV_IMAGE_SAVER_IF_VERSION, "xvpic",  save_xvpics, NULL},
39    {GIMV_IMAGE_SAVER_IF_VERSION, "xvpics", save_xvpics, NULL},
40 };
41 
42 GIMV_PLUGIN_GET_IMPL(plugin_impl, GIMV_PLUGIN_IMAGE_SAVER)
43 
44 GimvPluginInfo gimv_plugin_info =
45 {
46    if_version:    GIMV_PLUGIN_IF_VERSION,
47    name:          N_("XVPICS Image Saver"),
48    version:       "0.2.0",
49    author:        N_("Takuro Ashie"),
50    description:   NULL,
51    get_implement: gimv_plugin_get_impl,
52    get_mime_type: NULL,
53    get_prefs_ui:  NULL,
54 };
55 
56 
57 static gboolean
save_xvpics(GimvImageSaver * saver,GimvImage * image,const gchar * filename,const gchar * format)58 save_xvpics (GimvImageSaver *saver,
59              GimvImage *image,
60              const gchar *filename,
61              const gchar *format)
62 {
63    GimvImageInfo *info;
64    FILE *handle;
65    gint width, height, rowstride;
66    guchar *pixels, *buf;
67    gboolean success;
68    gint orig_width = -1, orig_height = -1, orig_size = -1;
69    gint y;
70 
71    g_return_val_if_fail (image, FALSE);
72    g_return_val_if_fail (filename, FALSE);
73    g_return_val_if_fail (filename[0] != '\0', FALSE);
74 
75    handle = fopen(filename, "wb");
76    if (!handle)
77       return FALSE;
78 
79    width  = gimv_image_width (image);
80    height = gimv_image_height (image);
81    pixels = gimv_image_get_pixels (image);
82    rowstride = gimv_image_rowstride (image);
83 
84    info = gimv_image_saver_get_image_info (saver);
85    if (info) {
86       gimv_image_info_get_image_size (info, &orig_width, &orig_height);
87       orig_size = info->st.st_size;
88    }
89    success = fprintf(handle,
90                      "P7 332\n"
91                      "#IMGINFO:%dx%d RGB (%d bytes)\n"
92                      "#END_OF_COMMENTS\n"
93                      "%d %d 255\n",
94                      orig_width, orig_height, orig_size,
95                      width, height);
96    if (!success) {
97       fclose(handle);
98       return FALSE;
99    }
100 
101    buf = ditherinit (width);
102    for (y = 0; y < height; y++) {
103       ditherline(pixels + y * rowstride, y, width);
104       fwrite(buf, 1, width, handle);
105    }
106    ditherfinish();
107 
108    fclose(handle);
109    return TRUE;
110 }
111