changeset 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
files Makefile bpgdec.c bpgenc.c x265_glue.c
diffstat 4 files changed, 172 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Mar 08 19:14:25 2020 +0200
+++ b/Makefile	Sun Mar 08 19:18:48 2020 +0200
@@ -59,7 +59,7 @@
 LIBTIFF_CFLAGS:=$(shell pkg-config --cflags libtiff-4)
 LIBTIFF_LDFLAGS:=$(shell pkg-config --libs libtiff-4)
 
-CFLAGS:=-Os -Wall -MMD -fno-asynchronous-unwind-tables -fdata-sections -ffunction-sections -fno-math-errno -fno-signed-zeros -fno-tree-vectorize -fomit-frame-pointer
+CFLAGS:=-O3 -W -Wall -MMD -fno-asynchronous-unwind-tables -fdata-sections -ffunction-sections -fno-math-errno -fno-signed-zeros -fno-tree-vectorize -fomit-frame-pointer
 CFLAGS+=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_REENTRANT
 CFLAGS+=-I. $(LIBSDL_CFLAGS) $(LIBPNG_CFLAGS) $(LIBJPEG_CFLAGS) $(LIBTIFF_CFLAGS) $(LIBEXIF_CFLAGS)
 CFLAGS+=-DCONFIG_BPG_VERSION=\"$(shell cat VERSION)\"
@@ -200,13 +200,13 @@
 endif # !CONFIG_APPLE 
 
 LIBS+=-lm -lpthread
-BPGDEC_LIBS:=$(LIBPNG_LDFLAGS) $(LIBS)
+BPGDEC_LIBS:=$(LIBPNG_LDFLAGS) $(LIBEXIF_LDFLAGS) $(LIBS)
 BPGENC_LIBS+=$(LIBPNG_LDFLAGS) $(LIBJPEG_LDFLAGS) $(LIBTIFF_LDFLAGS) $(LIBEXIF_LDFLAGS) $(LIBS)
 BPGVIEW_LIBS:=$(LIBSDL_LDFLAGS) $(LIBS)
 
 endif #!CONFIG_WIN32
 
-bpgenc.o: CFLAGS+=-Wno-unused-but-set-variable
+bpgenc.o:
 
 libbpg.a: $(LIBBPG_OBJS) 
 	$(AR) rcs $@ $^
@@ -260,6 +260,22 @@
 %.js8a.o: %.c
 	$(EMCC) $(EMCFLAGS) -c -o $@ $<
 
+
+BPGENC_OPTS=-keepmetadata -q 24
+# -c rgb
+
+%.bpg: %.tif bpgenc
+	./bpgenc $(BPGENC_OPTS) $< -o $@
+
+%.bpg: %.jpg bpgenc
+	./bpgenc $(BPGENC_OPTS) $< -o $@
+
+%.bpg: %.JPG bpgenc
+	./bpgenc $(BPGENC_OPTS) $< -o $@
+
+test: $(patsubst %.tif,%.bpg,$(wildcard *.tif)) $(patsubst %.jpg,%.bpg,$(wildcard *.jpg)) $(patsubst %.JPG,%.bpg,$(wildcard *.JPG))
+
+
 -include $(wildcard *.d)
 -include $(wildcard libavcodec/*.d)
 -include $(wildcard libavutil/*.d)
--- a/bpgdec.c	Sun Mar 08 19:14:25 2020 +0200
+++ b/bpgdec.c	Sun Mar 08 19:18:48 2020 +0200
@@ -37,6 +37,10 @@
 #include <png.h>
 #endif
 
+#ifdef HAVE_LIBEXIF
+#include <exif-data.h>
+#endif
+
 #include "libbpg.h"
 
 static void ppm_save(BPGDecoderContext *img, const char *filename)
@@ -249,6 +253,35 @@
 
             printf("  tag=%d (%s) length=%d\n",
                    md->tag, tag_name, md->buf_len);
+
+            switch (md->tag)
+            {
+                case BPG_EXTENSION_TAG_XMP:
+//                    fwrite(md->buf, md->buf_len, 1, stderr);
+                    break;
+
+#ifdef HAVE_LIBEXIF
+                case BPG_EXTENSION_TAG_EXIF:
+                    {
+                        ExifMem *exif_mem = exif_mem_new_default();
+                        if (exif_mem != NULL)
+                        {
+                            ExifData *exif = exif_data_new_mem(exif_mem);
+                            if (exif != NULL)
+                            {
+                                exif_data_load_data(exif, md->buf, md->buf_len);
+                                exif_data_fix(exif);
+                                exif_data_dump(exif);
+                                exif_data_free(exif);
+                            }
+                        }
+                    }
+                    break;
+#endif
+
+                default:
+                    break;
+            }
         }
         bpg_decoder_free_extension_data(first_md);
     }
--- 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)
--- a/x265_glue.c	Sun Mar 08 19:14:25 2020 +0200
+++ b/x265_glue.c	Sun Mar 08 19:18:48 2020 +0200
@@ -148,17 +148,13 @@
 
 static int x265_encode(HEVCEncoderContext *s, Image *img)
 {
-    int c_count, i, ret;
-    x265_picture *pic;
+    int ret;
     uint32_t nal_count;
     x265_nal *p_nal;
-    pic = s->pic;
+    x265_picture *pic = s->pic;
+    int c_count = (img->format == BPG_FORMAT_GRAY) ? 1 : 3;
 
-    if (img->format == BPG_FORMAT_GRAY)
-        c_count = 1;
-    else
-        c_count = 3;
-    for(i = 0; i < c_count; i++) {
+    for (int i = 0; i < c_count; i++) {
         pic->planes[i] = img->data[i];
         pic->stride[i] = img->linesize[i];
     }
@@ -166,7 +162,7 @@
 
     ret = s->api->encoder_encode(s->enc, &p_nal, &nal_count, pic, NULL);
     if (ret > 0) {
-        for(i = 0; i < nal_count; i++) {
+        for(uint32_t i = 0; i < nal_count; i++) {
             add_nal(s, p_nal[i].payload, p_nal[i].sizeBytes);
         }
     }
@@ -175,7 +171,7 @@
 
 static int x265_close(HEVCEncoderContext *s, uint8_t **pbuf)
 {
-    int buf_len, ret, i;
+    int buf_len, ret;
     uint32_t nal_count;
     x265_nal *p_nal;
 
@@ -184,7 +180,7 @@
         ret = s->api->encoder_encode(s->enc, &p_nal, &nal_count, NULL, NULL);
         if (ret <= 0)
             break;
-        for(i = 0; i < nal_count; i++) {
+        for (uint32_t i = 0; i < nal_count; i++) {
             add_nal(s, p_nal[i].payload, p_nal[i].sizeBytes);
         }
     }