Commit 095314f0 authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer
Browse files

made gimp_imagefile_save_thumbnail() return a gboolean indicating success.

2002-04-19  Michael Natterer  <mitch@gimp.org>

	* app/core/gimpimagefile.[ch]: made gimp_imagefile_save_thumbnail()
	return a gboolean indicating success.

	* app/file/file-save.c
	* tools/pdbgen/pdb/fileops.pdb: use gimp_imagefile_save_thumbnail()
	to save thumbnails.

	* app/file/file-utils.[ch]: removed the .xvpics thumbnail saving code.

	* app/pdb/fileops_cmds.c: regenerated.
parent 84ee76d5
2002-04-19 Michael Natterer <mitch@gimp.org>
* app/core/gimpimagefile.[ch]: made gimp_imagefile_save_thumbnail()
return a gboolean indicating success.
* app/file/file-save.c
* tools/pdbgen/pdb/fileops.pdb: use gimp_imagefile_save_thumbnail()
to save thumbnails.
* app/file/file-utils.[ch]: removed the .xvpics thumbnail saving code.
* app/pdb/fileops_cmds.c: regenerated.
2002-04-19 Michael Natterer <mitch@gimp.org>
 
* app/file/file-open.[ch]: all functions return the opened image
......
......@@ -98,7 +98,7 @@ static TempBuf * gimp_imagefile_get_new_preview (GimpViewable *viewable,
static TempBuf * gimp_imagefile_read_png_thumb (GimpImagefile *imagefile,
gint size);
static void gimp_imagefile_save_png_thumb (GimpImagefile *imagefile,
static gboolean gimp_imagefile_save_png_thumb (GimpImagefile *imagefile,
GimpImage *gimage,
const gchar *thumb_name,
time_t image_mtime,
......@@ -376,46 +376,51 @@ gimp_imagefile_create_thumbnail (GimpImagefile *imagefile)
}
}
void
gboolean
gimp_imagefile_save_thumbnail (GimpImagefile *imagefile,
GimpImage *gimage)
{
const gchar *uri;
const gchar *image_uri;
gchar *filename;
gchar *thumb_name;
time_t image_mtime;
off_t image_size;
gboolean success = FALSE;
g_return_if_fail (GIMP_IS_IMAGEFILE (imagefile));
g_return_if_fail (GIMP_IS_IMAGE (gimage));
g_return_val_if_fail (GIMP_IS_IMAGEFILE (imagefile), FALSE);
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
uri = gimp_object_get_name (GIMP_OBJECT (imagefile));
uri = gimp_object_get_name (GIMP_OBJECT (imagefile));
image_uri = gimp_object_get_name (GIMP_OBJECT (imagefile));
g_return_if_fail (! strcmp (uri, gimp_object_get_name (GIMP_OBJECT (gimage))));
g_return_val_if_fail (uri && image_uri && ! strcmp (uri, image_uri), FALSE);
filename = g_filename_from_uri (uri, NULL, NULL);
/* no thumbnails of remote images :-( */
if (! filename)
return;
return FALSE;
thumb_name =
gimp_imagefile_png_thumb_path (uri, GIMP_IMAGEFILE_THUMB_SIZE_NORMAL);
/* the thumbnail directory doesn't exist and couldn't be created */
if (! thumb_name)
return;
return FALSE;
if (gimp_imagefile_test (filename, &image_mtime, &image_size))
{
gimp_imagefile_save_png_thumb (imagefile,
gimage,
thumb_name,
image_mtime,
image_size);
success = gimp_imagefile_save_png_thumb (imagefile,
gimage,
thumb_name,
image_mtime,
image_size);
}
g_free (thumb_name);
return success;
}
static void
......@@ -745,7 +750,7 @@ gimp_imagefile_read_png_thumb (GimpImagefile *imagefile,
return temp_buf;
}
static void
static gboolean
gimp_imagefile_save_png_thumb (GimpImagefile *imagefile,
GimpImage *gimage,
const gchar *thumb_name,
......@@ -756,6 +761,7 @@ gimp_imagefile_save_png_thumb (GimpImagefile *imagefile,
gchar *temp_name = NULL;
GdkPixbuf *pixbuf;
gint width, height;
gboolean success = FALSE;
uri = gimp_object_get_name (GIMP_OBJECT (imagefile));
......@@ -809,17 +815,19 @@ gimp_imagefile_save_png_thumb (GimpImagefile *imagefile,
s_str = g_strdup_printf ("%ld", image_size);
l_str = g_strdup_printf ("%d", gimage->layers->num_children);
if (! gdk_pixbuf_save (pixbuf, thumb_name, "png", &error,
TAG_DESCRIPTION, desc,
TAG_SOFTWARE, "The GIMP",
TAG_THUMB_URI, uri,
TAG_THUMB_MTIME, t_str,
TAG_THUMB_SIZE, s_str,
TAG_THUMB_IMAGE_WIDTH, w_str,
TAG_THUMB_IMAGE_HEIGHT, h_str,
TAG_THUMB_GIMP_TYPE, type_str,
TAG_THUMB_GIMP_LAYERS, l_str,
NULL))
success = gdk_pixbuf_save (pixbuf, thumb_name, "png", &error,
TAG_DESCRIPTION, desc,
TAG_SOFTWARE, "The GIMP",
TAG_THUMB_URI, uri,
TAG_THUMB_MTIME, t_str,
TAG_THUMB_SIZE, s_str,
TAG_THUMB_IMAGE_WIDTH, w_str,
TAG_THUMB_IMAGE_HEIGHT, h_str,
TAG_THUMB_GIMP_TYPE, type_str,
TAG_THUMB_GIMP_LAYERS, l_str,
NULL);
if (! success)
{
g_message (_("Couldn't write thumbnail for '%s'\nas '%s'.\n%s"),
uri, thumb_name, error->message);
......@@ -839,6 +847,8 @@ gimp_imagefile_save_png_thumb (GimpImagefile *imagefile,
g_free (temp_name);
gimp_imagefile_update (imagefile);
return success;
}
static const gchar *
......
......@@ -83,7 +83,7 @@ GType gimp_imagefile_get_type (void) G_GNUC_CONST;
GimpImagefile * gimp_imagefile_new (const gchar *uri);
void gimp_imagefile_update (GimpImagefile *imagefile);
void gimp_imagefile_create_thumbnail (GimpImagefile *imagefile);
void gimp_imagefile_save_thumbnail (GimpImagefile *imagefile,
gboolean gimp_imagefile_save_thumbnail (GimpImagefile *imagefile,
GimpImage *gimage);
const gchar * gimp_imagefile_get_description (GimpImagefile *imagefile);
......
......@@ -46,6 +46,7 @@
#include "core/gimpdocuments.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimagefile.h"
#include "pdb/procedural_db.h"
......@@ -162,29 +163,37 @@ file_save (GimpImage *gimage,
if (status == GIMP_PDB_SUCCESS)
{
GimpImagefile *imagefile;
/* set this image to clean */
gimp_image_clean_all (gimage);
gimp_documents_add (gimage->gimp, uri);
imagefile = gimp_documents_add (gimage->gimp, uri);
/* use the same plug-in for this image next time */
/* DISABLED - gets stuck on first saved format... needs
attention --Adam */
/* gimage_set_save_proc(gimage, file_proc); */
if (set_uri)
{
/* set the image title */
gimp_image_set_uri (gimage, uri);
}
/* Write a thumbnail for the saved image, where appropriate */
if (gimage->gimp->config->write_thumbnails)
{
TempBuf *tempbuf;
if (set_uri)
{
gimp_imagefile_save_thumbnail (imagefile, gimage);
}
else
{
gchar *saved_uri;
tempbuf = make_thumb_tempbuf (gimage);
file_save_thumbnail (gimage, filename, tempbuf);
}
saved_uri = GIMP_OBJECT (gimage)->name;
GIMP_OBJECT (gimage)->name = (gchar *) uri;
if (set_uri)
{
/* set the image title */
gimp_image_set_uri (gimage, uri);
gimp_imagefile_save_thumbnail (imagefile, gimage);
GIMP_OBJECT (gimage)->name = saved_uri;
}
}
}
......
......@@ -598,42 +598,6 @@ file_check_magic_list (GSList *magics_list,
}
/* .xvpics thumbnail stuff */
TempBuf *
make_thumb_tempbuf (GimpImage *gimage)
{
gint w, h;
if (gimage->width<=80 && gimage->height<=60)
{
w = gimage->width;
h = gimage->height;
}
else
{
/* Ratio molesting to fit within .xvpic thumbnail size limits */
if (60 * gimage->width < 80 * gimage->height)
{
h = 60;
w = (60 * gimage->width) / gimage->height;
if (w == 0)
w = 1;
}
else
{
w = 80;
h = (80 * gimage->height) / gimage->width;
if (h == 0)
h = 1;
}
}
/*printf("tn: %d x %d -> ", w, h);fflush(stdout);*/
return gimp_viewable_get_preview (GIMP_VIEWABLE (gimage), w, h);
}
/* The readXVThumb function source may be re-used under
the XFree86-style license. <adam@gimp.org> */
guchar *
......@@ -720,147 +684,3 @@ readXVThumb (const gchar *fnam,
return buf;
}
gboolean
file_save_thumbnail (GimpImage *gimage,
const gchar *full_source_filename,
TempBuf *tempbuf)
{
gint i,j;
gint w,h;
guchar *tbd;
gchar *pathname;
gchar *filename;
gchar *xvpathname;
gchar *thumbnailname;
GimpImageBaseType basetype;
FILE *fp;
struct stat statbuf;
if (stat (full_source_filename, &statbuf) != 0)
{
return FALSE;
}
pathname = g_path_get_dirname (full_source_filename);
filename = g_path_get_basename (full_source_filename);
xvpathname = g_build_filename (pathname, ".xvpics", NULL);
thumbnailname = g_build_filename (xvpathname, filename, NULL);
tbd = temp_buf_data (tempbuf);
w = tempbuf->width;
h = tempbuf->height;
/*printf("tn: %d x %d\n", w, h);fflush(stdout);*/
mkdir (xvpathname, 0755);
fp = fopen (thumbnailname, "wb");
g_free (pathname);
g_free (filename);
g_free (xvpathname);
g_free (thumbnailname);
if (fp)
{
basetype = gimp_image_base_type (gimage);
fprintf (fp,
"P7 332\n#IMGINFO:%dx%d %s (%d %s)\n"
"#END_OF_COMMENTS\n%d %d 255\n",
gimage->width, gimage->height,
(basetype == GIMP_RGB) ? "RGB" :
(basetype == GIMP_GRAY) ? "Greyscale" :
(basetype == GIMP_INDEXED) ? "Indexed" :
"(UNKNOWN COLOUR TYPE)",
(int)statbuf.st_size,
(statbuf.st_size == 1) ? "byte" : "bytes",
w, h);
switch (basetype)
{
case GIMP_INDEXED:
case GIMP_RGB:
for (i=0; i<h; i++)
{
/* Do a cheap unidirectional error-spread to look better */
gint rerr=0, gerr=0, berr=0, a;
for (j=0; j<w; j++)
{
gint32 r,g,b;
if (128 & *(tbd + 3))
{
r = *(tbd++) + rerr;
g = *(tbd++) + gerr;
b = *(tbd++) + berr;
tbd++;
}
else
{
a = (( (i^j) & 4 ) << 5) | 64; /* cute. */
r = a + rerr;
g = a + gerr;
b = a + berr;
tbd += 4;
}
r = CLAMP0255 (r);
g = CLAMP0255 (g);
b = CLAMP0255 (b);
fputc(((r>>5)<<5) | ((g>>5)<<2) | (b>>6), fp);
rerr = r - ( (r>>5) * 255 ) / 7;
gerr = g - ( (g>>5) * 255 ) / 7;
berr = b - ( (b>>6) * 255 ) / 3;
}
}
break;
case GIMP_GRAY:
for (i=0; i<h; i++)
{
/* Do a cheap unidirectional error-spread to look better */
gint b3err=0, b2err=0, v, a;
for (j=0; j<w; j++)
{
gint32 b3, b2;
v = *(tbd++);
a = *(tbd++);
if (!(128 & a))
v = (( (i^j) & 4 ) << 5) | 64;
b2 = v + b2err;
b3 = v + b3err;
b2 = CLAMP0255 (b2);
b3 = CLAMP0255 (b3);
fputc(((b3>>5)<<5) | ((b3>>5)<<2) | (b2>>6), fp);
b2err = b2 - ( (b2>>6) * 255 ) / 3;
b3err = b3 - ( (b3>>5) * 255 ) / 7;
}
}
break;
default:
g_warning ("%s: Unknown image type.", G_STRLOC);
break;
}
fclose (fp);
}
else /* Error writing thumbnail */
{
return FALSE;
}
return TRUE;
}
......@@ -34,14 +34,10 @@ gchar * file_utils_uri_to_utf8_filename (const gchar *uri);
/* .xvpics thumbnail stuff */
TempBuf * make_thumb_tempbuf (GimpImage *gimage);
guchar * readXVThumb (const gchar *fnam,
gint *w,
gint *h,
gchar **imginfo /* caller frees if != NULL */);
gboolean file_save_thumbnail (GimpImage *gimage,
const char *full_source_filename,
TempBuf *tempbuf);
#endif /* __FILE_UTILS_H__ */
......@@ -44,6 +44,7 @@
#include "base/base-config.h"
#include "core/gimp.h"
#include "core/gimpimage.h"
#include "core/gimpimagefile.h"
#include "file/file-utils.h"
#include "plug-in/plug-in-proc.h"
#include "plug-in/plug-in.h"
......@@ -330,7 +331,9 @@ file_save_thumbnail_invoker (Gimp *gimp,
gboolean success = TRUE;
GimpImage *gimage;
gchar *filename;
TempBuf *thumb;
GimpImagefile *imagefile;
gchar *uri;
const gchar *image_uri;
gimage = gimp_image_get_by_ID (gimp, args[0].value.pdb_int);
if (gimage == NULL)
......@@ -342,9 +345,31 @@ file_save_thumbnail_invoker (Gimp *gimp,
if (success)
{
thumb = make_thumb_tempbuf (gimage);
if (! file_save_thumbnail (gimage, filename, thumb))
image_uri = gimp_object_get_name (GIMP_OBJECT (gimage));
if (! image_uri)
success = FALSE;
if (success)
{
uri = g_filename_to_uri (filename, NULL, NULL);
if (! uri)
success = FALSE;
if (success)
{
if (strcmp (uri, image_uri))
success = FALSE;
if (success)
{
imagefile = gimp_imagefile_new (uri);
success = gimp_imagefile_save_thumbnail (imagefile, gimage);
g_object_unref (G_OBJECT (imagefile));
}
g_free (uri);
}
}
}
return procedural_db_return_args (&file_save_thumbnail_proc, success);
......
......@@ -242,12 +242,35 @@ HELP
);
%invoke = (
vars => [ 'TempBuf *thumb' ],
vars => [ 'GimpImagefile *imagefile', 'gchar *uri',
'const gchar *image_uri' ],
code => <<'CODE'
{
thumb = make_thumb_tempbuf (gimage);
if (! file_save_thumbnail (gimage, filename, thumb))
image_uri = gimp_object_get_name (GIMP_OBJECT (gimage));
if (! image_uri)
success = FALSE;
if (success)
{
uri = g_filename_to_uri (filename, NULL, NULL);
if (! uri)
success = FALSE;
if (success)
{
if (strcmp (uri, image_uri))
success = FALSE;
if (success)
{
imagefile = gimp_imagefile_new (uri);
success = gimp_imagefile_save_thumbnail (imagefile, gimage);
g_object_unref (G_OBJECT (imagefile));
}
g_free (uri);
}
}
}
CODE
);
......@@ -415,7 +438,7 @@ CODE
);
}
@headers = qw(<sys/types.h> <unistd.h> "core/gimp.h"
@headers = qw(<sys/types.h> <unistd.h> "core/gimp.h" "core/gimpimagefile.h"
"plug-in/plug-in.h" "plug-in/plug-ins.h" "plug-in/plug-in-proc.h"
"file/file-utils.h");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment