]> git.sesse.net Git - qscale/blobdiff - libqscale.c
Add support for the Mitchell filter kernel.
[qscale] / libqscale.c
index fa7f2c3f8018c7329632a884eadb9699cc03866c..b784294def3eedb9f4210bd187675b56dee71393 100644 (file)
@@ -1,6 +1,25 @@
+/*
+ * qscale: Quick, high-quality JPEG-to-JPEG scaler.
+ * Copyright (C) 2008 Steinar H. Gunderson <sgunderson@bigfoot.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <malloc.h>
 
 #include "libqscale.h"
@@ -51,7 +70,7 @@ qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
        struct jpeg_error_mgr jerr;
        dinfo.err = jpeg_std_error(&jerr);
        jpeg_create_decompress(&dinfo);
-       jpeg_stdio_src(&dinfo, stdin);
+       jpeg_stdio_src(&dinfo, file);
        jpeg_read_header(&dinfo, TRUE);
        dinfo.raw_data_out = TRUE;
        jpeg_start_decompress(&dinfo);
@@ -74,6 +93,15 @@ qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
        img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
        img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
 
+       img->samp_h0 = dinfo.comp_info[0].h_samp_factor;
+       img->samp_v0 = dinfo.comp_info[0].v_samp_factor;
+
+       img->samp_h1 = dinfo.comp_info[1].h_samp_factor;
+       img->samp_v1 = dinfo.comp_info[1].v_samp_factor;
+
+       img->samp_h2 = dinfo.comp_info[2].h_samp_factor;
+       img->samp_v2 = dinfo.comp_info[2].v_samp_factor;
+
        img->data_y  = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
        img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
        img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
@@ -144,12 +172,39 @@ static double lanczos_tap(double x)
                return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
 }
 
+static double mitchell_tap(double x)
+{
+       const double b = 1.0 / 3.0;
+       const double c = 1.0 / 3.0;
+       const double p0 = (  6.0 -  2.0*b         ) / 6.0;
+       const double p2 = (-18.0 + 12.0*b +  6.0*c) / 6.0;
+       const double p3 = ( 12.0 -  9.0*b -  6.0*c) / 6.0;
+       const double q0 = (         8.0*b + 24.0*c) / 6.0;
+       const double q1 = (      - 12.0*b - 48.0*c) / 6.0;
+       const double q2 = (         6.0*b + 30.0*c) / 6.0;
+       const double q3 = (      -      b -  6.0*c) / 6.0;
+
+       if (x < -2.0) {
+               return 0.0;
+       } else if (x < -1.0) {
+               return q0 - x * (q1 - x * (q2 - x * q3));
+       } else if (x < 0.0) {
+               return p0 + x * x * (p2 - x * p3);
+       } else if (x < 1.0) {
+               return p0 + x * x * (p2 + x * p3);
+       } else if (x < 2.0) {
+               return q0 + x * (q1 + x * (q2 + x * q3));
+       } else {
+               return 0.0;
+       }
+}
+
 struct pix_desc {
        unsigned start, end;
        unsigned startcoeff;
 };
 
-static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
+static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride, enum qscale_scaling_filter scaling_filter)
 {
        struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
        int size_coeffs = 8;
@@ -157,7 +212,13 @@ static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsi
        int num_coeffs = 0;
        int x, y;
        double sf = (double)w / (double)nw;
-       double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
+       double support;
+       
+       if (scaling_filter == LANCZOS) {
+               support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
+       } else {  /* Mitchell */
+               support = (w > nw) ? (2.0 * sf) : (2.0 / sf);
+       }
 
        /* calculate the filter */
        for (x = 0; x < nw; ++x) {
@@ -194,7 +255,12 @@ static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsi
 
                for (sx = start; sx <= end; ++sx) {
                        double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
-                       double f = lanczos_tap(nd);
+                       double f;
+                       if (scaling_filter == LANCZOS) {
+                               f = lanczos_tap(nd);
+                       } else {  /* Mitchell */
+                               f = mitchell_tap(nd);
+                       }
                        if (num_coeffs == size_coeffs) {
                                size_coeffs <<= 1;
                                coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
@@ -272,7 +338,7 @@ static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsi
        }
 }
 
-static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
+static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride, enum qscale_scaling_filter scaling_filter)
 {
        struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
        int size_coeffs = 8;
@@ -280,7 +346,13 @@ static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsi
        int num_coeffs = 0;
        int x, y, sy;
        double sf = (double)h / (double)nh;
-       double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
+       double support;
+       
+       if (scaling_filter == LANCZOS) {
+               support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
+       } else {  /* Mitchell */
+               support = (h > nh) ? (2.0 * sf) : (2.0 / sf);
+       }
 
        /* calculate the filter */
        for (y = 0; y < nh; ++y) {
@@ -301,7 +373,12 @@ static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsi
 
                for (sy = start; sy <= end; ++sy) {
                        double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
-                       double f = lanczos_tap(nd);
+                       double f;
+                       if (scaling_filter == LANCZOS) {
+                               f = lanczos_tap(nd);
+                       } else {  /* Mitchell */
+                               f = mitchell_tap(nd);
+                       }
                        if (num_coeffs == size_coeffs) {
                                size_coeffs <<= 1;
                                coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
@@ -452,6 +529,36 @@ static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsi
        }
 }
 
+qscale_img *qscale_clone(const qscale_img *img)
+{
+       qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
+       if (dst == NULL) {
+               return NULL;
+       }
+
+       *dst = *img;
+
+       unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
+       unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
+       unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
+
+       /* FIXME: handle out-of-memory gracefully */
+       {
+               dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
+               memcpy(dst->data_y, img->data_y, dst->h0 * dstride0);
+       }
+       {
+               dst->data_cb = (unsigned char *)malloc(dst->h1 * dstride1);
+               memcpy(dst->data_cb, img->data_cb, dst->h1 * dstride1);
+       }
+       {
+               dst->data_cr = (unsigned char *)malloc(dst->h2 * dstride2);
+               memcpy(dst->data_cr, img->data_cr, dst->h2 * dstride2);
+       }
+
+       return dst;
+}
+
 qscale_img *qscale_scale(qscale_img *src, unsigned width, unsigned height, unsigned samp_h0, unsigned samp_v0, unsigned samp_h1, unsigned samp_v1, unsigned samp_h2, unsigned samp_v2, enum qscale_scaling_filter scaling_filter)
 {
        qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
@@ -484,6 +591,15 @@ qscale_img *qscale_scale(qscale_img *src, unsigned width, unsigned height, unsig
        dst->w2 = width * samp_h2 / max_samp_h;
        dst->h2 = height * samp_v2 / max_samp_v;
 
+       dst->samp_h0 = samp_h0;
+       dst->samp_v0 = samp_v0;
+
+       dst->samp_h1 = samp_h1;
+       dst->samp_v1 = samp_v1;
+
+       dst->samp_h2 = samp_h2;
+       dst->samp_v2 = samp_v2;
+
        unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
        unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
        unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
@@ -495,25 +611,110 @@ qscale_img *qscale_scale(qscale_img *src, unsigned width, unsigned height, unsig
        /* FIXME: handle out-of-memory gracefully */
        {
                float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
-               vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0);
+               vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0, scaling_filter);
                dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
-               hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0);
+               hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0, scaling_filter);
                free(npix);
        }
        {
                float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
-               vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1);
+               vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1, scaling_filter);
                dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
