clang-tidy: readability-non-const-parameter
[geeqie.git] / src / image-load-psd.cc
index 5fa3251..6fb9e5f 100644 (file)
@@ -17,9 +17,9 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * 
+ *
  * Derived from:
- * 
+ *
  * GdkPixbuf library - PSD image loader
  *
  * Copyright (C) 2008 Jan Dudek
@@ -53,8 +53,7 @@
 #include "image-load.h"
 #include "image-load-psd.h"
 
-typedef struct _ImageLoaderPSD ImageLoaderPSD;
-struct _ImageLoaderPSD {
+struct ImageLoaderPSD {
        ImageLoaderBackendCbAreaUpdated area_updated_cb;
        ImageLoaderBackendCbSize size_cb;
        ImageLoaderBackendCbAreaPrepared area_prepared_cb;
@@ -65,7 +64,7 @@ struct _ImageLoaderPSD {
        gboolean abort;
 };
 
-typedef struct
+struct PsdHeader
 {
        guchar  signature[4];  /* file ID, always "8BPS" */
        guint16 version;       /* version number, always 1 */
@@ -75,11 +74,11 @@ typedef struct
        guint32 columns;       /* width of image in pixels (1-30000) */
        guint16 depth;         /* number of bits per channel (1, 8, 16 or 32) */
        guint16 color_mode;    /* color mode as defined below */
-} PsdHeader;
+};
 
 #define PSD_HEADER_SIZE 26
 
-typedef enum
+enum PsdColorMode
 {
        PSD_MODE_MONO = 0,
        PSD_MODE_GRAYSCALE = 1,
@@ -89,15 +88,15 @@ typedef enum
        PSD_MODE_MULTICHANNEL = 7,
        PSD_MODE_DUOTONE = 8,
        PSD_MODE_LAB = 9,
-} PsdColorMode;
+};
 
-typedef enum
+enum PsdCompressionType
 {
        PSD_COMPRESSION_NONE = 0,
        PSD_COMPRESSION_RLE = 1
-} PsdCompressionType;
+};
 
-typedef enum
+enum PsdReadState
 {
        PSD_STATE_HEADER,
        PSD_STATE_COLOR_MODE_BLOCK,
@@ -107,12 +106,12 @@ typedef enum
        PSD_STATE_LINES_LENGTHS,
        PSD_STATE_CHANNEL_DATA,
        PSD_STATE_DONE
-} PsdReadState;
+};
 
-typedef struct
+struct PsdContext
 {
        PsdReadState       state;
-       
+
        GdkPixbuf*                  pixbuf;
        gpointer                    user_data;
 
@@ -135,17 +134,17 @@ typedef struct
        guint              pos;
        guint16*           lines_lengths;
        gboolean           finalized;
-} PsdContext;
+};
 
 
 static guint16
-read_uint16 (guchar* buf)
+read_uint16 (const guchar* buf)
 {
        return (buf[0] << 8) | buf[1];
 }
 
 static guint32
-read_uint32 (guchar* buf)
+read_uint32 (const guchar* buf)
 {
        return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
 }
@@ -160,7 +159,7 @@ static PsdHeader
 psd_parse_header (guchar* str)
 {
        PsdHeader hd;
-       
+
        memcpy(hd.signature, str, 4);
        hd.version = read_uint16(str + 4);
        hd.channels = read_uint16(str + 12);
@@ -214,16 +213,18 @@ skip_block (PsdContext* context, const guchar** data, guint* size)
                        return FALSE;
                }
        }
-       if (*size < context->bytes_to_skip) {
+
+       if (*size < context->bytes_to_skip)
+               {
                *data += *size;
                context->bytes_to_skip -= *size;
                *size = 0;
                return FALSE;
-       } else {
-               *size -= context->bytes_to_skip;
-               *data += context->bytes_to_skip;
-               return TRUE;
-       }
+               }
+
+       *size -= context->bytes_to_skip;
+       *data += context->bytes_to_skip;
+       return TRUE;
 }
 
 /*
@@ -234,33 +235,42 @@ decompress_line(const guchar* src, guint line_length, guchar* dest)
 {
        guint16 bytes_read = 0;
        int k;
-       while (bytes_read < line_length) {
+       while (bytes_read < line_length)
+               {
                gchar byte = src[bytes_read];
                ++bytes_read;
-       
-               if (byte == -128) {
+
+               if (byte == -128)
+                       {
                        continue;
-               } else if (byte > -1) {
+                       }
+
+               if (byte > -1)
+                       {
                        gint count = byte + 1;
-               
+
                        /* copy next count bytes */
