]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libjpeg.c
Conf: Rewritten configuration parser to support multiple contexts (step 1)
[libucw.git] / images / io-libjpeg.c
index a3120dfb0dcf987b8c4b7b74d565fba2807cda11..74af24acccc1d95fceba24b91554ac35be29b1ec 100644 (file)
@@ -9,13 +9,13 @@
 
 #undef LOCAL_DEBUG
 
-#include "lib/lib.h"
-#include "lib/mempool.h"
-#include "lib/fastbuf.h"
-#include "images/images.h"
-#include "images/error.h"
-#include "images/color.h"
-#include "images/io-main.h"
+#include <ucw/lib.h>
+#include <ucw/mempool.h>
+#include <ucw/fastbuf.h>
+#include <images/images.h>
+#include <images/error.h>
+#include <images/color.h>
+#include <images/io-main.h>
 
 #include <stdio.h>
 #include <sys/types.h>
@@ -75,15 +75,32 @@ libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
   cinfo->err->format_message(cinfo, buf);
   DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf);
 #endif
+#if 0
+  // Terminate on warning?
   if (unlikely(msg_level == -1))
-    longjmp(((struct libjpeg_err *)(cinfo)->err)->setjmp_buf, 1);
+    {
+      struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
+      byte buf[JMSG_LENGTH_MAX];
+      cinfo->err->format_message(cinfo, buf);
+      IMAGE_ERROR(e->io->context, 0, "libjpeg: %s", buf);
+      longjmp(e->setjmp_buf, 1);
+    }
+#endif
 }
 
 static inline uns
 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
 {
+  DBG("libjpeg_fb_read_prepare()");
   byte *start;
   uns len = bdirect_read_prepare(i->fastbuf, &start);
+  DBG("readed %u bytes at %p", len, start);
+  if (!len)
+    {
+      // XXX: maybe only generate a warning and return EOI markers to recover from such errors (also in skip_input_data)
+      IMAGE_ERROR(i->err.io->context, IMAGE_ERROR_READ_FAILED, "Incomplete JPEG file");
+      longjmp(i->err.setjmp_buf, 1);
+    }
   i->fastbuf_pos = start + len;
   i->src.next_input_byte = start;
   i->src.bytes_in_buffer = len;
@@ -93,6 +110,7 @@ libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
 static inline void
 libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i)
 {
+  DBG("libjpeg_fb_read_commit()");
   bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
 }
 
@@ -116,7 +134,8 @@ libjpeg_fill_input_buffer(j_decompress_ptr cinfo)
   DBG("libjpeg_fill_input_buffer()");
   struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
   libjpeg_fastbuf_read_commit(i);
-  return !!libjpeg_fastbuf_read_prepare(i);
+  libjpeg_fastbuf_read_prepare(i);
+  return 1;
 }
 
 static void
@@ -339,38 +358,29 @@ libjpeg_read_data(struct image_io *io)
   uns read_flags = io->flags;
 
   /* Select color space */
-  switch (read_flags & IMAGE_COLOR_SPACE)
+  switch (i->cinfo.jpeg_color_space)
     {
-      case COLOR_SPACE_GRAYSCALE:
-       i->cinfo.out_color_space = JCS_GRAYSCALE;
-       break;
-      case COLOR_SPACE_YCBCR:
+      case JCS_GRAYSCALE:
+        read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCBCR; 
+        i->cinfo.out_color_space = JCS_YCbCr;
+        break;
+      case JCS_YCbCr:
+       read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCBCR; 
        i->cinfo.out_color_space = JCS_YCbCr;
        break;
-      case COLOR_SPACE_CMYK:
+      case JCS_CMYK:
+       read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_CMYK; 
        i->cinfo.out_color_space = JCS_CMYK;
        break;
-      case COLOR_SPACE_YCCK:
+      case JCS_YCCK:
+       read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCCK; 
        i->cinfo.out_color_space = JCS_YCCK;
        break;
       default:
-       switch (i->cinfo.jpeg_color_space)
-         {
-           case JCS_CMYK:
-             read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_CMYK; 
-             i->cinfo.out_color_space = JCS_CMYK;
-             break;
-           case JCS_YCCK:
-             read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_YCCK; 
-             i->cinfo.out_color_space = JCS_YCCK;
-             break;
-           default:
-             read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; 
-             i->cinfo.out_color_space = JCS_RGB;
-             break;
-         }
+       read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB; 
+       i->cinfo.out_color_space = JCS_RGB;
        break;
-    }
+  }
 
   /* Prepare the image  */
   struct image_io_read_data_internals rdi;
@@ -436,7 +446,7 @@ libjpeg_read_data(struct image_io *io)
 #             define IMAGE_WALK_COL_STEP 2
 #             define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
 #             define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
-#             include "images/image-walk.h"
+#             include <images/image-walk.h>
            }
            break;
          case 4: /* * -> *+Alpha or aligned * */
@@ -450,7 +460,7 @@ libjpeg_read_data(struct image_io *io)
 #             define IMAGE_WALK_COL_STEP 4
 #             define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
 #             define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = *(u32 *)src; walk_pos[3] = 255; src += 3; }while(0)
-#             include "images/image-walk.h"
+#             include <images/image-walk.h>
            }
            break;
          default:
@@ -567,7 +577,7 @@ libjpeg_write(struct image_io *io)
 #             define IMAGE_WALK_COL_STEP 2
 #             define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
 #             define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
-#             include "images/image-walk.h"
+#             include <images/image-walk.h>
              break;
            }
          case 4: /* *+Alpha or aligned * -> * */
@@ -581,7 +591,7 @@ libjpeg_write(struct image_io *io)
 #             define IMAGE_WALK_COL_STEP 4
 #             define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
 #             define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0)
-#             include "images/image-walk.h"
+#             include <images/image-walk.h>
              break;
            }
          default: