pngwio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* pngwio.c - functions for data output
  2. *
  3. * Last changed in libpng 1.5.0 [January 6, 2011]
  4. * Copyright (c) 1998-2011 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 output. Users who need
  13. * special handling are expected to write functions that have the same
  14. * arguments as these and perform similar functions, but that possibly
  15. * use different output methods. Note that you shouldn't change these
  16. * functions, but rather write replacement functions and then change
  17. * them at run time with png_set_write_fn(...).
  18. */
  19. #include "pngpriv.h"
  20. #ifdef PNG_WRITE_SUPPORTED
  21. /* Write the data to whatever output you are using. The default routine
  22. * writes to a file pointer. Note that this routine sometimes gets called
  23. * with very small lengths, so you should implement some kind of simple
  24. * buffering if you are using unbuffered writes. This should never be asked
  25. * to write more than 64K on a 16 bit machine.
  26. */
  27. void /* PRIVATE */
  28. png_write_data(png_structp png_ptr, png_const_bytep data, png_size_t length)
  29. {
  30. /* NOTE: write_data_fn must not change the buffer! */
  31. if (png_ptr->write_data_fn != NULL )
  32. (*(png_ptr->write_data_fn))(png_ptr, (png_bytep)data, length);
  33. else
  34. png_error(png_ptr, "Call to NULL write function");
  35. }
  36. #ifdef PNG_STDIO_SUPPORTED
  37. /* This is the function that does the actual writing of data. If you are
  38. * not writing to a standard C stream, you should create a replacement
  39. * write_data function and use it at run time with png_set_write_fn(), rather
  40. * than changing the library.
  41. */
  42. #ifndef USE_FAR_KEYWORD
  43. void PNGCBAPI
  44. png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  45. {
  46. png_size_t check;
  47. if (png_ptr == NULL)
  48. return;
  49. check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
  50. if (check != length)
  51. png_error(png_ptr, "Write Error");
  52. }
  53. #else
  54. /* This is the model-independent version. Since the standard I/O library
  55. * can't handle far buffers in the medium and small models, we have to copy
  56. * the data.
  57. */
  58. #define NEAR_BUF_SIZE 1024
  59. #define MIN(a,b) (a <= b ? a : b)
  60. void PNGCBAPI
  61. png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  62. {
  63. png_uint_32 check;
  64. png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
  65. png_FILE_p io_ptr;
  66. if (png_ptr == NULL)
  67. return;
  68. /* Check if data really is near. If so, use usual code. */
  69. near_data = (png_byte *)CVT_PTR_NOCHECK(data);
  70. io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
  71. if ((png_bytep)near_data == data)
  72. {
  73. check = fwrite(near_data, 1, length, io_ptr);
  74. }
  75. else
  76. {
  77. png_byte buf[NEAR_BUF_SIZE];
  78. png_size_t written, remaining, err;
  79. check = 0;
  80. remaining = length;
  81. do
  82. {
  83. written = MIN(NEAR_BUF_SIZE, remaining);
  84. png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
  85. err = fwrite(buf, 1, written, io_ptr);
  86. if (err != written)
  87. break;
  88. else
  89. check += err;
  90. data += written;
  91. remaining -= written;
  92. }
  93. while (remaining != 0);
  94. }
  95. if (check != length)
  96. png_error(png_ptr, "Write Error");
  97. }
  98. #endif
  99. #endif
  100. /* This function is called to output any data pending writing (normally
  101. * to disk). After png_flush is called, there should be no data pending
  102. * writing in any buffers.
  103. */
  104. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  105. void /* PRIVATE */
  106. png_flush(png_structp png_ptr)
  107. {
  108. if (png_ptr->output_flush_fn != NULL)
  109. (*(png_ptr->output_flush_fn))(png_ptr);
  110. }
  111. # ifdef PNG_STDIO_SUPPORTED
  112. void PNGCBAPI
  113. png_default_flush(png_structp png_ptr)
  114. {
  115. png_FILE_p io_ptr;
  116. if (png_ptr == NULL)
  117. return;
  118. io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
  119. fflush(io_ptr);
  120. }
  121. # endif
  122. #endif
  123. /* This function allows the application to supply new output functions for
  124. * libpng if standard C streams aren't being used.
  125. *
  126. * This function takes as its arguments:
  127. * png_ptr - pointer to a png output data structure
  128. * io_ptr - pointer to user supplied structure containing info about
  129. * the output functions. May be NULL.
  130. * write_data_fn - pointer to a new output function that takes as its
  131. * arguments a pointer to a png_struct, a pointer to
  132. * data to be written, and a 32-bit unsigned int that is
  133. * the number of bytes to be written. The new write
  134. * function should call png_error(png_ptr, "Error msg")
  135. * to exit and output any fatal error messages. May be
  136. * NULL, in which case libpng's default function will
  137. * be used.
  138. * flush_data_fn - pointer to a new flush function that takes as its
  139. * arguments a pointer to a png_struct. After a call to
  140. * the flush function, there should be no data in any buffers
  141. * or pending transmission. If the output method doesn't do
  142. * any buffering of output, a function prototype must still be
  143. * supplied although it doesn't have to do anything. If
  144. * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
  145. * time, output_flush_fn will be ignored, although it must be
  146. * supplied for compatibility. May be NULL, in which case
  147. * libpng's default function will be used, if
  148. * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
  149. * a good idea if io_ptr does not point to a standard
  150. * *FILE structure.
  151. */
  152. void PNGAPI
  153. png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
  154. png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
  155. {
  156. if (png_ptr == NULL)
  157. return;
  158. png_ptr->io_ptr = io_ptr;
  159. #ifdef PNG_STDIO_SUPPORTED
  160. if (write_data_fn != NULL)
  161. png_ptr->write_data_fn = write_data_fn;
  162. else
  163. png_ptr->write_data_fn = png_default_write_data;
  164. #else
  165. png_ptr->write_data_fn = write_data_fn;
  166. #endif
  167. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  168. # ifdef PNG_STDIO_SUPPORTED
  169. if (output_flush_fn != NULL)
  170. png_ptr->output_flush_fn = output_flush_fn;
  171. else
  172. png_ptr->output_flush_fn = png_default_flush;
  173. # else
  174. png_ptr->output_flush_fn = output_flush_fn;
  175. # endif
  176. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  177. /* It is an error to read while writing a png file */
  178. if (png_ptr->read_data_fn != NULL)
  179. {
  180. png_ptr->read_data_fn = NULL;
  181. png_warning(png_ptr,
  182. "Can't set both read_data_fn and write_data_fn in the"
  183. " same structure");
  184. }
  185. }
  186. #ifdef USE_FAR_KEYWORD
  187. # ifdef _MSC_VER
  188. void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
  189. {
  190. void *near_ptr;
  191. void FAR *far_ptr;
  192. FP_OFF(near_ptr) = FP_OFF(ptr);
  193. far_ptr = (void FAR *)near_ptr;
  194. if (check != 0)
  195. if (FP_SEG(ptr) != FP_SEG(far_ptr))
  196. png_error(png_ptr, "segment lost in conversion");
  197. return(near_ptr);
  198. }
  199. # else
  200. void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
  201. {
  202. void *near_ptr;
  203. void FAR *far_ptr;
  204. near_ptr = (void FAR *)ptr;
  205. far_ptr = (void FAR *)near_ptr;
  206. if (check != 0)
  207. if (far_ptr != ptr)
  208. png_error(png_ptr, "segment lost in conversion");
  209. return(near_ptr);
  210. }
  211. # endif
  212. #endif
  213. #endif /* PNG_WRITE_SUPPORTED */