ZBar bar code reader . http://zbar.sourceforge.net/ ZBar is licensed under the GNU LGPL 2.1 to enable development of both open source and commercial projects.

Dependents:   GR-PEACH_Camera_in_barcode levkov_ov7670

LICENSE

The ZBar Bar Code Reader is Copyright (C) 2007-2009 Jeff Brown <spadix@users.sourceforge.net> The QR Code reader is Copyright (C) 1999-2009 Timothy B. Terriberry <tterribe@xiph.org>

You can redistribute this library and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

ISAAC is based on the public domain implementation by Robert J. Jenkins Jr., and is itself public domain.

Portions of the bit stream reader are copyright (C) The Xiph.Org Foundation 1994-2008, and are licensed under a BSD-style license.

The Reed-Solomon decoder is derived from an implementation (C) 1991-1995 Henry Minsky (hqm@ua.com, hqm@ai.mit.edu), and is licensed under the LGPL with permission.

Committer:
RyoheiHagimoto
Date:
Tue Apr 19 02:19:39 2016 +0000
Revision:
1:500d42699c34
Parent:
0:56c5742b9e2b
Add copying.txt and license.txt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyoheiHagimoto 0:56c5742b9e2b 1 /*------------------------------------------------------------------------
RyoheiHagimoto 0:56c5742b9e2b 2 * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
RyoheiHagimoto 0:56c5742b9e2b 3 *
RyoheiHagimoto 0:56c5742b9e2b 4 * This file is part of the ZBar Bar Code Reader.
RyoheiHagimoto 0:56c5742b9e2b 5 *
RyoheiHagimoto 0:56c5742b9e2b 6 * The ZBar Bar Code Reader is free software; you can redistribute it
RyoheiHagimoto 0:56c5742b9e2b 7 * and/or modify it under the terms of the GNU Lesser Public License as
RyoheiHagimoto 0:56c5742b9e2b 8 * published by the Free Software Foundation; either version 2.1 of
RyoheiHagimoto 0:56c5742b9e2b 9 * the License, or (at your option) any later version.
RyoheiHagimoto 0:56c5742b9e2b 10 *
RyoheiHagimoto 0:56c5742b9e2b 11 * The ZBar Bar Code Reader is distributed in the hope that it will be
RyoheiHagimoto 0:56c5742b9e2b 12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
RyoheiHagimoto 0:56c5742b9e2b 13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
RyoheiHagimoto 0:56c5742b9e2b 14 * GNU Lesser Public License for more details.
RyoheiHagimoto 0:56c5742b9e2b 15 *
RyoheiHagimoto 0:56c5742b9e2b 16 * You should have received a copy of the GNU Lesser Public License
RyoheiHagimoto 0:56c5742b9e2b 17 * along with the ZBar Bar Code Reader; if not, write to the Free
RyoheiHagimoto 0:56c5742b9e2b 18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
RyoheiHagimoto 0:56c5742b9e2b 19 * Boston, MA 02110-1301 USA
RyoheiHagimoto 0:56c5742b9e2b 20 *
RyoheiHagimoto 0:56c5742b9e2b 21 * http://sourceforge.net/projects/zbar
RyoheiHagimoto 0:56c5742b9e2b 22 *------------------------------------------------------------------------*/
RyoheiHagimoto 0:56c5742b9e2b 23
RyoheiHagimoto 0:56c5742b9e2b 24 #include "error.h"
RyoheiHagimoto 0:56c5742b9e2b 25 #include "image.h"
RyoheiHagimoto 0:56c5742b9e2b 26 #include "refcnt.h"
RyoheiHagimoto 0:56c5742b9e2b 27
RyoheiHagimoto 0:56c5742b9e2b 28 zbar_image_t *zbar_image_create ()
RyoheiHagimoto 0:56c5742b9e2b 29 {
RyoheiHagimoto 0:56c5742b9e2b 30 zbar_image_t *img = calloc(1, sizeof(zbar_image_t));
RyoheiHagimoto 0:56c5742b9e2b 31 _zbar_refcnt_init();
RyoheiHagimoto 0:56c5742b9e2b 32 _zbar_image_refcnt(img, 1);
RyoheiHagimoto 0:56c5742b9e2b 33 img->srcidx = -1;
RyoheiHagimoto 0:56c5742b9e2b 34 return(img);
RyoheiHagimoto 0:56c5742b9e2b 35 }
RyoheiHagimoto 0:56c5742b9e2b 36
RyoheiHagimoto 0:56c5742b9e2b 37 void _zbar_image_free (zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 38 {
RyoheiHagimoto 0:56c5742b9e2b 39 if(img->syms) {
RyoheiHagimoto 0:56c5742b9e2b 40 zbar_symbol_set_ref(img->syms, -1);
RyoheiHagimoto 0:56c5742b9e2b 41 img->syms = NULL;
RyoheiHagimoto 0:56c5742b9e2b 42 }
RyoheiHagimoto 0:56c5742b9e2b 43 free(img);
RyoheiHagimoto 0:56c5742b9e2b 44 }
RyoheiHagimoto 0:56c5742b9e2b 45
RyoheiHagimoto 0:56c5742b9e2b 46 void zbar_image_destroy (zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 47 {
RyoheiHagimoto 0:56c5742b9e2b 48 _zbar_image_refcnt(img, -1);
RyoheiHagimoto 0:56c5742b9e2b 49 }
RyoheiHagimoto 0:56c5742b9e2b 50
RyoheiHagimoto 0:56c5742b9e2b 51 void zbar_image_ref (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 52 int refs)
RyoheiHagimoto 0:56c5742b9e2b 53 {
RyoheiHagimoto 0:56c5742b9e2b 54 _zbar_image_refcnt(img, refs);
RyoheiHagimoto 0:56c5742b9e2b 55 }
RyoheiHagimoto 0:56c5742b9e2b 56
RyoheiHagimoto 0:56c5742b9e2b 57 unsigned long zbar_image_get_format (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 58 {
RyoheiHagimoto 0:56c5742b9e2b 59 return(img->format);
RyoheiHagimoto 0:56c5742b9e2b 60 }
RyoheiHagimoto 0:56c5742b9e2b 61
RyoheiHagimoto 0:56c5742b9e2b 62 unsigned zbar_image_get_sequence (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 63 {
RyoheiHagimoto 0:56c5742b9e2b 64 return(img->seq);
RyoheiHagimoto 0:56c5742b9e2b 65 }
RyoheiHagimoto 0:56c5742b9e2b 66
RyoheiHagimoto 0:56c5742b9e2b 67 unsigned zbar_image_get_width (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 68 {
RyoheiHagimoto 0:56c5742b9e2b 69 return(img->width);
RyoheiHagimoto 0:56c5742b9e2b 70 }
RyoheiHagimoto 0:56c5742b9e2b 71
RyoheiHagimoto 0:56c5742b9e2b 72 unsigned zbar_image_get_height (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 73 {
RyoheiHagimoto 0:56c5742b9e2b 74 return(img->height);
RyoheiHagimoto 0:56c5742b9e2b 75 }
RyoheiHagimoto 0:56c5742b9e2b 76
RyoheiHagimoto 0:56c5742b9e2b 77 const void *zbar_image_get_data (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 78 {
RyoheiHagimoto 0:56c5742b9e2b 79 return(img->data);
RyoheiHagimoto 0:56c5742b9e2b 80 }
RyoheiHagimoto 0:56c5742b9e2b 81
RyoheiHagimoto 0:56c5742b9e2b 82 unsigned long zbar_image_get_data_length (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 83 {
RyoheiHagimoto 0:56c5742b9e2b 84 return(img->datalen);
RyoheiHagimoto 0:56c5742b9e2b 85 }
RyoheiHagimoto 0:56c5742b9e2b 86
RyoheiHagimoto 0:56c5742b9e2b 87 void zbar_image_set_format (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 88 unsigned long fmt)
RyoheiHagimoto 0:56c5742b9e2b 89 {
RyoheiHagimoto 0:56c5742b9e2b 90 img->format = fmt;
RyoheiHagimoto 0:56c5742b9e2b 91 }
RyoheiHagimoto 0:56c5742b9e2b 92
RyoheiHagimoto 0:56c5742b9e2b 93 void zbar_image_set_sequence (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 94 unsigned seq)
RyoheiHagimoto 0:56c5742b9e2b 95 {
RyoheiHagimoto 0:56c5742b9e2b 96 img->seq = seq;
RyoheiHagimoto 0:56c5742b9e2b 97 }
RyoheiHagimoto 0:56c5742b9e2b 98
RyoheiHagimoto 0:56c5742b9e2b 99 void zbar_image_set_size (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 100 unsigned w,
RyoheiHagimoto 0:56c5742b9e2b 101 unsigned h)
RyoheiHagimoto 0:56c5742b9e2b 102 {
RyoheiHagimoto 0:56c5742b9e2b 103 img->width = w;
RyoheiHagimoto 0:56c5742b9e2b 104 img->height = h;
RyoheiHagimoto 0:56c5742b9e2b 105 }
RyoheiHagimoto 0:56c5742b9e2b 106
RyoheiHagimoto 0:56c5742b9e2b 107 inline void zbar_image_free_data (zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 108 {
RyoheiHagimoto 0:56c5742b9e2b 109 if(!img)
RyoheiHagimoto 0:56c5742b9e2b 110 return;
RyoheiHagimoto 0:56c5742b9e2b 111 if(img->src) {
RyoheiHagimoto 0:56c5742b9e2b 112 /* replace video image w/new copy */
RyoheiHagimoto 0:56c5742b9e2b 113 assert(img->refcnt); /* FIXME needs lock */
RyoheiHagimoto 0:56c5742b9e2b 114 zbar_image_t *newimg = zbar_image_create();
RyoheiHagimoto 0:56c5742b9e2b 115 memcpy(newimg, img, sizeof(zbar_image_t));
RyoheiHagimoto 0:56c5742b9e2b 116 /* recycle video image */
RyoheiHagimoto 0:56c5742b9e2b 117 newimg->cleanup(newimg);
RyoheiHagimoto 0:56c5742b9e2b 118 /* detach old image from src */
RyoheiHagimoto 0:56c5742b9e2b 119 img->cleanup = NULL;
RyoheiHagimoto 0:56c5742b9e2b 120 img->src = NULL;
RyoheiHagimoto 0:56c5742b9e2b 121 img->srcidx = -1;
RyoheiHagimoto 0:56c5742b9e2b 122 }
RyoheiHagimoto 0:56c5742b9e2b 123 else if(img->cleanup && img->data) {
RyoheiHagimoto 0:56c5742b9e2b 124 if(img->cleanup != zbar_image_free_data) {
RyoheiHagimoto 0:56c5742b9e2b 125 /* using function address to detect this case is a bad idea;
RyoheiHagimoto 0:56c5742b9e2b 126 * windows link libraries add an extra layer of indirection...
RyoheiHagimoto 0:56c5742b9e2b 127 * this works around that problem (bug #2796277)
RyoheiHagimoto 0:56c5742b9e2b 128 */
RyoheiHagimoto 0:56c5742b9e2b 129 zbar_image_cleanup_handler_t *cleanup = img->cleanup;
RyoheiHagimoto 0:56c5742b9e2b 130 img->cleanup = zbar_image_free_data;
RyoheiHagimoto 0:56c5742b9e2b 131 cleanup(img);
RyoheiHagimoto 0:56c5742b9e2b 132 }
RyoheiHagimoto 0:56c5742b9e2b 133 #if (1) /* img->data is static address, not need free() */
RyoheiHagimoto 0:56c5742b9e2b 134 #else
RyoheiHagimoto 0:56c5742b9e2b 135 else {
RyoheiHagimoto 0:56c5742b9e2b 136 free((void*)img->data);
RyoheiHagimoto 0:56c5742b9e2b 137 }
RyoheiHagimoto 0:56c5742b9e2b 138 #endif
RyoheiHagimoto 0:56c5742b9e2b 139 }
RyoheiHagimoto 0:56c5742b9e2b 140 img->data = NULL;
RyoheiHagimoto 0:56c5742b9e2b 141 }
RyoheiHagimoto 0:56c5742b9e2b 142
RyoheiHagimoto 0:56c5742b9e2b 143 void zbar_image_set_data (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 144 const void *data,
RyoheiHagimoto 0:56c5742b9e2b 145 unsigned long len,
RyoheiHagimoto 0:56c5742b9e2b 146 zbar_image_cleanup_handler_t *cleanup)
RyoheiHagimoto 0:56c5742b9e2b 147 {
RyoheiHagimoto 0:56c5742b9e2b 148 zbar_image_free_data(img);
RyoheiHagimoto 0:56c5742b9e2b 149 img->data = data;
RyoheiHagimoto 0:56c5742b9e2b 150 img->datalen = len;
RyoheiHagimoto 0:56c5742b9e2b 151 img->cleanup = cleanup;
RyoheiHagimoto 0:56c5742b9e2b 152 }
RyoheiHagimoto 0:56c5742b9e2b 153
RyoheiHagimoto 0:56c5742b9e2b 154 void zbar_image_set_userdata (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 155 void *userdata)
RyoheiHagimoto 0:56c5742b9e2b 156 {
RyoheiHagimoto 0:56c5742b9e2b 157 img->userdata = userdata;
RyoheiHagimoto 0:56c5742b9e2b 158 }
RyoheiHagimoto 0:56c5742b9e2b 159
RyoheiHagimoto 0:56c5742b9e2b 160 void *zbar_image_get_userdata (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 161 {
RyoheiHagimoto 0:56c5742b9e2b 162 return(img->userdata);
RyoheiHagimoto 0:56c5742b9e2b 163 }
RyoheiHagimoto 0:56c5742b9e2b 164
RyoheiHagimoto 0:56c5742b9e2b 165 zbar_image_t *zbar_image_copy (const zbar_image_t *src)
RyoheiHagimoto 0:56c5742b9e2b 166 {
RyoheiHagimoto 0:56c5742b9e2b 167 zbar_image_t *dst = zbar_image_create();
RyoheiHagimoto 0:56c5742b9e2b 168 dst->format = src->format;
RyoheiHagimoto 0:56c5742b9e2b 169 dst->width = src->width;
RyoheiHagimoto 0:56c5742b9e2b 170 dst->height = src->height;
RyoheiHagimoto 0:56c5742b9e2b 171 dst->datalen = src->datalen;
RyoheiHagimoto 0:56c5742b9e2b 172 dst->data = malloc(src->datalen);
RyoheiHagimoto 0:56c5742b9e2b 173 assert(dst->data);
RyoheiHagimoto 0:56c5742b9e2b 174 memcpy((void*)dst->data, src->data, src->datalen);
RyoheiHagimoto 0:56c5742b9e2b 175 dst->cleanup = zbar_image_free_data;
RyoheiHagimoto 0:56c5742b9e2b 176 return(dst);
RyoheiHagimoto 0:56c5742b9e2b 177 }
RyoheiHagimoto 0:56c5742b9e2b 178
RyoheiHagimoto 0:56c5742b9e2b 179 const zbar_symbol_set_t *zbar_image_get_symbols (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 180 {
RyoheiHagimoto 0:56c5742b9e2b 181 return(img->syms);
RyoheiHagimoto 0:56c5742b9e2b 182 }
RyoheiHagimoto 0:56c5742b9e2b 183
RyoheiHagimoto 0:56c5742b9e2b 184 void zbar_image_set_symbols (zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 185 const zbar_symbol_set_t *syms)
RyoheiHagimoto 0:56c5742b9e2b 186 {
RyoheiHagimoto 0:56c5742b9e2b 187 if(img->syms)
RyoheiHagimoto 0:56c5742b9e2b 188 zbar_symbol_set_ref(img->syms, -1);
RyoheiHagimoto 0:56c5742b9e2b 189 img->syms = (zbar_symbol_set_t*)syms;
RyoheiHagimoto 0:56c5742b9e2b 190 if(syms)
RyoheiHagimoto 0:56c5742b9e2b 191 zbar_symbol_set_ref(img->syms, 1);
RyoheiHagimoto 0:56c5742b9e2b 192 }
RyoheiHagimoto 0:56c5742b9e2b 193
RyoheiHagimoto 0:56c5742b9e2b 194 const zbar_symbol_t *zbar_image_first_symbol (const zbar_image_t *img)
RyoheiHagimoto 0:56c5742b9e2b 195 {
RyoheiHagimoto 0:56c5742b9e2b 196 return((img->syms) ? img->syms->head : NULL);
RyoheiHagimoto 0:56c5742b9e2b 197 }
RyoheiHagimoto 0:56c5742b9e2b 198
RyoheiHagimoto 0:56c5742b9e2b 199 typedef struct zimg_hdr_s {
RyoheiHagimoto 0:56c5742b9e2b 200 uint32_t magic, format;
RyoheiHagimoto 0:56c5742b9e2b 201 uint16_t width, height;
RyoheiHagimoto 0:56c5742b9e2b 202 uint32_t size;
RyoheiHagimoto 0:56c5742b9e2b 203 } zimg_hdr_t;
RyoheiHagimoto 0:56c5742b9e2b 204
RyoheiHagimoto 0:56c5742b9e2b 205 int zbar_image_write (const zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 206 const char *filebase)
RyoheiHagimoto 0:56c5742b9e2b 207 {
RyoheiHagimoto 0:56c5742b9e2b 208 int len = strlen(filebase) + 16;
RyoheiHagimoto 0:56c5742b9e2b 209 char filename[len];
RyoheiHagimoto 0:56c5742b9e2b 210 strcpy(filename, filebase);
RyoheiHagimoto 0:56c5742b9e2b 211 int n = 0;
RyoheiHagimoto 0:56c5742b9e2b 212 if(*(char*)&img->format >= ' ')
RyoheiHagimoto 0:56c5742b9e2b 213 n = snprintf(filename, len, "%s.%.4s.zimg",
RyoheiHagimoto 0:56c5742b9e2b 214 filebase, (char*)&img->format);
RyoheiHagimoto 0:56c5742b9e2b 215 else
RyoheiHagimoto 0:56c5742b9e2b 216 n = snprintf(filename, len, "%s.%08" PRIx32 ".zimg",
RyoheiHagimoto 0:56c5742b9e2b 217 filebase, img->format);
RyoheiHagimoto 0:56c5742b9e2b 218 assert(n < len);
RyoheiHagimoto 0:56c5742b9e2b 219 filename[len] = '\0';
RyoheiHagimoto 0:56c5742b9e2b 220
RyoheiHagimoto 0:56c5742b9e2b 221 zprintf(1, "dumping %.4s(%08" PRIx32 ") image to %s\n",
RyoheiHagimoto 0:56c5742b9e2b 222 (char*)&img->format, img->format, filename);
RyoheiHagimoto 0:56c5742b9e2b 223
RyoheiHagimoto 0:56c5742b9e2b 224 FILE *f = fopen(filename, "w");
RyoheiHagimoto 0:56c5742b9e2b 225 if(!f) {
RyoheiHagimoto 0:56c5742b9e2b 226 int rc = errno;
RyoheiHagimoto 0:56c5742b9e2b 227 zprintf(1, "ERROR opening %s: %s\n", filename, strerror(rc));
RyoheiHagimoto 0:56c5742b9e2b 228 return(rc);
RyoheiHagimoto 0:56c5742b9e2b 229 }
RyoheiHagimoto 0:56c5742b9e2b 230
RyoheiHagimoto 0:56c5742b9e2b 231 zimg_hdr_t hdr;
RyoheiHagimoto 0:56c5742b9e2b 232 hdr.magic = 0x676d697a;
RyoheiHagimoto 0:56c5742b9e2b 233 hdr.format = img->format;
RyoheiHagimoto 0:56c5742b9e2b 234 hdr.width = img->width;
RyoheiHagimoto 0:56c5742b9e2b 235 hdr.height = img->height;
RyoheiHagimoto 0:56c5742b9e2b 236 hdr.size = img->datalen;
RyoheiHagimoto 0:56c5742b9e2b 237
RyoheiHagimoto 0:56c5742b9e2b 238 if(fwrite(&hdr, sizeof(hdr), 1, f) != 1 ||
RyoheiHagimoto 0:56c5742b9e2b 239 fwrite(img->data, 1, img->datalen, f) != img->datalen) {
RyoheiHagimoto 0:56c5742b9e2b 240 int rc = errno;
RyoheiHagimoto 0:56c5742b9e2b 241 zprintf(1, "ERROR writing %s: %s\n", filename, strerror(rc));
RyoheiHagimoto 0:56c5742b9e2b 242 fclose(f);
RyoheiHagimoto 0:56c5742b9e2b 243 return(rc);
RyoheiHagimoto 0:56c5742b9e2b 244 }
RyoheiHagimoto 0:56c5742b9e2b 245 return(fclose(f));
RyoheiHagimoto 0:56c5742b9e2b 246 }
RyoheiHagimoto 0:56c5742b9e2b 247
RyoheiHagimoto 0:56c5742b9e2b 248 #ifdef DEBUG_SVG
RyoheiHagimoto 0:56c5742b9e2b 249 # include <png.h>
RyoheiHagimoto 0:56c5742b9e2b 250
RyoheiHagimoto 0:56c5742b9e2b 251 int zbar_image_write_png (const zbar_image_t *img,
RyoheiHagimoto 0:56c5742b9e2b 252 const char *filename)
RyoheiHagimoto 0:56c5742b9e2b 253 {
RyoheiHagimoto 0:56c5742b9e2b 254 int rc = -1;
RyoheiHagimoto 0:56c5742b9e2b 255 FILE *file = NULL;
RyoheiHagimoto 0:56c5742b9e2b 256 png_struct *png = NULL;
RyoheiHagimoto 0:56c5742b9e2b 257 png_info *info = NULL;
RyoheiHagimoto 0:56c5742b9e2b 258 const uint8_t **rows = NULL;
RyoheiHagimoto 0:56c5742b9e2b 259
RyoheiHagimoto 0:56c5742b9e2b 260 rows = malloc(img->height * sizeof(*rows));
RyoheiHagimoto 0:56c5742b9e2b 261 if(!rows)
RyoheiHagimoto 0:56c5742b9e2b 262 goto done;
RyoheiHagimoto 0:56c5742b9e2b 263
RyoheiHagimoto 0:56c5742b9e2b 264 rows[0] = img->data;
RyoheiHagimoto 0:56c5742b9e2b 265 int y;
RyoheiHagimoto 0:56c5742b9e2b 266 for(y = 1; y < img->height; y++)
RyoheiHagimoto 0:56c5742b9e2b 267 rows[y] = rows[y - 1] + img->width;
RyoheiHagimoto 0:56c5742b9e2b 268
RyoheiHagimoto 0:56c5742b9e2b 269 file = fopen(filename, "wb");
RyoheiHagimoto 0:56c5742b9e2b 270 if(!file)
RyoheiHagimoto 0:56c5742b9e2b 271 goto done;
RyoheiHagimoto 0:56c5742b9e2b 272
RyoheiHagimoto 0:56c5742b9e2b 273 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
RyoheiHagimoto 0:56c5742b9e2b 274 if(!png)
RyoheiHagimoto 0:56c5742b9e2b 275 goto done;
RyoheiHagimoto 0:56c5742b9e2b 276
RyoheiHagimoto 0:56c5742b9e2b 277 info = png_create_info_struct(png);
RyoheiHagimoto 0:56c5742b9e2b 278 if(!info)
RyoheiHagimoto 0:56c5742b9e2b 279 goto done;
RyoheiHagimoto 0:56c5742b9e2b 280
RyoheiHagimoto 0:56c5742b9e2b 281 if(setjmp(png_jmpbuf(png)))
RyoheiHagimoto 0:56c5742b9e2b 282 goto done;
RyoheiHagimoto 0:56c5742b9e2b 283
RyoheiHagimoto 0:56c5742b9e2b 284 png_init_io(png, file);
RyoheiHagimoto 0:56c5742b9e2b 285 png_set_compression_level(png, 9);
RyoheiHagimoto 0:56c5742b9e2b 286 png_set_IHDR(png, info, img->width, img->height, 8, PNG_COLOR_TYPE_GRAY,
RyoheiHagimoto 0:56c5742b9e2b 287 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
RyoheiHagimoto 0:56c5742b9e2b 288 PNG_FILTER_TYPE_DEFAULT);
RyoheiHagimoto 0:56c5742b9e2b 289
RyoheiHagimoto 0:56c5742b9e2b 290 png_set_rows(png, info, (void*)rows);
RyoheiHagimoto 0:56c5742b9e2b 291 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
RyoheiHagimoto 0:56c5742b9e2b 292
RyoheiHagimoto 0:56c5742b9e2b 293 png_write_end(png,info);
RyoheiHagimoto 0:56c5742b9e2b 294 rc = 0;
RyoheiHagimoto 0:56c5742b9e2b 295
RyoheiHagimoto 0:56c5742b9e2b 296 done:
RyoheiHagimoto 0:56c5742b9e2b 297 if(png)
RyoheiHagimoto 0:56c5742b9e2b 298 png_destroy_write_struct(&png, &info);
RyoheiHagimoto 0:56c5742b9e2b 299 if(rows)
RyoheiHagimoto 0:56c5742b9e2b 300 free(rows);
RyoheiHagimoto 0:56c5742b9e2b 301 if(file)
RyoheiHagimoto 0:56c5742b9e2b 302 fclose(file);
RyoheiHagimoto 0:56c5742b9e2b 303 return(rc);
RyoheiHagimoto 0:56c5742b9e2b 304 }
RyoheiHagimoto 0:56c5742b9e2b 305
RyoheiHagimoto 0:56c5742b9e2b 306 #endif
RyoheiHagimoto 0:56c5742b9e2b 307