< prev index next >

modules/javafx.graphics/src/main/native-iio/libjpeg7/jquant1.c

Print this page


   1 /*
   2  * jquant1.c
   3  *
   4  * Copyright (C) 1991-1996, Thomas G. Lane.

   5  * This file is part of the Independent JPEG Group's software.
   6  * For conditions of distribution and use, see the accompanying README file.
   7  *
   8  * This file contains 1-pass color quantization (color mapping) routines.
   9  * These routines provide mapping to a fixed color map using equally spaced
  10  * color values.  Optional Floyd-Steinberg or ordered dithering is available.
  11  */
  12 
  13 #define JPEG_INTERNALS
  14 #include "jinclude.h"
  15 #include "jpeglib.h"
  16 
  17 #ifdef QUANT_1PASS_SUPPORTED
  18 
  19 
  20 /*
  21  * The main purpose of 1-pass quantization is to provide a fast, if not very
  22  * high quality, colormapped output capability.  A 2-pass quantizer usually
  23  * gives better visual quality; however, for quantized grayscale output this
  24  * quantizer is perfectly adequate.  Dithering is highly recommended with this


 513 
 514 METHODDEF(void)
 515 quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 516                      JSAMPARRAY output_buf, int num_rows)
 517 /* General case, with ordered dithering */
 518 {
 519   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 520   register JSAMPROW input_ptr;
 521   register JSAMPROW output_ptr;
 522   JSAMPROW colorindex_ci;
 523   int * dither;                 /* points to active row of dither matrix */
 524   int row_index, col_index;     /* current indexes into dither matrix */
 525   int nc = cinfo->out_color_components;
 526   int ci;
 527   int row;
 528   JDIMENSION col;
 529   JDIMENSION width = cinfo->output_width;
 530 
 531   for (row = 0; row < num_rows; row++) {
 532     /* Initialize output values to 0 so can process components separately */
 533     jzero_far((void FAR *) output_buf[row],
 534               (size_t) (width * SIZEOF(JSAMPLE)));
 535     row_index = cquantize->row_index;
 536     for (ci = 0; ci < nc; ci++) {
 537       input_ptr = input_buf[row] + ci;
 538       output_ptr = output_buf[row];
 539       colorindex_ci = cquantize->colorindex[ci];
 540       dither = cquantize->odither[ci][row_index];
 541       col_index = 0;
 542 
 543       for (col = width; col > 0; col--) {
 544         /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
 545          * select output value, accumulate into output code for this pixel.
 546          * Range-limiting need not be done explicitly, as we have extended
 547          * the colorindex table to produce the right answers for out-of-range
 548          * inputs.  The maximum dither is +- MAXJSAMPLE; this sets the
 549          * required amount of padding.
 550          */
 551         *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
 552         input_ptr += nc;
 553         output_ptr++;


 618   LOCFSERROR bnexterr;          /* error for below/next col */
 619   LOCFSERROR delta;
 620   register FSERRPTR errorptr;   /* => fserrors[] at column before current */
 621   register JSAMPROW input_ptr;
 622   register JSAMPROW output_ptr;
 623   JSAMPROW colorindex_ci;
 624   JSAMPROW colormap_ci;
 625   int pixcode;
 626   int nc = cinfo->out_color_components;
 627   int dir;                      /* 1 for left-to-right, -1 for right-to-left */
 628   int dirnc;                    /* dir * nc */
 629   int ci;
 630   int row;
 631   JDIMENSION col;
 632   JDIMENSION width = cinfo->output_width;
 633   JSAMPLE *range_limit = cinfo->sample_range_limit;
 634   SHIFT_TEMPS
 635 
 636   for (row = 0; row < num_rows; row++) {
 637     /* Initialize output values to 0 so can process components separately */
 638     jzero_far((void FAR *) output_buf[row],
 639               (size_t) (width * SIZEOF(JSAMPLE)));
 640     for (ci = 0; ci < nc; ci++) {
 641       input_ptr = input_buf[row] + ci;
 642       output_ptr = output_buf[row];
 643       if (cquantize->on_odd_row) {
 644         /* work right to left in this row */
 645         input_ptr += (width-1) * nc; /* so point to rightmost pixel */
 646         output_ptr += width-1;
 647         dir = -1;
 648         dirnc = -nc;
 649         errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
 650       } else {
 651         /* work left to right in this row */
 652         dir = 1;
 653         dirnc = nc;
 654         errorptr = cquantize->fserrors[ci]; /* => entry before first column */
 655       }
 656       colorindex_ci = cquantize->colorindex[ci];
 657       colormap_ci = cquantize->sv_colormap[ci];
 658       /* Preset error values: no error propagated to first pixel from left */


 764     cquantize->row_index = 0;   /* initialize state for ordered dither */
 765     /* If user changed to ordered dither from another mode,
 766      * we must recreate the color index table with padding.
 767      * This will cost extra space, but probably isn't very likely.
 768      */
 769     if (! cquantize->is_padded)
 770       create_colorindex(cinfo);
 771     /* Create ordered-dither tables if we didn't already. */
 772     if (cquantize->odither[0] == NULL)
 773       create_odither_tables(cinfo);
 774     break;
 775   case JDITHER_FS:
 776     cquantize->pub.color_quantize = quantize_fs_dither;
 777     cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
 778     /* Allocate Floyd-Steinberg workspace if didn't already. */
 779     if (cquantize->fserrors[0] == NULL)
 780       alloc_fs_workspace(cinfo);
 781     /* Initialize the propagated errors to zero. */
 782     arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
 783     for (i = 0; i < cinfo->out_color_components; i++)
 784       jzero_far((void FAR *) cquantize->fserrors[i], arraysize);
 785     break;
 786   default:
 787     ERREXIT(cinfo, JERR_NOT_COMPILED);
 788     break;
 789   }
 790 }
 791 
 792 
 793 /*
 794  * Finish up at the end of the pass.
 795  */
 796 
 797 METHODDEF(void)
 798 finish_pass_1_quant (j_decompress_ptr cinfo)
 799 {
 800   /* no work in 1-pass case */
 801 }
 802 
 803 
 804 /*


   1 /*
   2  * jquant1.c
   3  *
   4  * Copyright (C) 1991-1996, Thomas G. Lane.
   5  * Modified 2011 by Guido Vollbeding.
   6  * This file is part of the Independent JPEG Group's software.
   7  * For conditions of distribution and use, see the accompanying README file.
   8  *
   9  * This file contains 1-pass color quantization (color mapping) routines.
  10  * These routines provide mapping to a fixed color map using equally spaced
  11  * color values.  Optional Floyd-Steinberg or ordered dithering is available.
  12  */
  13 
  14 #define JPEG_INTERNALS
  15 #include "jinclude.h"
  16 #include "jpeglib.h"
  17 
  18 #ifdef QUANT_1PASS_SUPPORTED
  19 
  20 
  21 /*
  22  * The main purpose of 1-pass quantization is to provide a fast, if not very
  23  * high quality, colormapped output capability.  A 2-pass quantizer usually
  24  * gives better visual quality; however, for quantized grayscale output this
  25  * quantizer is perfectly adequate.  Dithering is highly recommended with this


 514 
 515 METHODDEF(void)
 516 quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 517                      JSAMPARRAY output_buf, int num_rows)
 518 /* General case, with ordered dithering */
 519 {
 520   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 521   register JSAMPROW input_ptr;
 522   register JSAMPROW output_ptr;
 523   JSAMPROW colorindex_ci;
 524   int * dither;                 /* points to active row of dither matrix */
 525   int row_index, col_index;     /* current indexes into dither matrix */
 526   int nc = cinfo->out_color_components;
 527   int ci;
 528   int row;
 529   JDIMENSION col;
 530   JDIMENSION width = cinfo->output_width;
 531 
 532   for (row = 0; row < num_rows; row++) {
 533     /* Initialize output values to 0 so can process components separately */
 534     FMEMZERO((void FAR *) output_buf[row],
 535              (size_t) (width * SIZEOF(JSAMPLE)));
 536     row_index = cquantize->row_index;
 537     for (ci = 0; ci < nc; ci++) {
 538       input_ptr = input_buf[row] + ci;
 539       output_ptr = output_buf[row];
 540       colorindex_ci = cquantize->colorindex[ci];
 541       dither = cquantize->odither[ci][row_index];
 542       col_index = 0;
 543 
 544       for (col = width; col > 0; col--) {
 545         /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
 546          * select output value, accumulate into output code for this pixel.
 547          * Range-limiting need not be done explicitly, as we have extended
 548          * the colorindex table to produce the right answers for out-of-range
 549          * inputs.  The maximum dither is +- MAXJSAMPLE; this sets the
 550          * required amount of padding.
 551          */
 552         *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
 553         input_ptr += nc;
 554         output_ptr++;


 619   LOCFSERROR bnexterr;          /* error for below/next col */
 620   LOCFSERROR delta;
 621   register FSERRPTR errorptr;   /* => fserrors[] at column before current */
 622   register JSAMPROW input_ptr;
 623   register JSAMPROW output_ptr;
 624   JSAMPROW colorindex_ci;
 625   JSAMPROW colormap_ci;
 626   int pixcode;
 627   int nc = cinfo->out_color_components;
 628   int dir;                      /* 1 for left-to-right, -1 for right-to-left */
 629   int dirnc;                    /* dir * nc */
 630   int ci;
 631   int row;
 632   JDIMENSION col;
 633   JDIMENSION width = cinfo->output_width;
 634   JSAMPLE *range_limit = cinfo->sample_range_limit;
 635   SHIFT_TEMPS
 636 
 637   for (row = 0; row < num_rows; row++) {
 638     /* Initialize output values to 0 so can process components separately */
 639     FMEMZERO((void FAR *) output_buf[row],
 640              (size_t) (width * SIZEOF(JSAMPLE)));
 641     for (ci = 0; ci < nc; ci++) {
 642       input_ptr = input_buf[row] + ci;
 643       output_ptr = output_buf[row];
 644       if (cquantize->on_odd_row) {
 645         /* work right to left in this row */
 646         input_ptr += (width-1) * nc; /* so point to rightmost pixel */
 647         output_ptr += width-1;
 648         dir = -1;
 649         dirnc = -nc;
 650         errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
 651       } else {
 652         /* work left to right in this row */
 653         dir = 1;
 654         dirnc = nc;
 655         errorptr = cquantize->fserrors[ci]; /* => entry before first column */
 656       }
 657       colorindex_ci = cquantize->colorindex[ci];
 658       colormap_ci = cquantize->sv_colormap[ci];
 659       /* Preset error values: no error propagated to first pixel from left */


 765     cquantize->row_index = 0;        /* initialize state for ordered dither */
 766     /* If user changed to ordered dither from another mode,
 767      * we must recreate the color index table with padding.
 768      * This will cost extra space, but probably isn't very likely.
 769      */
 770     if (! cquantize->is_padded)
 771       create_colorindex(cinfo);
 772     /* Create ordered-dither tables if we didn't already. */
 773     if (cquantize->odither[0] == NULL)
 774       create_odither_tables(cinfo);
 775     break;
 776   case JDITHER_FS:
 777     cquantize->pub.color_quantize = quantize_fs_dither;
 778     cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
 779     /* Allocate Floyd-Steinberg workspace if didn't already. */
 780     if (cquantize->fserrors[0] == NULL)
 781       alloc_fs_workspace(cinfo);
 782     /* Initialize the propagated errors to zero. */
 783     arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
 784     for (i = 0; i < cinfo->out_color_components; i++)
 785       FMEMZERO((void FAR *) cquantize->fserrors[i], arraysize);
 786     break;
 787   default:
 788     ERREXIT(cinfo, JERR_NOT_COMPILED);
 789     break;
 790   }
 791 }
 792 
 793 
 794 /*
 795  * Finish up at the end of the pass.
 796  */
 797 
 798 METHODDEF(void)
 799 finish_pass_1_quant (j_decompress_ptr cinfo)
 800 {
 801   /* no work in 1-pass case */
 802 }
 803 
 804 
 805 /*


< prev index next >