-                       for (k = 0; k < count; ++k) {
+                       for (k = 0; k < count; ++k)
+                               {
                                *dest = src[bytes_read];
                                ++dest;
                                ++bytes_read;
+                               }
                        }
-               } else {
+               else
+                       {
                        gint count = -byte + 1;
-               
+
                        /* copy next byte count times */
                        guchar next_byte = src[bytes_read];
-                       ++bytes_read; 
-                       for (k = 0; k < count; ++k) {
+                       ++bytes_read;
+                       for (k = 0; k < count; ++k)
+                               {
                                *dest = next_byte;
                                ++dest;
+                               }
                        }
                }
-       }
 }
 
 static void
@@ -284,10 +294,10 @@ static void free_context(PsdContext *ctx)
        g_free(ctx);
 }
 
-static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize count, GError **UNUSED(error))
+static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize count, GError **)
 {
        auto ld = static_cast<ImageLoaderPSD *>(loader);
-       auto  ctx = g_new0(PsdContext, 1);
+       auto ctx = g_new0(PsdContext, 1);
        guint i;
        guint32 j;
        guint size = count;
@@ -298,11 +308,11 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
        ctx->buffer = static_cast<guchar *>(g_malloc(PSD_HEADER_SIZE));
        reset_context_buffer(ctx);
 
-       ctx->ch_bufs = NULL;
+       ctx->ch_bufs = nullptr;
        ctx->curr_ch = 0;
        ctx->curr_row = 0;
        ctx->pos = 0;
-       ctx->lines_lengths = NULL;
+       ctx->lines_lengths = nullptr;
        ctx->finalized = FALSE;
 
        while (size > 0) {
@@ -320,7 +330,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                        ctx->depth = hd.depth;
                                        ctx->depth_bytes = (ctx->depth/8 > 0 ? ctx->depth/8 : 1);
                                        ctx->color_mode = static_cast<PsdColorMode>(hd.color_mode);
-                                       
+
                                        if (ctx->color_mode != PSD_MODE_RGB
                                            && ctx->color_mode != PSD_MODE_GRAYSCALE
                                            && ctx->color_mode != PSD_MODE_CMYK
@@ -330,7 +340,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                                free_context(ctx);
                                                return FALSE;
                                        }
-                                       
+
                                        if (ctx->depth != 8 && ctx->depth != 16) {
                                                log_printf("warning: psd - Unsupported color depth\n");
                                                free_context(ctx);
@@ -341,33 +351,33 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                           row in RLE compressed format. 2*width should be enough */
                                        g_free(ctx->buffer);
                                        ctx->buffer = static_cast<guchar *>(g_malloc(ctx->width * 2 * ctx->depth_bytes));
-                                       
+
                                        /* this will be needed for RLE decompression */
                                        ctx->lines_lengths =
                                                static_cast<guint16 *>(g_malloc(2 * ctx->channels * ctx->height));
-                                       
+
                                        ctx->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
                                                FALSE, 8, ctx->width, ctx->height);
 
-                                       if (ctx->lines_lengths == NULL || ctx->buffer == NULL ||
-                                               ctx->pixbuf == NULL)
+                                       if (ctx->lines_lengths == nullptr || ctx->buffer == nullptr ||
+                                               ctx->pixbuf == nullptr)
                                        {
                                                log_printf("warning: Insufficient memory to load PSD image file\n");
                                                free_context(ctx);
                                                return FALSE;
                                        }
-                                       
+
                                        /* create separate buffers for each channel */
                                        ctx->ch_bufs = static_cast<guchar **>(g_malloc(sizeof(guchar*) * ctx->channels));
                                        for (i = 0; i < ctx->channels; i++) {
                                                ctx->ch_bufs[i] =
                                                        static_cast<guchar *>(g_malloc(ctx->width*ctx->height*ctx->depth_bytes));
 
-                                               if (ctx->ch_bufs[i] == NULL) {
+                                               if (ctx->ch_bufs[i] == nullptr) {
                                                log_printf("warning: Insufficient memory to load PSD image file\n");
                                                free_context(ctx);
                                                return FALSE;
-                                               }       
+                                               }
                                        }
 
                                        ctx->state = PSD_STATE_COLOR_MODE_BLOCK;
@@ -430,7 +440,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                                line_length = ctx->lines_lengths[
                                                        ctx->curr_ch * ctx->height + ctx->curr_row];
                                        }
-                                       
+
                                        if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size,
                                                        line_length))
                                        {
@@ -442,10 +452,10 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                                        memcpy(ctx->ch_bufs[ctx->curr_ch] + ctx->pos,
                                                                ctx->buffer, line_length);
                                                }
