go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

imageio_util.c (5122B)


      1 // Copyright 2016 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 //  Utility functions used by the image decoders.
     11 //
     12 
     13 #include "./imageio_util.h"
     14 
     15 #if defined(_WIN32)
     16 #include <fcntl.h>   // for _O_BINARY
     17 #include <io.h>      // for _setmode()
     18 #endif
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 
     23 #include "webp/types.h"
     24 #include "../examples/unicode.h"
     25 
     26 // -----------------------------------------------------------------------------
     27 // File I/O
     28 
     29 FILE* ImgIoUtilSetBinaryMode(FILE* file) {
     30 #if defined(_WIN32)
     31   if (_setmode(_fileno(file), _O_BINARY) == -1) {
     32     fprintf(stderr, "Failed to reopen file in O_BINARY mode.\n");
     33     return NULL;
     34   }
     35 #endif
     36   return file;
     37 }
     38 
     39 int ImgIoUtilReadFromStdin(const uint8_t** data, size_t* data_size) {
     40   static const size_t kBlockSize = 16384;  // default initial size
     41   size_t max_size = 0;
     42   size_t size = 0;
     43   uint8_t* input = NULL;
     44 
     45   if (data == NULL || data_size == NULL) return 0;
     46   *data = NULL;
     47   *data_size = 0;
     48 
     49   if (!ImgIoUtilSetBinaryMode(stdin)) return 0;
     50 
     51   while (!feof(stdin)) {
     52     // We double the buffer size each time and read as much as possible.
     53     const size_t extra_size = (max_size == 0) ? kBlockSize : max_size;
     54     // we allocate one extra byte for the \0 terminator
     55     void* const new_data = realloc(input, max_size + extra_size + 1);
     56     if (new_data == NULL) goto Error;
     57     input = (uint8_t*)new_data;
     58     max_size += extra_size;
     59     size += fread(input + size, 1, extra_size, stdin);
     60     if (size < max_size) break;
     61   }
     62   if (ferror(stdin)) goto Error;
     63   if (input != NULL) input[size] = '\0';  // convenient 0-terminator
     64   *data = input;
     65   *data_size = size;
     66   return 1;
     67 
     68  Error:
     69   free(input);
     70   fprintf(stderr, "Could not read from stdin\n");
     71   return 0;
     72 }
     73 
     74 int ImgIoUtilReadFile(const char* const file_name,
     75                       const uint8_t** data, size_t* data_size) {
     76   int ok;
     77   uint8_t* file_data;
     78   size_t file_size;
     79   FILE* in;
     80   const int from_stdin = (file_name == NULL) || !WSTRCMP(file_name, "-");
     81 
     82   if (from_stdin) return ImgIoUtilReadFromStdin(data, data_size);
     83 
     84   if (data == NULL || data_size == NULL) return 0;
     85   *data = NULL;
     86   *data_size = 0;
     87 
     88   in = WFOPEN(file_name, "rb");
     89   if (in == NULL) {
     90     WFPRINTF(stderr, "cannot open input file '%s'\n", (const W_CHAR*)file_name);
     91     return 0;
     92   }
     93   fseek(in, 0, SEEK_END);
     94   file_size = ftell(in);
     95   if (file_size == (size_t)-1) {
     96     fclose(in);
     97     WFPRINTF(stderr, "error getting size of '%s'\n", (const W_CHAR*)file_name);
     98     return 0;
     99   }
    100   fseek(in, 0, SEEK_SET);
    101   // we allocate one extra byte for the \0 terminator
    102   file_data = (uint8_t*)WebPMalloc(file_size + 1);
    103   if (file_data == NULL) {
    104     fclose(in);
    105     WFPRINTF(stderr, "memory allocation failure when reading file %s\n",
    106              (const W_CHAR*)file_name);
    107     return 0;
    108   }
    109   ok = (fread(file_data, file_size, 1, in) == 1);
    110   fclose(in);
    111 
    112   if (!ok) {
    113     WFPRINTF(stderr, "Could not read %d bytes of data from file %s\n",
    114              (int)file_size, (const W_CHAR*)file_name);
    115     WebPFree(file_data);
    116     return 0;
    117   }
    118   file_data[file_size] = '\0';  // convenient 0-terminator
    119   *data = file_data;
    120   *data_size = file_size;
    121   return 1;
    122 }
    123 
    124 // -----------------------------------------------------------------------------
    125 
    126 int ImgIoUtilWriteFile(const char* const file_name,
    127                        const uint8_t* data, size_t data_size) {
    128   int ok;
    129   FILE* out;
    130   const int to_stdout = (file_name == NULL) || !WSTRCMP(file_name, "-");
    131 
    132   if (data == NULL) {
    133     return 0;
    134   }
    135   out = to_stdout ? ImgIoUtilSetBinaryMode(stdout) : WFOPEN(file_name, "wb");
    136   if (out == NULL) {
    137     WFPRINTF(stderr, "Error! Cannot open output file '%s'\n",
    138              (const W_CHAR*)file_name);
    139     return 0;
    140   }
    141   ok = (fwrite(data, data_size, 1, out) == 1);
    142   if (out != stdout) fclose(out);
    143   return ok;
    144 }
    145 
    146 // -----------------------------------------------------------------------------
    147 
    148 void ImgIoUtilCopyPlane(const uint8_t* src, int src_stride,
    149                         uint8_t* dst, int dst_stride, int width, int height) {
    150   while (height-- > 0) {
    151     memcpy(dst, src, width * sizeof(*dst));
    152     src += src_stride;
    153     dst += dst_stride;
    154   }
    155 }
    156 
    157 // -----------------------------------------------------------------------------
    158 
    159 int ImgIoUtilCheckSizeArgumentsOverflow(uint64_t stride, size_t height) {
    160   const uint64_t total_size = stride * height;
    161   int ok = (total_size == (size_t)total_size);
    162   // check that 'stride' is representable as int:
    163   ok = ok && ((uint64_t)(int)stride == stride);
    164 #if defined(WEBP_MAX_IMAGE_SIZE)
    165   ok = ok && (total_size <= (uint64_t)WEBP_MAX_IMAGE_SIZE);
    166 #endif
    167   return ok;
    168 }
    169 
    170 // -----------------------------------------------------------------------------