Blame gdk-pixbuf/io-gdip-jpeg.c

Packit a4058c
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit a4058c
/* GdkPixbuf library - Win32 GDI+ Pixbuf Loader
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 2008 Dominic Lachowicz
Packit a4058c
 * Copyright (C) 2008 Alberto Ruiz
Packit a4058c
 *
Packit a4058c
 * Authors: Dominic Lachowicz <domlachowicz@gmail.com>
Packit a4058c
 *          Alberto Ruiz <aruiz@gnome.org>
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Lesser General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Lesser General Public License for more  * You should have received a copy of the GNU Lesser General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c

Packit a4058c
#include "config.h"
Packit a4058c
#include <glib/gi18n-lib.h>
Packit a4058c

Packit a4058c
#define INITGUID
Packit a4058c
#include "io-gdip-utils.h"
Packit a4058c

Packit a4058c
DEFINE_GUID(EncoderQuality, 0x1d5be4b5,0xfa4a,0x452d,0x9c,0xdd,0x5d,0xb3,0x51,0x05,0xe7,0xeb);
Packit a4058c

Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__gdip_image_save_JPEG_to_callback (GdkPixbufSaveFunc   save_func,
Packit a4058c
                                              gpointer            user_data,
Packit a4058c
                                              GdkPixbuf          *pixbuf,
Packit a4058c
                                              gchar             **keys,
Packit a4058c
                                              gchar             **values,
Packit a4058c
                                              GError            **error)
Packit a4058c
{
Packit a4058c
  EncoderParameters encoder_params;
Packit a4058c
  LONG quality = 75; /* default; must be between 0 and 100 */
Packit a4058c

Packit a4058c
  if (keys && *keys) {
Packit a4058c
    gchar **kiter = keys;
Packit a4058c
    gchar **viter = values;
Packit a4058c
    
Packit a4058c
    while (*kiter) {
Packit a4058c
      if (strcmp (*kiter, "quality") == 0) {
Packit a4058c
        char *endptr = NULL;
Packit a4058c
        quality = strtol (*viter, &endptr, 10);
Packit a4058c
        
Packit a4058c
        if (endptr == *viter) {
Packit a4058c
          g_set_error (error,
Packit a4058c
                       GDK_PIXBUF_ERROR,
Packit a4058c
                       GDK_PIXBUF_ERROR_BAD_OPTION,
Packit a4058c
                       _("JPEG quality must be a value between 0 and 100; value '%s' could not be parsed."),
Packit a4058c
                       *viter);
Packit a4058c
          
Packit a4058c
          return FALSE;
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
        if (quality < 0 ||
Packit a4058c
            quality > 100) {
Packit a4058c
          /* This is a user-visible error;
Packit a4058c
           * lets people skip the range-checking
Packit a4058c
           * in their app.
Packit a4058c
           */
Packit a4058c
          g_set_error (error,
Packit a4058c
                       GDK_PIXBUF_ERROR,
Packit a4058c
                       GDK_PIXBUF_ERROR_BAD_OPTION,
Packit a4058c
                       _("JPEG quality must be a value between 0 and 100; value '%d' is not allowed."),
Packit a4058c
                       (int)quality);
Packit a4058c
          
Packit a4058c
          return FALSE;
Packit a4058c
        }
Packit a4058c
      } else {
Packit a4058c
        g_warning ("Unrecognized parameter (%s) passed to JPEG saver.", *kiter);
Packit a4058c
      }
Packit a4058c
      
Packit a4058c
      ++kiter;
Packit a4058c
      ++viter;
Packit a4058c
    }
Packit a4058c
  }
Packit a4058c

Packit a4058c
  encoder_params.Count = 1;
Packit a4058c
  encoder_params.Parameter[0].Guid = EncoderQuality;
Packit a4058c
  encoder_params.Parameter[0].Type = EncoderParameterValueTypeLong;
Packit a4058c
  encoder_params.Parameter[0].NumberOfValues = 1;
Packit a4058c
  encoder_params.Parameter[0].Value = &quality;
Packit a4058c
     
Packit a4058c
  return gdip_save_pixbuf (pixbuf, L"image/jpeg", &encoder_params, save_func, user_data, error);
Packit a4058c
}
Packit a4058c

Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__gdip_image_save_JPEG (FILE         *f,
Packit a4058c
                                 GdkPixbuf     *pixbuf,
Packit a4058c
                                 gchar        **keys,
Packit a4058c
                                 gchar        **values,
Packit a4058c
                                 GError       **error)
Packit a4058c
{
Packit a4058c
  return gdk_pixbuf__gdip_image_save_JPEG_to_callback (gdip_save_to_file_callback, f, pixbuf, keys, values, error);
Packit a4058c
}
Packit a4058c

Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__gdip_is_save_option_supported_JPEG (const gchar *option_key)
Packit a4058c
{
Packit a4058c
  if (g_strcmp0 (option_key, "quality") == 0)
Packit a4058c
    return TRUE;
Packit a4058c

Packit a4058c
  return FALSE;
Packit a4058c
}
Packit a4058c

Packit a4058c
#ifndef INCLUDE_gdiplus
Packit a4058c
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
Packit a4058c
#else
Packit a4058c
#define MODULE_ENTRY(function) void _gdk_pixbuf__gdip_jpeg_ ## function
Packit a4058c
#endif
Packit a4058c

Packit a4058c
MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
Packit a4058c
{
Packit a4058c
  gdip_fill_vtable (module);
Packit a4058c

Packit a4058c
  module->save_to_callback = gdk_pixbuf__gdip_image_save_JPEG_to_callback;
Packit a4058c
  module->save = gdk_pixbuf__gdip_image_save_JPEG; /* for gtk < 2.14, you need to implement both. otherwise gdk-pixbuf-queryloaders fails */
Packit a4058c
  module->is_save_option_supported = gdk_pixbuf__gdip_is_save_option_supported_JPEG;
Packit a4058c
}
Packit a4058c

Packit a4058c
MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
Packit a4058c
{
Packit a4058c
  static const GdkPixbufModulePattern signature[] = {
Packit a4058c
    { "\xff\xd8", NULL, 100 }, /* JPEG */
Packit a4058c
    { NULL, NULL, 0 }
Packit a4058c
  };
Packit a4058c

Packit a4058c
  static const gchar *mime_types[] = {
Packit a4058c
    "image/jpeg",
Packit a4058c
    NULL
Packit a4058c
  };
Packit a4058c

Packit a4058c
  static const gchar *extensions[] = {
Packit a4058c
    "jpeg",
Packit a4058c
    "jpe",
Packit a4058c
    "jpg",
Packit a4058c
    NULL
Packit a4058c
  };
Packit a4058c

Packit a4058c
  info->name        = "jpeg";
Packit a4058c
  info->signature   = (GdkPixbufModulePattern *) signature;
Packit a4058c
  info->description = NC_("image format", "JPEG");
Packit a4058c
  info->mime_types  = (gchar **) mime_types;
Packit a4058c
  info->extensions  = (gchar **) extensions;
Packit a4058c
  info->flags       = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
Packit a4058c
  info->license     = "LGPL";
Packit a4058c
}