added libimago
[eradicate] / libs / imago / libpng / pngwio.c
diff --git a/libs/imago/libpng/pngwio.c b/libs/imago/libpng/pngwio.c
new file mode 100644 (file)
index 0000000..d79b955
--- /dev/null
@@ -0,0 +1,234 @@
+\r
+/* pngwio.c - functions for data output\r
+ *\r
+ * Last changed in libpng 1.2.30 [August 15, 2008]\r
+ * For conditions of distribution and use, see copyright notice in png.h\r
+ * Copyright (c) 1998-2008 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This file provides a location for all output.  Users who need\r
+ * special handling are expected to write functions that have the same\r
+ * arguments as these and perform similar functions, but that possibly\r
+ * use different output methods.  Note that you shouldn't change these\r
+ * functions, but rather write replacement functions and then change\r
+ * them at run time with png_set_write_fn(...).\r
+ */\r
+\r
+#define PNG_INTERNAL\r
+#include "png.h"\r
+#ifdef PNG_WRITE_SUPPORTED\r
+\r
+/* Write the data to whatever output you are using.  The default routine\r
+   writes to a file pointer.  Note that this routine sometimes gets called\r
+   with very small lengths, so you should implement some kind of simple\r
+   buffering if you are using unbuffered writes.  This should never be asked\r
+   to write more than 64K on a 16 bit machine.  */\r
+\r
+void /* PRIVATE */\r
+png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   if (png_ptr->write_data_fn != NULL )\r
+      (*(png_ptr->write_data_fn))(png_ptr, data, length);\r
+   else\r
+      png_error(png_ptr, "Call to NULL write function");\r
+}\r
+\r
+#if !defined(PNG_NO_STDIO)\r
+/* This is the function that does the actual writing of data.  If you are\r
+   not writing to a standard C stream, you should create a replacement\r
+   write_data function and use it at run time with png_set_write_fn(), rather\r
+   than changing the library. */\r
+#ifndef USE_FAR_KEYWORD\r
+void PNGAPI\r
+png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_uint_32 check;\r
+\r
+   if (png_ptr == NULL) return;\r
+#if defined(_WIN32_WCE)\r
+   if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )\r
+      check = 0;\r
+#else\r
+   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));\r
+#endif\r
+   if (check != length)\r
+      png_error(png_ptr, "Write Error");\r
+}\r
+#else\r
+/* this is the model-independent version. Since the standard I/O library\r
+   can't handle far buffers in the medium and small models, we have to copy\r
+   the data.\r
+*/\r
+\r
+#define NEAR_BUF_SIZE 1024\r
+#define MIN(a,b) (a <= b ? a : b)\r
+\r
+void PNGAPI\r
+png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_uint_32 check;\r
+   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */\r
+   png_FILE_p io_ptr;\r
+\r
+   if (png_ptr == NULL) return;\r
+   /* Check if data really is near. If so, use usual code. */\r
+   near_data = (png_byte *)CVT_PTR_NOCHECK(data);\r
+   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);\r
+   if ((png_bytep)near_data == data)\r
+   {\r
+#if defined(_WIN32_WCE)\r
+      if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )\r
+         check = 0;\r
+#else\r
+      check = fwrite(near_data, 1, length, io_ptr);\r
+#endif\r
+   }\r
+   else\r
+   {\r
+      png_byte buf[NEAR_BUF_SIZE];\r
+      png_size_t written, remaining, err;\r
+      check = 0;\r
+      remaining = length;\r
+      do\r
+      {\r
+         written = MIN(NEAR_BUF_SIZE, remaining);\r
+         png_memcpy(buf, data, written); /* copy far buffer to near buffer */\r
+#if defined(_WIN32_WCE)\r
+         if ( !WriteFile(io_ptr, buf, written, &err, NULL) )\r
+            err = 0;\r
+#else\r
+         err = fwrite(buf, 1, written, io_ptr);\r
+#endif\r
+         if (err != written)\r
+            break;\r
+         else\r
+            check += err;\r
+         data += written;\r
+         remaining -= written;\r
+      }\r
+      while (remaining != 0);\r
+   }\r
+   if (check != length)\r
+      png_error(png_ptr, "Write Error");\r
+}\r
+\r
+#endif\r
+#endif\r
+\r
+/* This function is called to output any data pending writing (normally\r
+   to disk).  After png_flush is called, there should be no data pending\r
+   writing in any buffers. */\r
+#if defined(PNG_WRITE_FLUSH_SUPPORTED)\r
+void /* PRIVATE */\r
+png_flush(png_structp png_ptr)\r
+{\r
+   if (png_ptr->output_flush_fn != NULL)\r
+      (*(png_ptr->output_flush_fn))(png_ptr);\r
+}\r
+\r
+#if !defined(PNG_NO_STDIO)\r
+void PNGAPI\r
+png_default_flush(png_structp png_ptr)\r
+{\r
+#if !defined(_WIN32_WCE)\r
+   png_FILE_p io_ptr;\r
+#endif\r
+   if (png_ptr == NULL) return;\r
+#if !defined(_WIN32_WCE)\r
+   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));\r
+   if (io_ptr != NULL)\r
+      fflush(io_ptr);\r
+#endif\r
+}\r
+#endif\r
+#endif\r
+\r
+/* This function allows the application to supply new output functions for\r
+   libpng if standard C streams aren't being used.\r
+\r
+   This function takes as its arguments:\r
+   png_ptr       - pointer to a png output data structure\r
+   io_ptr        - pointer to user supplied structure containing info about\r
+                   the output functions.  May be NULL.\r
+   write_data_fn - pointer to a new output function that takes as its\r
+                   arguments a pointer to a png_struct, a pointer to\r
+                   data to be written, and a 32-bit unsigned int that is\r
+                   the number of bytes to be written.  The new write\r
+                   function should call png_error(png_ptr, "Error msg")\r
+                   to exit and output any fatal error messages.\r
+   flush_data_fn - pointer to a new flush function that takes as its\r
+                   arguments a pointer to a png_struct.  After a call to\r
+                   the flush function, there should be no data in any buffers\r
+                   or pending transmission.  If the output method doesn't do\r
+                   any buffering of ouput, a function prototype must still be\r
+                   supplied although it doesn't have to do anything.  If\r
+                   PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile\r
+                   time, output_flush_fn will be ignored, although it must be\r
+                   supplied for compatibility. */\r
+void PNGAPI\r
+png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,\r
+   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)\r
+{\r
+   if (png_ptr == NULL) return;\r
+   png_ptr->io_ptr = io_ptr;\r
+\r
+#if !defined(PNG_NO_STDIO)\r
+   if (write_data_fn != NULL)\r
+      png_ptr->write_data_fn = write_data_fn;\r
+   else\r
+      png_ptr->write_data_fn = png_default_write_data;\r
+#else\r
+   png_ptr->write_data_fn = write_data_fn;\r
+#endif\r
+\r
+#if defined(PNG_WRITE_FLUSH_SUPPORTED)\r
+#if !defined(PNG_NO_STDIO)\r
+   if (output_flush_fn != NULL)\r
+      png_ptr->output_flush_fn = output_flush_fn;\r
+   else\r
+      png_ptr->output_flush_fn = png_default_flush;\r
+#else\r
+   png_ptr->output_flush_fn = output_flush_fn;\r
+#endif\r
+#endif /* PNG_WRITE_FLUSH_SUPPORTED */\r
+\r
+   /* It is an error to read while writing a png file */\r
+   if (png_ptr->read_data_fn != NULL)\r
+   {\r
+      png_ptr->read_data_fn = NULL;\r
+      png_warning(png_ptr,\r
+         "Attempted to set both read_data_fn and write_data_fn in");\r
+      png_warning(png_ptr,\r
+         "the same structure.  Resetting read_data_fn to NULL.");\r
+   }\r
+}\r
+\r
+#if defined(USE_FAR_KEYWORD)\r
+#if defined(_MSC_VER)\r
+void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)\r
+{\r
+   void *near_ptr;\r
+   void FAR *far_ptr;\r
+   FP_OFF(near_ptr) = FP_OFF(ptr);\r
+   far_ptr = (void FAR *)near_ptr;\r
+   if (check != 0)\r
+      if (FP_SEG(ptr) != FP_SEG(far_ptr))\r
+         png_error(png_ptr, "segment lost in conversion");\r
+   return(near_ptr);\r
+}\r
+#  else\r
+void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)\r
+{\r
+   void *near_ptr;\r
+   void FAR *far_ptr;\r
+   near_ptr = (void FAR *)ptr;\r
+   far_ptr = (void FAR *)near_ptr;\r
+   if (check != 0)\r
+      if (far_ptr != ptr)\r
+         png_error(png_ptr, "segment lost in conversion");\r
+   return(near_ptr);\r
+}\r
+#   endif\r
+#   endif\r
+#endif /* PNG_WRITE_SUPPORTED */\r