-                                               
+
                                                ctx->pos += ctx->width * ctx->depth_bytes;
                                                ++ctx->curr_row;
-                                       
+
                                                if (ctx->curr_row >= ctx->height) {
                                                        ++ctx->curr_ch;
                                                        ctx->curr_row = 0;
@@ -454,7 +464,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                                                ctx->state = PSD_STATE_DONE;
                                                        }
                                                }
-                                               
+
                                                reset_context_buffer(ctx);
                                        }
                                }
@@ -465,7 +475,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                break;
                }
        }
-       
+
        if (ctx->state == PSD_STATE_DONE && !ctx->finalized) {
                /* convert or copy channel buffers to our GdkPixbuf */
                guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
@@ -493,7 +503,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                } else if (ctx->color_mode == PSD_MODE_CMYK) {
                        /* unfortunately, this doesn't work 100% correctly...
                           CMYK-RGB conversion distorts colors significantly  */
-               
+
                        guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
                        for (i = 0; i < ctx->height; i++) {
                                for (j = 0; j < ctx->width; j++) {
@@ -505,7 +515,7 @@ static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize
                                                static_cast<double>(ctx->ch_bufs[2][ctx->width*i + j]) / 255.0;
                                        double k = 1.0 -
                                                static_cast<double>(ctx->ch_bufs[3][ctx->width*i + j]) / 255.0;
-                                       
+
                                        pixels[3*j+0] = (1.0 - (c * (1.0 - k) + k)) * 255.0;
                                        pixels[3*j+1] = (1.0 - (m * (1.0 - k) + k)) * 255.0;
                                        pixels[3*j+2] = (1.0 - (y * (1.0 - k) + k)) * 255.0;
@@ -534,7 +544,7 @@ static gpointer image_loader_psd_new(ImageLoaderBackendCbAreaUpdated area_update
        loader->size_cb = size_cb;
        loader->area_prepared_cb = area_prepared_cb;
        loader->data = data;
-       return (gpointer) loader;
+       return loader;
 }
 
 static void image_loader_psd_set_size(gpointer loader, int width, int height)
@@ -550,18 +560,18 @@ static GdkPixbuf* image_loader_psd_get_pixbuf(gpointer loader)
        return ld->pixbuf;
 }
 
-static gchar* image_loader_psd_get_format_name(gpointer UNUSED(loader))
+static gchar* image_loader_psd_get_format_name(gpointer)
 {
        return g_strdup("psd");
 }
 
-static gchar** image_loader_psd_get_format_mime_types(gpointer UNUSED(loader))
+static gchar** image_loader_psd_get_format_mime_types(gpointer)
 {
-       static const gchar *mime[] = {"application/psd", NULL};
+       static const gchar *mime[] = {"application/psd", nullptr};
        return g_strdupv(const_cast<gchar **>(mime));
 }
 
-static gboolean image_loader_psd_close(gpointer UNUSED(loader), GError **UNUSED(error))
+static gboolean image_loader_psd_close(gpointer, GError **)
 {
        return TRUE;
 }
@@ -584,7 +594,7 @@ void image_loader_backend_set_psd(ImageLoaderBackend *funcs)
        funcs->loader_new = image_loader_psd_new;
        funcs->set_size = image_loader_psd_set_size;
        funcs->load = image_loader_psd_load;
-       funcs->write = NULL;
+       funcs->write = nullptr;
        funcs->get_pixbuf = image_loader_psd_get_pixbuf;
        funcs->close = image_loader_psd_close;
        funcs->abort = image_loader_psd_abort;