Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/3rdparty/libjpeg/jchuff.c

    r2 r846  
    33 *
    44 * Copyright (C) 1991-1997, Thomas G. Lane.
     5
    56 * This file is part of the Independent JPEG Group's software.
    67 * For conditions of distribution and use, see the accompanying README file.
    78 *
    89 * This file contains Huffman entropy encoding routines.
     10
    911 *
    1012 * Much of the complexity here has to do with supporting output suspension.
     
    1315 * variables into local working storage, and update them back to the
    1416 * permanent JPEG objects only upon successful completion of an MCU.
     17
     18
     19
     20
    1521 */
    1622
     
    1824#include "jinclude.h"
    1925#include "jpeglib.h"
    20 #include "jchuff.h"             /* Declarations shared with jcphuff.c */
     26
     27
     28/* The legal range of a DCT coefficient is
     29 *  -1024 .. +1023  for 8-bit data;
     30 * -16384 .. +16383 for 12-bit data.
     31 * Hence the magnitude should always fit in 10 or 14 bits respectively.
     32 */
     33
     34#if BITS_IN_JSAMPLE == 8
     35#define MAX_COEF_BITS 10
     36#else
     37#define MAX_COEF_BITS 14
     38#endif
     39
     40/* Derived data constructed for each Huffman table */
     41
     42typedef struct {
     43  unsigned int ehufco[256];     /* code for each symbol */
     44  char ehufsi[256];             /* length of code for each symbol */
     45  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
     46} c_derived_tbl;
    2147
    2248
     
    6692  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
    6793
    68 #ifdef ENTROPY_OPT_SUPPORTED    /* Statistics tables for optimization */
     94/* Statistics tables for optimization */
    6995  long * dc_count_ptrs[NUM_HUFF_TBLS];
    7096  long * ac_count_ptrs[NUM_HUFF_TBLS];
    71 #endif
     97
     98  /* Following fields used only in progressive mode */
     99
     100  /* Mode flag: TRUE for optimization, FALSE for actual data output */
     101  boolean gather_statistics;
     102
     103  /* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
     104   */
     105  JOCTET * next_output_byte;    /* => next byte to write in buffer */
     106  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
     107  j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
     108
     109  /* Coding status for AC components */
     110  int ac_tbl_no;                /* the table number of the single component */
     111  unsigned int EOBRUN;          /* run length of EOBs */
     112  unsigned int BE;              /* # of buffered correction bits before MCU */
     113  char * bit_buffer;            /* buffer for correction bits (1 per char) */
     114  /* packing correction bits tightly would save some space but cost time... */
    72115} huff_entropy_encoder;
    73116
    74117typedef huff_entropy_encoder * huff_entropy_ptr;
    75118
    76 /* Working state while writing an MCU.
     119/* Working state while writing an MCU.
    77120 * This struct contains all the fields that are needed by subroutines.
    78121 */
     
    85128} working_state;
    86129
    87 
    88 /* Forward declarations */
    89 METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
    90                                         JBLOCKROW *MCU_data));
    91 METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
    92 #ifdef ENTROPY_OPT_SUPPORTED
    93 METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
    94                                           JBLOCKROW *MCU_data));
    95 METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
     130/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
     131 * buffer can hold.  Larger sizes may slightly improve compression, but
     132 * 1000 is already well into the realm of overkill.
     133 * The minimum safe size is 64 bits.
     134 */
     135
     136#define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
     137
     138/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
     139 * We assume that int right shift is unsigned if INT32 right shift is,
     140 * which should be safe.
     141 */
     142
     143#ifdef RIGHT_SHIFT_IS_UNSIGNED
     144#define ISHIFT_TEMPS    int ishift_temp;
     145#define IRIGHT_SHIFT(x,shft)  \
     146        ((ishift_temp = (x)) < 0 ? \
     147         (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
     148         (ishift_temp >> (shft)))
     149#else
     150#define ISHIFT_TEMPS
     151#define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
    96152#endif
    97 
    98 
    99 /*
    100  * Initialize for a Huffman-compressed scan.
    101  * If gather_statistics is TRUE, we do not output anything during the scan,
    102  * just count the Huffman symbols used and generate Huffman code tables.
    103  */
    104 
    105 METHODDEF(void)
    106 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
    107 {
    108   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    109   int ci, dctbl, actbl;
    110   jpeg_component_info * compptr;
    111 
    112   if (gather_statistics) {
    113 #ifdef ENTROPY_OPT_SUPPORTED
    114     entropy->pub.encode_mcu = encode_mcu_gather;
    115     entropy->pub.finish_pass = finish_pass_gather;
    116 #else
    117     ERREXIT(cinfo, JERR_NOT_COMPILED);
    118 #endif
    119   } else {
    120     entropy->pub.encode_mcu = encode_mcu_huff;
    121     entropy->pub.finish_pass = finish_pass_huff;
    122   }
    123 
    124   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    125     compptr = cinfo->cur_comp_info[ci];
    126     dctbl = compptr->dc_tbl_no;
    127     actbl = compptr->ac_tbl_no;
    128     if (gather_statistics) {
    129 #ifdef ENTROPY_OPT_SUPPORTED
    130       /* Check for invalid table indexes */
    131       /* (make_c_derived_tbl does this in the other path) */
    132       if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
    133         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
    134       if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
    135         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
    136       /* Allocate and zero the statistics tables */
    137       /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
    138       if (entropy->dc_count_ptrs[dctbl] == NULL)
    139         entropy->dc_count_ptrs[dctbl] = (long *)
    140           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
    141                                       257 * SIZEOF(long));
    142       MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
    143       if (entropy->ac_count_ptrs[actbl] == NULL)
    144         entropy->ac_count_ptrs[actbl] = (long *)
    145           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
    146                                       257 * SIZEOF(long));
    147       MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
    148 #endif
    149     } else {
    150       /* Compute derived values for Huffman tables */
    151       /* We may do this more than once for a table, but it's not expensive */
    152       jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
    153                               & entropy->dc_derived_tbls[dctbl]);
    154       jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
    155                               & entropy->ac_derived_tbls[actbl]);
    156     }
    157     /* Initialize DC predictions to 0 */
    158     entropy->saved.last_dc_val[ci] = 0;
    159   }
    160 
    161   /* Initialize bit buffer to empty */
    162   entropy->saved.put_buffer = 0;
    163   entropy->saved.put_bits = 0;
    164 
    165   /* Initialize restart stuff */
    166   entropy->restarts_to_go = cinfo->restart_interval;
    167   entropy->next_restart_num = 0;
    168 }
    169153
    170154
     
    172156 * Compute the derived values for a Huffman table.
    173157 * This routine also performs some validation checks on the table.
    174  *
    175  * Note this is also used by jcphuff.c.
    176  */
    177 
    178 GLOBAL(void)
     158 */
     159
     160LOCAL(void)
    179161jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
    180162                         c_derived_tbl ** pdtbl)
     
    265247
    266248
    267 /* Outputting bytes to the file */
     249/* Outputting bytes to the file.
     250 * NB: these must be called only when actually outputting,
     251 * that is, entropy->gather_statistics == FALSE.
     252 */
    268253
    269254/* Emit a byte, taking 'action' if must suspend. */
    270 #define emit_byte(state,val,action)  \
     255#define emit_byte(state,val,action)  \
    271256        { *(state)->next_output_byte++ = (JOCTET) (val);  \
    272257          if (--(state)->free_in_buffer == 0)  \
    273             if (! dump_buffer(state))  \
     258            if (! dump_buffer(state))  \
    274259              { action; } }
    275260
     261
     262
     263
     264
     265
     266
    276267
    277268LOCAL(boolean)
    278 dump_buffer (working_state * state)
     269dump_buffer (working_state * state)
    279270/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
    280271{
     
    290281
    291282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
    292297/* Outputting bits to the file */
    293298
     
    300305INLINE
    301306LOCAL(boolean)
    302 emit_bits (working_state * state, unsigned int code, int size)
     307emit_bits (working_state * state, unsigned int code, int size)
    303308/* Emit some bits; return TRUE if successful, FALSE if must suspend */
    304309{
     
    322327    int c = (int) ((put_buffer >> 16) & 0xFF);
    323328   
    324     emit_byte(state, c, return FALSE);
     329    emit_byte(state, c, return FALSE);
    325330    if (c == 0xFF) {            /* need to stuff a zero byte? */
    326       emit_byte(state, 0, return FALSE);
     331      emit_byte(state, 0, return FALSE);
    327332    }
    328333    put_buffer <<= 8;
     
    337342
    338343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
    339385LOCAL(boolean)
    340 flush_bits (working_state * state)
    341 {
    342   if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
     386flush_bits (working_state * state)
     387{
     388  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
    343389    return FALSE;
    344   state->cur.put_buffer = 0;    /* and reset bit-buffer to empty */
     390  state->cur.put_buffer = 0;    /* and reset bit-buffer to empty */
    345391  state->cur.put_bits = 0;
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
     582
     583
     584
     585
     586
     587
     588
     589
     590
     591
     592
     593
     594
     595
     596
     597
     598
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
     760
     761
     762
     763
     764
     765
     766
     767
     768
     769
     770
     771
     772
     773
     774
     775
     776
     777
     778
     779
     780
     781
     782
     783
     784
     785
     786
     787
     788
     789
     790
     791
     792
     793
     794
     795
     796
     797
     798
     799
     800
     801
     802
     803
     804
     805
     806
     807
     808
     809
     810
     811
     812
     813
     814
     815
     816
     817
     818
     819
     820
     821
     822
     823
     824
     825
     826
     827
     828
     829
     830
     831
     832
     833
     834
     835
     836
     837
     838
     839
     840
     841
     842
     843
     844
     845
     846
     847
     848
     849
     850
     851
     852
     853
     854
     855
     856
     857
     858
     859
     860
     861
     862
     863
     864
     865
     866
     867
     868
     869
     870
     871
     872
     873
     874
     875
     876
     877
     878
     879
     880
     881
     882
     883
     884
     885
     886
     887
     888
     889
     890
     891
     892
     893
     894
     895
     896
     897
     898
     899
     900
     901
     902
     903
     904
     905
     906
     907
     908
    346909  return TRUE;
    347910}
     
    357920  register int nbits;
    358921  register int k, r, i;
    359  
     922  int Se = state->cinfo->lim_Se;
     923  const int * natural_order = state->cinfo->natural_order;
     924
    360925  /* Encode the DC coefficient difference per section F.1.2.1 */
    361  
     926
    362927  temp = temp2 = block[0] - last_dc_val;
    363928
     
    368933    temp2--;
    369934  }
    370  
     935
    371936  /* Find the number of bits needed for the magnitude of the coefficient */
    372937  nbits = 0;
     
    380945  if (nbits > MAX_COEF_BITS+1)
    381946    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
    382  
     947
    383948  /* Emit the Huffman-coded symbol for the number of bits */
    384   if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
     949  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
    385950    return FALSE;
    386951
     
    388953  /* or the complement of its magnitude, if negative. */
    389954  if (nbits)                    /* emit_bits rejects calls with size 0 */
    390     if (! emit_bits(state, (unsigned int) temp2, nbits))
     955    if (! emit_bits(state, (unsigned int) temp2, nbits))
    391956      return FALSE;
    392957
    393958  /* Encode the AC coefficients per section F.1.2.2 */
    394  
     959
    395960  r = 0;                        /* r = run length of zeros */
    396  
    397   for (k = 1; k < DCTSIZE2; k++) {
    398     if ((temp = block[jpeg_natural_order[k]]) == 0) {
     961
     962  for (k = 1; k <; k++) {
     963    if ((temp = block[natural_order[k]]) == 0) {
    399964      r++;
    400965    } else {
    401966      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
    402967      while (r > 15) {
    403         if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
     968        if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
    404969          return FALSE;
    405970        r -= 16;
     
    412977        temp2--;
    413978      }
    414      
     979
    415980      /* Find the number of bits needed for the magnitude of the coefficient */
    416981      nbits = 1;                /* there must be at least one 1 bit */
     
    420985      if (nbits > MAX_COEF_BITS)
    421986        ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
    422      
     987
    423988      /* Emit Huffman symbol for run length / number of bits */
    424989      i = (r << 4) + nbits;
    425       if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
     990      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
    426991        return FALSE;
    427992
    428993      /* Emit that number of bits of the value, if positive, */
    429994      /* or the complement of its magnitude, if negative. */
    430       if (! emit_bits(state, (unsigned int) temp2, nbits))
     995      if (! emit_bits(state, (unsigned int) temp2, nbits))
    431996        return FALSE;
    432      
     997
    433998      r = 0;
    434999    }
     
    4371002  /* If the last coef(s) were zero, emit an end-of-block code */
    4381003  if (r > 0)
    439     if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
     1004    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
    4401005      return FALSE;
    441 
    442   return TRUE;
    443 }
    444 
    445 
    446 /*
    447  * Emit a restart marker & resynchronize predictions.
    448  */
    449 
    450 LOCAL(boolean)
    451 emit_restart (working_state * state, int restart_num)
    452 {
    453   int ci;
    454 
    455   if (! flush_bits(state))
    456     return FALSE;
    457 
    458   emit_byte(state, 0xFF, return FALSE);
    459   emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
    460 
    461   /* Re-initialize DC predictions to 0 */
    462   for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
    463     state->cur.last_dc_val[ci] = 0;
    464 
    465   /* The restart counter is not updated until we successfully write the MCU. */
    4661006
    4671007  return TRUE;
     
    4901030  if (cinfo->restart_interval) {
    4911031    if (entropy->restarts_to_go == 0)
    492       if (! emit_restart(&state, entropy->next_restart_num))
     1032      if (! emit_restart(&state, entropy->next_restart_num))
    4931033        return FALSE;
    4941034  }
     
    5361076  working_state state;
    5371077
    538   /* Load up working state ... flush_bits needs it */
    539   state.next_output_byte = cinfo->dest->next_output_byte;
    540   state.free_in_buffer = cinfo->dest->free_in_buffer;
    541   ASSIGN_STATE(state.cur, entropy->saved);
    542   state.cinfo = cinfo;
    543 
    544   /* Flush out the last data */
    545   if (! flush_bits(&state))
    546     ERREXIT(cinfo, JERR_CANT_SUSPEND);
    547 
    548   /* Update state */
    549   cinfo->dest->next_output_byte = state.next_output_byte;
    550   cinfo->dest->free_in_buffer = state.free_in_buffer;
    551   ASSIGN_STATE(entropy->saved, state.cur);
     1078  if (cinfo->progressive_mode) {
     1079    entropy->next_output_byte = cinfo->dest->next_output_byte;
     1080    entropy->free_in_buffer = cinfo->dest->free_in_buffer;
     1081
     1082    /* Flush out any buffered data */
     1083    emit_eobrun(entropy);
     1084    flush_bits_e(entropy);
     1085
     1086    cinfo->dest->next_output_byte = entropy->next_output_byte;
     1087    cinfo->dest->free_in_buffer = entropy->free_in_buffer;
     1088  } else {
     1089    /* Load up working state ... flush_bits needs it */
     1090    state.next_output_byte = cinfo->dest->next_output_byte;
     1091    state.free_in_buffer = cinfo->dest->free_in_buffer;
     1092    ASSIGN_STATE(state.cur, entropy->saved);
     1093    state.cinfo = cinfo;
     1094
     1095    /* Flush out the last data */
     1096    if (! flush_bits_s(&state))
     1097      ERREXIT(cinfo, JERR_CANT_SUSPEND);
     1098
     1099    /* Update state */
     1100    cinfo->dest->next_output_byte = state.next_output_byte;
     1101    cinfo->dest->free_in_buffer = state.free_in_buffer;
     1102    ASSIGN_STATE(entropy->saved, state.cur);
     1103  }
    5521104}
    5531105
     
    5641116 */
    5651117
    566 #ifdef ENTROPY_OPT_SUPPORTED
    567 
    5681118
    5691119/* Process a single block's worth of coefficients */
     
    5761126  register int nbits;
    5771127  register int k, r;
     1128
     1129
    5781130 
    5791131  /* Encode the DC coefficient difference per section F.1.2.1 */
     
    6021154  r = 0;                        /* r = run length of zeros */
    6031155 
    604   for (k = 1; k < DCTSIZE2; k++) {
    605     if ((temp = block[jpeg_natural_order[k]]) == 0) {
     1156  for (k = 1; k <; k++) {
     1157    if ((temp = block[natural_order[k]]) == 0) {
    6061158      r++;
    6071159    } else {
     
    6761228/*
    6771229 * Generate the best Huffman code table for the given counts, fill htbl.
    678  * Note this is also used by jcphuff.c.
    6791230 *
    6801231 * The JPEG standard requires that no symbol be assigned a codeword of all
     
    7021253 */
    7031254
    704 GLOBAL(void)
     1255AL(void)
    7051256jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
    7061257{
     
    8471398{
    8481399  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    849   int ci, dctbl, actbl;
     1400  int ci, tbl;
    8501401  jpeg_component_info * compptr;
    8511402  JHUFF_TBL **htblptr;
     
    8561407   * per table, because it clobbers the input frequency counts!
    8571408   */
     1409
     1410
     1411
     1412
    8581413  MEMZERO(did_dc, SIZEOF(did_dc));
    8591414  MEMZERO(did_ac, SIZEOF(did_ac));
     
    8611416  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    8621417    compptr = cinfo->cur_comp_info[ci];
    863     dctbl = compptr->dc_tbl_no;
    864     actbl = compptr->ac_tbl_no;
    865     if (! did_dc[dctbl]) {
    866       htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
    867       if (*htblptr == NULL)
    868         *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
    869       jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
    870       did_dc[dctbl] = TRUE;
    871     }
    872     if (! did_ac[actbl]) {
    873       htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
    874       if (*htblptr == NULL)
    875         *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
    876       jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
    877       did_ac[actbl] = TRUE;
    878     }
    879   }
    880 }
    881 
    882 
    883 #endif /* ENTROPY_OPT_SUPPORTED */
     1418    /* DC needs no table for refinement scan */
     1419    if (cinfo->Ss == 0 && cinfo->Ah == 0) {
     1420      tbl = compptr->dc_tbl_no;
     1421      if (! did_dc[tbl]) {
     1422        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
     1423        if (*htblptr == NULL)
     1424          *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
     1425        jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
     1426        did_dc[tbl] = TRUE;
     1427      }
     1428    }
     1429    /* AC needs no table when not present */
     1430    if (cinfo->Se) {
     1431      tbl = compptr->ac_tbl_no;
     1432      if (! did_ac[tbl]) {
     1433        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
     1434        if (*htblptr == NULL)
     1435          *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
     1436        jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
     1437        did_ac[tbl] = TRUE;
     1438      }
     1439    }
     1440  }
     1441}
     1442
     1443
     1444/*
     1445 * Initialize for a Huffman-compressed scan.
     1446 * If gather_statistics is TRUE, we do not output anything during the scan,
     1447 * just count the Huffman symbols used and generate Huffman code tables.
     1448 */
     1449
     1450METHODDEF(void)
     1451start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
     1452{
     1453  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
     1454  int ci, tbl;
     1455  jpeg_component_info * compptr;
     1456
     1457  if (gather_statistics)
     1458    entropy->pub.finish_pass = finish_pass_gather;
     1459  else
     1460    entropy->pub.finish_pass = finish_pass_huff;
     1461
     1462  if (cinfo->progressive_mode) {
     1463    entropy->cinfo = cinfo;
     1464    entropy->gather_statistics = gather_statistics;
     1465
     1466    /* We assume jcmaster.c already validated the scan parameters. */
     1467
     1468    /* Select execution routine */
     1469    if (cinfo->Ah == 0) {
     1470      if (cinfo->Ss == 0)
     1471        entropy->pub.encode_mcu = encode_mcu_DC_first;
     1472      else
     1473        entropy->pub.encode_mcu = encode_mcu_AC_first;
     1474    } else {
     1475      if (cinfo->Ss == 0)
     1476        entropy->pub.encode_mcu = encode_mcu_DC_refine;
     1477      else {
     1478        entropy->pub.encode_mcu = encode_mcu_AC_refine;
     1479        /* AC refinement needs a correction bit buffer */
     1480        if (entropy->bit_buffer == NULL)
     1481          entropy->bit_buffer = (char *)
     1482            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
     1483                                        MAX_CORR_BITS * SIZEOF(char));
     1484      }
     1485    }
     1486
     1487    /* Initialize AC stuff */
     1488    entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
     1489    entropy->EOBRUN = 0;
     1490    entropy->BE = 0;
     1491  } else {
     1492    if (gather_statistics)
     1493      entropy->pub.encode_mcu = encode_mcu_gather;
     1494    else
     1495      entropy->pub.encode_mcu = encode_mcu_huff;
     1496  }
     1497
     1498  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
     1499    compptr = cinfo->cur_comp_info[ci];
     1500    /* DC needs no table for refinement scan */
     1501    if (cinfo->Ss == 0 && cinfo->Ah == 0) {
     1502      tbl = compptr->dc_tbl_no;
     1503      if (gather_statistics) {
     1504        /* Check for invalid table index */
     1505        /* (make_c_derived_tbl does this in the other path) */
     1506        if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
     1507          ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
     1508        /* Allocate and zero the statistics tables */
     1509        /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
     1510        if (entropy->dc_count_ptrs[tbl] == NULL)
     1511          entropy->dc_count_ptrs[tbl] = (long *)
     1512            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
     1513                                        257 * SIZEOF(long));
     1514        MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
     1515      } else {
     1516        /* Compute derived values for Huffman tables */
     1517        /* We may do this more than once for a table, but it's not expensive */
     1518        jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
     1519                                & entropy->dc_derived_tbls[tbl]);
     1520      }
     1521      /* Initialize DC predictions to 0 */
     1522      entropy->saved.last_dc_val[ci] = 0;
     1523    }
     1524    /* AC needs no table when not present */
     1525    if (cinfo->Se) {
     1526      tbl = compptr->ac_tbl_no;
     1527      if (gather_statistics) {
     1528        if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
     1529          ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
     1530        if (entropy->ac_count_ptrs[tbl] == NULL)
     1531          entropy->ac_count_ptrs[tbl] = (long *)
     1532            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
     1533                                        257 * SIZEOF(long));
     1534        MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
     1535      } else {
     1536        jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
     1537                                & entropy->ac_derived_tbls[tbl]);
     1538      }
     1539    }
     1540  }
     1541
     1542  /* Initialize bit buffer to empty */
     1543  entropy->saved.put_buffer = 0;
     1544  entropy->saved.put_bits = 0;
     1545
     1546  /* Initialize restart stuff */
     1547  entropy->restarts_to_go = cinfo->restart_interval;
     1548  entropy->next_restart_num = 0;
     1549}
    8841550
    8851551
     
    9031569  for (i = 0; i < NUM_HUFF_TBLS; i++) {
    9041570    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
    905 #ifdef ENTROPY_OPT_SUPPORTED
    9061571    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
    907 #endif
    908   }
    909 }
     1572  }
     1573
     1574  if (cinfo->progressive_mode)
     1575    entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
     1576}
Note: See TracChangeset for help on using the changeset viewer.