diff bpgenc.c @ 34:5d51fff843eb default tip

A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 08 Mar 2020 19:18:48 +0200
parents 33594243ce31
children
line wrap: on
line diff
--- a/bpgenc.c	Sun Mar 08 19:14:25 2020 +0200
+++ b/bpgenc.c	Sun Mar 08 19:18:48 2020 +0200
@@ -693,10 +693,12 @@
                    BPGColorSpaceEnum color_space, int bit_depth)
 {
     Image *img;
-    int i, linesize, w1, h1, c_count;
-
-    img = malloc(sizeof(Image));
-    memset(img, 0, sizeof(*img));
+    int i, c_count;
+
+    if ((img = malloc(sizeof(Image))) == NULL)
+        return NULL;
+
+    memset(img, 0, sizeof(Image));
 
     img->w = w;
     img->h = h;
@@ -716,6 +718,8 @@
         c_count++;
 
     for(i = 0; i < c_count; i++) {
+        int w1, h1, linesize;
+
         get_plane_res(img, &w1, &h1, i);
 
         /* multiple of 16 pixels to add borders */
@@ -938,6 +942,26 @@
     if (buf == NULL || len == 0)
         return -2;
 
+#ifdef TIFF_DEBUG
+    char *filename = NULL, *mtype = NULL;
+    FILE *fh;
+
+    switch (tag)
+    {
+        case BPG_EXTENSION_TAG_XMP: filename = "paska.xmp"; mtype = "XMP"; break;
+        case BPG_EXTENSION_TAG_EXIF: filename = "paska.exif"; mtype = "EXIF"; break;
+        case BPG_EXTENSION_TAG_ICCP: filename = "paska.iccp"; mtype = "ICCP"; break;
+        default: mtype = "???";
+    }
+    printf("bpg_add_md_contents(%p, %s (%d), %d, %p)\n", list, mtype, tag, len, buf);
+    if (filename != NULL && (fh = fopen(filename, "wb")) != NULL)
+    {
+        printf("bpg_add_md_contents() writing to file '%s' ..\n", filename);
+        fwrite(buf, len, 1, fh);
+        fclose(fh);
+    }
+#endif
+
     if ((md = bpg_md_alloc(tag)) == NULL)
         goto err;
 
@@ -987,8 +1011,13 @@
     { EXIFTAG_FNUMBER                     , "FNumber"                  , EXIF_IFD_EXIF   },
     { EXIFTAG_EXPOSUREPROGRAM             , "ExposureProgram"          , EXIF_IFD_EXIF   },
     { EXIFTAG_SPECTRALSENSITIVITY         , "Spectralsensitivity"      , EXIF_IFD_EXIF   },
+//  { EXIFTAG_ISOSPEEDRATINGS             , "ISOSpeed"                 , EXIF_IFD_EXIF   },
+//  { EXIFTAG_OECF                        , "OptoElectricConversion"   , EXIF_IFD_EXIF   },
+//  { EXIFTAG_EXIFVERSION                 , "ExifVersion"              , EXIF_IFD_EXIF   },
     { EXIFTAG_DATETIMEORIGINAL            , "DateTimeOriginal"         , EXIF_IFD_EXIF   },
     { EXIFTAG_DATETIMEDIGITIZED           , "DateTimeDigitized"        , EXIF_IFD_EXIF   },
+//  { EXIFTAG_COMPONENTSCONFIGURATION     , "ComponentsConfiguration"  , EXIF_IFD_EXIF   },
+//  { EXIFTAG_COMPRESSEDBITSPERPIXEL      , "ImageCompression"         , EXIF_IFD_EXIF   },
     { EXIFTAG_SHUTTERSPEEDVALUE           , "ShutterSpeed"             , EXIF_IFD_EXIF   },
     { EXIFTAG_APERTUREVALUE               , "Aperture"                 , EXIF_IFD_EXIF   },
     { EXIFTAG_BRIGHTNESSVALUE             , "Brightness"               , EXIF_IFD_EXIF   },
@@ -1001,11 +1030,13 @@
     { EXIFTAG_FLASH                       , "Flash"                    , EXIF_IFD_EXIF   },
     { EXIFTAG_FOCALLENGTH                 , "FocalLength"              , EXIF_IFD_EXIF   },
     { EXIFTAG_SUBJECTAREA                 , "SubjectArea"              , EXIF_IFD_EXIF   },
+//    { EXIFTAG_MAKERNOTE                   , "MakerNote"                , EXIF_IFD_EXIF   },
     { EXIFTAG_USERCOMMENT                 , "UserComment"              , EXIF_IFD_EXIF   },
     { EXIFTAG_SUBSECTIME                  , "SubSecTime"               , EXIF_IFD_EXIF   },
     { EXIFTAG_SUBSECTIMEORIGINAL          , "SubSecTimeOriginal"       , EXIF_IFD_EXIF   },
     { EXIFTAG_SUBSECTIMEDIGITIZED         , "SubSecTimeDigitized"      , EXIF_IFD_EXIF   },
 
+//    { EXIFTAG_FLASHPIXVERSION             , "FlashpixVersion"          , EXIF_IFD_EXIF   },
     { EXIFTAG_COLORSPACE                  , "Colorspace"               , EXIF_IFD_EXIF   },
     { EXIFTAG_PIXELXDIMENSION             , "Validimage"               , EXIF_IFD_EXIF   },
     { EXIFTAG_PIXELYDIMENSION             , "Validimage"               , EXIF_IFD_EXIF   },
@@ -1020,6 +1051,7 @@
     { EXIFTAG_SENSINGMETHOD               , "SensingMethod"            , EXIF_IFD_EXIF   },
     { EXIFTAG_FILESOURCE                  , "FileSource"               , EXIF_IFD_EXIF   },
     { EXIFTAG_SCENETYPE                   , "SceneType"                , EXIF_IFD_EXIF   },
+//    { EXIFTAG_CFAPATTERN                  , "CFApattern"               , EXIF_IFD_EXIF   },
     { EXIFTAG_CUSTOMRENDERED              , "CustomRendered"           , EXIF_IFD_EXIF   },
     { EXIFTAG_EXPOSUREMODE                , "ExposureMode"             , EXIF_IFD_EXIF   },
     { EXIFTAG_WHITEBALANCE                , "WhiteBalance"             , EXIF_IFD_EXIF   },
@@ -1097,6 +1129,49 @@
 #endif
 
 
+#ifdef TIFF_DEBUG
+static void tiff_tags_dump(TIFF *tif, const TIFFTagConvertInfo *table)
+{
+    const TIFFTagConvertInfo *conv;
+
+    for (conv = table; conv->tag != 0 && conv->name != NULL; conv++)
+    {
+        const TIFFField *tfd = TIFFFieldWithTag(tif, conv->tag);
+        TIFFDataType type = TIFFFieldDataType(tfd);
+        uint16_t tmp_short;
+        float tmp_rat;
+        char *tmp_str, *name = "?";
+        char tmp_buf[128] = "-";
+        int have;
+
+        switch (type)
+        {
+            case TIFF_SHORT:
+                name = "short";
+                if ((have = TIFFGetField(tif, conv->tag, &tmp_short)))
+                    snprintf(tmp_buf, sizeof(tmp_buf), "%d", tmp_short);
+                break;
+            case TIFF_RATIONAL:
+                name = "rational";
+                if ((have = TIFFGetField(tif, conv->tag, &tmp_rat)))
+                    snprintf(tmp_buf, sizeof(tmp_buf), "%1.2f", tmp_rat);
+                break;
+            case TIFF_ASCII:
+                name = "ascii";
+                if ((have = (TIFFGetField(tif, conv->tag, &tmp_str) && tmp_str != NULL)))
+                    snprintf(tmp_buf, sizeof(tmp_buf), "'%s'", tmp_str);
+                break;
+
+            default:
+                have = 0;
+        }
+        if (have)
+            printf("%-20s [%-10s]: %s\n", conv->name, name, tmp_buf);
+    }
+}
+#endif
+
+
 void tiff_warning_handler(const char *module, const char *fmt, va_list ap)
 {
     (void) module;
@@ -1155,6 +1230,7 @@
     switch (img_pmetric)
     {
         case PHOTOMETRIC_MINISBLACK:
+        case PHOTOMETRIC_MINISWHITE:
             img_format = BPG_FORMAT_GRAY;
             color_space = BPG_CS_YCbCr;
             if (img_spp != 1)
@@ -1198,16 +1274,37 @@
 
     img_linesize = TIFFScanlineSize(tif);
 
+#ifdef TIFF_DEBUG
+    int img_bpp = sizeof(uint32_t) * img_depth / 8;
+    printf(
+        "\nTIFF '%s'\n"
+        "    %d x %d, spp=%d, bpp=%d, depth=%d, pmet=%d\n"
+        "    pconfig=%d, line_size=%d, cbps=%d\n",
+        filename,
+        img_width, img_height, img_spp, img_bpp, img_depth, img_pmetric,
+        img_pconfig, img_linesize, img_width * img_bpp);
+#endif
+
     if (img_spp > 3)
     {
         uint16_t img_esmp, *img_esmp_types;
 
         TIFFGetField(tif, TIFFTAG_EXTRASAMPLES, &img_esmp, &img_esmp_types);
         img_alpha = img_esmp == 1 && img_esmp_types[0] == EXTRASAMPLE_ASSOCALPHA;
+#ifdef TIFF_DEBUG
+        printf("    esmp=%d: ", img_esmp);
+        for (int n = 0; n < img_esmp; n++)
+            printf("#%d=%d%s", n + 1, img_esmp_types[n], (n < img_esmp - 1) ? ", " : "");
+        printf("\n");
+#endif
     }
     else
         img_alpha = 0;
 
+#ifdef TIFF_DEBUG
+    printf("    alpha: %s\n\n", img_alpha ? "yes" : "no");
+#endif
+
     // Allocate temporary image space
     buf = (uint8_t *) _TIFFmalloc(img_linesize * img_spp);
     if (buf == NULL)
@@ -1242,6 +1339,11 @@
     for (y = 0; y < img->h; y++)
     {
         // Read TIFF image to raster
+/*
+        for (int n = 0; n < img_spp; n++)
+            if (!TIFFReadScanline(tif, buf + (img_linesize * n), y, n))
+                goto err;
+*/
         if (!TIFFReadScanline(tif, buf, y, 0))
             goto err;
 
@@ -1286,6 +1388,7 @@
                 }
                 break;
 
+            case PHOTOMETRIC_MINISWHITE:
             case PHOTOMETRIC_MINISBLACK:
                 if (img_depth == 16)
                 {
@@ -1339,11 +1442,17 @@
     exif_data_set_byte_order(exif, EXIF_BYTE_ORDER_INTEL);
 
     tiff_tags_convert(tif, tiff_tag_convert_table, exif_mem, exif, &added);
+#ifdef TIFF_DEBUG
+    tiff_tags_dump(tif, tiff_tag_convert_table);
+#endif
 
     if (TIFFGetField(tif, TIFFTAG_EXIFIFD, &tmp_offs) &&
         TIFFReadEXIFDirectory(tif, tmp_offs))
     {
         tiff_tags_convert(tif, exif_tag_convert_table, exif_mem, exif, &added);
+#ifdef TIFF_DEBUG
+        tiff_tags_dump(tif, exif_tag_convert_table);
+#endif
     }
 
     if (added)
@@ -1378,7 +1487,6 @@
     return img;
 }
 
-
 Image *read_png(BPGMetaData **pmd,
                 FILE *f, BPGColorSpaceEnum color_space, int out_bit_depth,
                 int limited_range, int premultiplied_alpha)