File: imageio.c

package info (click to toggle)
mapcache 1.14.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: ansic: 33,609; xml: 889; sh: 183; makefile: 61; python: 48
file content (141 lines) | stat: -rw-r--r-- 4,860 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/******************************************************************************
 * $Id$
 *
 * Project:  MapServer
 * Purpose:  MapCache tile caching support file: high level image format I/O
 * Author:   Thomas Bonfort and the MapServer team.
 *
 ******************************************************************************
 * Copyright (c) 1996-2011 Regents of the University of Minnesota.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

#include "mapcache.h"
#include <png.h>
#include <jpeglib.h>

/**\addtogroup imageio*/
/** @{ */

int mapcache_imageio_is_valid_format(mapcache_context *ctx, mapcache_buffer *buffer)
{
  mapcache_image_format_type t = mapcache_imageio_header_sniff(ctx,buffer);
  if(t==GC_PNG || t==GC_JPEG) {
    return MAPCACHE_TRUE;
  } else {
    return MAPCACHE_FALSE;
  }
}

mapcache_image_format_type mapcache_imageio_header_sniff(mapcache_context *ctx, mapcache_buffer *buffer)
{
  if(!buffer) {
    return GC_UNKNOWN;
  }
  if(buffer->size >= 8 && png_sig_cmp((png_bytep)buffer->buf, 0, 8) == 0) {
    return GC_PNG;
  } else if(buffer->size >= 2 && ((unsigned char*)buffer->buf)[0] == 0xFF && ((unsigned char*)buffer->buf)[1] == 0xD8) {
    return GC_JPEG;
  } else {
    return GC_UNKNOWN;
  }
}

mapcache_image_alpha_type mapcache_imageio_alpha_sniff(mapcache_context *ctx, mapcache_buffer *buffer)
{
  const unsigned char * b = buffer->buf;
  mapcache_image_format_type t = mapcache_imageio_header_sniff(ctx,buffer);
  mapcache_image_alpha_type alpha_type;

  switch (t) {
    case GC_JPEG:
      // JPEG files are opaque
      alpha_type = MC_ALPHA_NO;
      break;
    case GC_PNG:
      if (buffer->size >= 26) {
        // Check color type of PNG file in IHDR chunk
        if ( (b[12]|32)=='i' && (b[13]|32)=='h' && (b[14]|32)=='d' && (b[15]|32)=='r' ) {
          switch (b[25]) {
            case 4:
            case 6:
              // Gray or RGB with alpha
              alpha_type = MC_ALPHA_YES;
              break;
            default:
              // Other colortypes have no alpha channel
              alpha_type = MC_ALPHA_NO;
          }
        }
      } else {
        alpha_type = MC_ALPHA_UNKNOWN;
      }
      break;
    default:
      alpha_type = MC_ALPHA_UNKNOWN;
      break;
  }
  return alpha_type;
}

mapcache_image* mapcache_imageio_decode(mapcache_context *ctx, mapcache_buffer *buffer)
{
  mapcache_image_format_type type = mapcache_imageio_header_sniff(ctx,buffer);
  if(type == GC_PNG) {
    return _mapcache_imageio_png_decode(ctx,buffer);
  } else if(type == GC_JPEG) {
    return _mapcache_imageio_jpeg_decode(ctx,buffer);
  } else {
    ctx->set_error(ctx, 500, "mapcache_imageio_decode: unrecognized image format");
    return NULL;
  }
}

void mapcache_image_create_empty(mapcache_context *ctx, mapcache_cfg *cfg)
{
  unsigned int color=0;

  /* create a transparent image for PNG, and a white one for jpeg */
  if(cfg->default_image_format->mime_type && !strstr(cfg->default_image_format->mime_type,"png")) {
    color = 0xffffffff;
  }
  cfg->empty_image = cfg->default_image_format->create_empty_image(ctx, cfg->default_image_format,
                     256,256, color);
  GC_CHECK_ERROR(ctx);
}

void mapcache_imageio_decode_to_image(mapcache_context *ctx, mapcache_buffer *buffer,
                                      mapcache_image *image)
{
  mapcache_image_format_type type = mapcache_imageio_header_sniff(ctx,buffer);
  if(type == GC_PNG) {
    _mapcache_imageio_png_decode_to_image(ctx,buffer,image);
  } else if(type == GC_JPEG) {
    _mapcache_imageio_jpeg_decode_to_image(ctx,buffer,image);
  } else {
    ctx->set_error(ctx, 500, "mapcache_imageio_decode: unrecognized image format");
  }
  return;
}

/** @} */

/* vim: ts=2 sts=2 et sw=2
*/