-               hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1);
+               hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1, scaling_filter);
                free(npix);
        }
        {
                float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
-               vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2);
+               vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2, scaling_filter);
                dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
-               hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2);
+               hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2, scaling_filter);
                free(npix);
        }
 
        return dst;
 }
+
+int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
+{
+       FILE *file = fopen(filename, "wb");
+       if (file == NULL) {
+               return -1;
+       }
+
+       int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
+
+       fclose(file);
+       return err;
+}
+
+int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
+{
+        struct jpeg_compress_struct cinfo;
+        struct jpeg_error_mgr jerr;
+        cinfo.err = jpeg_std_error(&jerr);
+        jpeg_create_compress(&cinfo);
+        jpeg_stdio_dest(&cinfo, file);
+        cinfo.input_components = 3;
+        jpeg_set_defaults(&cinfo);
+        jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
+
+       if (jpeg_mode == PROGRESSIVE) {
+               jpeg_simple_progression(&cinfo);
+       }
+
+        cinfo.image_width = img->width;
+        cinfo.image_height = img->height;
+        cinfo.raw_data_in = TRUE;
+        jpeg_set_colorspace(&cinfo, JCS_YCbCr);
+        cinfo.comp_info[0].h_samp_factor = img->samp_h0;
+        cinfo.comp_info[0].v_samp_factor = img->samp_v0;
+        cinfo.comp_info[1].h_samp_factor = img->samp_h1;
+        cinfo.comp_info[1].v_samp_factor = img->samp_v1;
+        cinfo.comp_info[2].h_samp_factor = img->samp_h2;
+        cinfo.comp_info[2].v_samp_factor = img->samp_v2;
+        jpeg_start_compress(&cinfo, TRUE);
+
+        unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
+        unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
+        unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
+
+        int total_lines = 0;
+        int blocks = 0;
+        while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
+                unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
+
+                JSAMPROW y_row_ptrs[max_lines];
+                JSAMPROW cb_row_ptrs[max_lines];
+                JSAMPROW cr_row_ptrs[max_lines];
+                JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
+                int i;
+
+                for (i = 0; i < max_lines; ++i) {
+                        /* simple edge extension */
+                        int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
+                        if (yline > img->h0 - 1)
+                                yline = img->h0 - 1;
+
+                        int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
+                       if (cbline > img->h1 - 1)
+                                cbline = img->h1 - 1;
+
+                        int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
+                        if (crline > img->h2 - 1)
+                                crline = img->h2 - 1;
+
+                        y_row_ptrs[i]  = img->data_y  + yline * dstride0;
+                        cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
+                        cr_row_ptrs[i] = img->data_cr + crline * dstride2;
+                }
+
+                total_lines += max_lines;
+                ++blocks;
+
+                jpeg_write_raw_data(&cinfo, ptrs, max_lines);
+        }
+        jpeg_finish_compress(&cinfo);
+        jpeg_destroy_compress(&cinfo);
+
+       return 0;
+}