/src/compiler/android/jni/ftk/pngrio.c

http://ftk.googlecode.com/ · C · 205 lines · 134 code · 17 blank · 54 comment · 29 complexity · 60a0278d9e96d439e142c023301aa920 MD5 · raw file

  1. /* pngrio.c - functions for data input
  2. *
  3. * Last changed in libpng 1.2.43 [February 25, 2010]
  4. * Copyright (c) 1998-2010 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file provides a location for all input. Users who need
  13. * special handling are expected to write a function that has the same
  14. * arguments as this and performs a similar function, but that possibly
  15. * has a different input method. Note that you shouldn't change this
  16. * function, but rather write a replacement function and then make
  17. * libpng use it at run time with png_set_read_fn(...).
  18. */
  19. #define PNG_INTERNAL
  20. #define PNG_NO_PEDANTIC_WARNINGS
  21. #include "png.h"
  22. #ifdef PNG_READ_SUPPORTED
  23. /* Read the data from whatever input you are using. The default routine
  24. * reads from a file pointer. Note that this routine sometimes gets called
  25. * with very small lengths, so you should implement some kind of simple
  26. * buffering if you are using unbuffered reads. This should never be asked
  27. * to read more then 64K on a 16 bit machine.
  28. */
  29. void /* PRIVATE */
  30. png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  31. {
  32. png_debug1(4, "reading %d bytes", (int)length);
  33. if (png_ptr->read_data_fn != NULL)
  34. (*(png_ptr->read_data_fn))(png_ptr, data, length);
  35. else
  36. png_error(png_ptr, "Call to NULL read function");
  37. #ifdef PNG_INDEX_SUPPORTED
  38. png_ptr->total_data_read += length;
  39. #endif
  40. }
  41. #ifdef PNG_INDEX_SUPPORTED
  42. void /* PRIVATE */
  43. png_seek_data(png_structp png_ptr, png_uint_32 offset)
  44. {
  45. if (png_ptr->seek_data_fn != NULL)
  46. (*(png_ptr->seek_data_fn))(png_ptr, offset);
  47. else
  48. png_error(png_ptr, "Call to NULL seek function");
  49. }
  50. #endif
  51. #ifdef PNG_STDIO_SUPPORTED
  52. /* This is the function that does the actual reading of data. If you are
  53. * not reading from a standard C stream, you should create a replacement
  54. * read_data function and use it at run time with png_set_read_fn(), rather
  55. * than changing the library.
  56. */
  57. #ifndef USE_FAR_KEYWORD
  58. void PNGAPI
  59. png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  60. {
  61. png_size_t check;
  62. if (png_ptr == NULL)
  63. return;
  64. /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  65. * instead of an int, which is what fread() actually returns.
  66. */
  67. #ifdef _WIN32_WCE
  68. if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
  69. check = 0;
  70. #else
  71. check = (png_size_t)fread(data, (png_size_t)1, length,
  72. (png_FILE_p)png_ptr->io_ptr);
  73. #endif
  74. if (check != length)
  75. png_error(png_ptr, "Read Error");
  76. }
  77. #else
  78. /* This is the model-independent version. Since the standard I/O library
  79. can't handle far buffers in the medium and small models, we have to copy
  80. the data.
  81. */
  82. #define NEAR_BUF_SIZE 1024
  83. #define MIN(a,b) (a <= b ? a : b)
  84. static void PNGAPI
  85. png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  86. {
  87. int check;
  88. png_byte *n_data;
  89. png_FILE_p io_ptr;
  90. if (png_ptr == NULL)
  91. return;
  92. /* Check if data really is near. If so, use usual code. */
  93. n_data = (png_byte *)CVT_PTR_NOCHECK(data);
  94. io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
  95. if ((png_bytep)n_data == data)
  96. {
  97. #ifdef _WIN32_WCE
  98. if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check,
  99. NULL) )
  100. check = 0;
  101. #else
  102. check = fread(n_data, 1, length, io_ptr);
  103. #endif
  104. }
  105. else
  106. {
  107. png_byte buf[NEAR_BUF_SIZE];
  108. png_size_t read, remaining, err;
  109. check = 0;
  110. remaining = length;
  111. do
  112. {
  113. read = MIN(NEAR_BUF_SIZE, remaining);
  114. #ifdef _WIN32_WCE
  115. if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
  116. err = 0;
  117. #else
  118. err = fread(buf, (png_size_t)1, read, io_ptr);
  119. #endif
  120. png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  121. if (err != read)
  122. break;
  123. else
  124. check += err;
  125. data += read;
  126. remaining -= read;
  127. }
  128. while (remaining != 0);
  129. }
  130. if ((png_uint_32)check != (png_uint_32)length)
  131. png_error(png_ptr, "read Error");
  132. }
  133. #endif
  134. #endif
  135. /* This function allows the application to supply a new input function
  136. * for libpng if standard C streams aren't being used.
  137. *
  138. * This function takes as its arguments:
  139. * png_ptr - pointer to a png input data structure
  140. * io_ptr - pointer to user supplied structure containing info about
  141. * the input functions. May be NULL.
  142. * read_data_fn - pointer to a new input function that takes as its
  143. * arguments a pointer to a png_struct, a pointer to
  144. * a location where input data can be stored, and a 32-bit
  145. * unsigned int that is the number of bytes to be read.
  146. * To exit and output any fatal error messages the new write
  147. * function should call png_error(png_ptr, "Error msg").
  148. * May be NULL, in which case libpng's default function will
  149. * be used.
  150. */
  151. void PNGAPI
  152. png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  153. png_rw_ptr read_data_fn)
  154. {
  155. if (png_ptr == NULL)
  156. return;
  157. png_ptr->io_ptr = io_ptr;
  158. #ifdef PNG_STDIO_SUPPORTED
  159. if (read_data_fn != NULL)
  160. png_ptr->read_data_fn = read_data_fn;
  161. else
  162. png_ptr->read_data_fn = png_default_read_data;
  163. #else
  164. png_ptr->read_data_fn = read_data_fn;
  165. #endif
  166. /* It is an error to write to a read device */
  167. if (png_ptr->write_data_fn != NULL)
  168. {
  169. png_ptr->write_data_fn = NULL;
  170. png_warning(png_ptr,
  171. "It's an error to set both read_data_fn and write_data_fn in the ");
  172. png_warning(png_ptr,
  173. "same structure. Resetting write_data_fn to NULL.");
  174. }
  175. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  176. png_ptr->output_flush_fn = NULL;
  177. #endif
  178. }
  179. #ifdef PNG_INDEX_SUPPORTED
  180. void PNGAPI
  181. png_set_seek_fn(png_structp png_ptr, png_seek_ptr seek_data_fn)
  182. {
  183. if (png_ptr == NULL)
  184. return;
  185. png_ptr->seek_data_fn = seek_data_fn;
  186. }
  187. #endif
  188. #endif /* PNG_READ_SUPPORTED */