00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #define JPEG_INTERNALS
00014 #include "jinclude.h"
00015 #include "jpeglib.h"
00016
00017
00018
00019
00020
00021
00022 GLOBAL(void)
00023 jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
00024 const unsigned int *basic_table,
00025 int scale_factor, boolean force_baseline)
00026
00027
00028
00029
00030
00031 {
00032 JQUANT_TBL ** qtblptr;
00033 int i;
00034 long temp;
00035
00036
00037 if (cinfo->global_state != CSTATE_START)
00038 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00039
00040 if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
00041 ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
00042
00043 qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
00044
00045 if (*qtblptr == NULL)
00046 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
00047
00048 for (i = 0; i < DCTSIZE2; i++) {
00049 temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
00050
00051 if (temp <= 0L) temp = 1L;
00052 if (temp > 32767L) temp = 32767L;
00053 if (force_baseline && temp > 255L)
00054 temp = 255L;
00055 (*qtblptr)->quantval[i] = (UINT16) temp;
00056 }
00057
00058
00059 (*qtblptr)->sent_table = FALSE;
00060 }
00061
00062
00063 GLOBAL(void)
00064 jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
00065 boolean force_baseline)
00066
00067
00068
00069
00070
00071 {
00072
00073
00074
00075
00076 static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
00077 16, 11, 10, 16, 24, 40, 51, 61,
00078 12, 12, 14, 19, 26, 58, 60, 55,
00079 14, 13, 16, 24, 40, 57, 69, 56,
00080 14, 17, 22, 29, 51, 87, 80, 62,
00081 18, 22, 37, 56, 68, 109, 103, 77,
00082 24, 35, 55, 64, 81, 104, 113, 92,
00083 49, 64, 78, 87, 103, 121, 120, 101,
00084 72, 92, 95, 98, 112, 100, 103, 99
00085 };
00086 static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
00087 17, 18, 24, 47, 99, 99, 99, 99,
00088 18, 21, 26, 66, 99, 99, 99, 99,
00089 24, 26, 56, 99, 99, 99, 99, 99,
00090 47, 66, 99, 99, 99, 99, 99, 99,
00091 99, 99, 99, 99, 99, 99, 99, 99,
00092 99, 99, 99, 99, 99, 99, 99, 99,
00093 99, 99, 99, 99, 99, 99, 99, 99,
00094 99, 99, 99, 99, 99, 99, 99, 99
00095 };
00096
00097
00098 jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
00099 scale_factor, force_baseline);
00100 jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
00101 scale_factor, force_baseline);
00102 }
00103
00104
00105 GLOBAL(int)
00106 jpeg_quality_scaling (int quality)
00107
00108
00109
00110
00111 {
00112
00113 if (quality <= 0) quality = 1;
00114 if (quality > 100) quality = 100;
00115
00116
00117
00118
00119
00120
00121
00122 if (quality < 50)
00123 quality = 5000 / quality;
00124 else
00125 quality = 200 - quality*2;
00126
00127 return quality;
00128 }
00129
00130
00131 GLOBAL(void)
00132 jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
00133
00134
00135
00136
00137
00138 {
00139
00140 quality = jpeg_quality_scaling(quality);
00141
00142
00143 jpeg_set_linear_quality(cinfo, quality, force_baseline);
00144 }
00145
00146
00147
00148
00149
00150
00151 LOCAL(void)
00152 add_huff_table (j_compress_ptr cinfo,
00153 JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
00154
00155 {
00156 int nsymbols, len;
00157
00158 if (*htblptr == NULL)
00159 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
00160
00161
00162 MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
00163
00164
00165
00166
00167
00168 nsymbols = 0;
00169 for (len = 1; len <= 16; len++)
00170 nsymbols += bits[len];
00171 if (nsymbols < 1 || nsymbols > 256)
00172 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00173
00174 MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
00175
00176
00177 (*htblptr)->sent_table = FALSE;
00178 }
00179
00180
00181 LOCAL(void)
00182 std_huff_tables (j_compress_ptr cinfo)
00183
00184
00185 {
00186 static const UINT8 bits_dc_luminance[17] =
00187 { 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
00188 static const UINT8 val_dc_luminance[] =
00189 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
00190
00191 static const UINT8 bits_dc_chrominance[17] =
00192 { 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
00193 static const UINT8 val_dc_chrominance[] =
00194 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
00195
00196 static const UINT8 bits_ac_luminance[17] =
00197 { 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
00198 static const UINT8 val_ac_luminance[] =
00199 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
00200 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
00201 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
00202 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
00203 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
00204 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
00205 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
00206 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
00207 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
00208 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00209 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00210 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
00211 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
00212 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
00213 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
00214 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
00215 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
00216 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
00217 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
00218 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00219 0xf9, 0xfa };
00220
00221 static const UINT8 bits_ac_chrominance[17] =
00222 { 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
00223 static const UINT8 val_ac_chrominance[] =
00224 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
00225 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
00226 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
00227 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
00228 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
00229 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
00230 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
00231 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00232 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
00233 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
00234 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
00235 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00236 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
00237 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
00238 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
00239 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
00240 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
00241 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
00242 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
00243 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00244 0xf9, 0xfa };
00245
00246 add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
00247 bits_dc_luminance, val_dc_luminance);
00248 add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
00249 bits_ac_luminance, val_ac_luminance);
00250 add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
00251 bits_dc_chrominance, val_dc_chrominance);
00252 add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
00253 bits_ac_chrominance, val_ac_chrominance);
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 GLOBAL(void)
00268 jpeg_set_defaults (j_compress_ptr cinfo)
00269 {
00270 int i;
00271
00272
00273 if (cinfo->global_state != CSTATE_START)
00274 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00275
00276
00277
00278
00279
00280 if (cinfo->comp_info == NULL)
00281 cinfo->comp_info = (jpeg_component_info *)
00282 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00283 MAX_COMPONENTS * SIZEOF(jpeg_component_info));
00284
00285
00286
00287 cinfo->data_precision = BITS_IN_JSAMPLE;
00288
00289 jpeg_set_quality(cinfo, 75, TRUE);
00290
00291 std_huff_tables(cinfo);
00292
00293
00294 for (i = 0; i < NUM_ARITH_TBLS; i++) {
00295 cinfo->arith_dc_L[i] = 0;
00296 cinfo->arith_dc_U[i] = 1;
00297 cinfo->arith_ac_K[i] = 5;
00298 }
00299
00300
00301 cinfo->scan_info = NULL;
00302 cinfo->num_scans = 0;
00303
00304
00305 cinfo->raw_data_in = FALSE;
00306
00307
00308 cinfo->arith_code = FALSE;
00309
00310
00311 cinfo->optimize_coding = FALSE;
00312
00313
00314
00315
00316
00317 if (cinfo->data_precision > 8)
00318 cinfo->optimize_coding = TRUE;
00319
00320
00321 cinfo->CCIR601_sampling = FALSE;
00322
00323
00324 cinfo->smoothing_factor = 0;
00325
00326
00327 cinfo->dct_method = JDCT_DEFAULT;
00328
00329
00330 cinfo->restart_interval = 0;
00331 cinfo->restart_in_rows = 0;
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 cinfo->JFIF_major_version = 1;
00343 cinfo->JFIF_minor_version = 1;
00344 cinfo->density_unit = 0;
00345 cinfo->X_density = 1;
00346 cinfo->Y_density = 1;
00347
00348
00349
00350 jpeg_default_colorspace(cinfo);
00351 }
00352
00353
00354
00355
00356
00357
00358 GLOBAL(void)
00359 jpeg_default_colorspace (j_compress_ptr cinfo)
00360 {
00361 switch (cinfo->in_color_space) {
00362 case JCS_GRAYSCALE:
00363 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
00364 break;
00365 case JCS_RGB:
00366 jpeg_set_colorspace(cinfo, JCS_YCbCr);
00367 break;
00368 case JCS_YCbCr:
00369 jpeg_set_colorspace(cinfo, JCS_YCbCr);
00370 break;
00371 case JCS_CMYK:
00372 jpeg_set_colorspace(cinfo, JCS_CMYK);
00373 break;
00374 case JCS_YCCK:
00375 jpeg_set_colorspace(cinfo, JCS_YCCK);
00376 break;
00377 case JCS_UNKNOWN:
00378 jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
00379 break;
00380 default:
00381 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
00382 }
00383 }
00384
00385
00386
00387
00388
00389
00390 GLOBAL(void)
00391 jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
00392 {
00393 jpeg_component_info * compptr;
00394 int ci;
00395
00396 #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \
00397 (compptr = &cinfo->comp_info[index], \
00398 compptr->component_id = (id), \
00399 compptr->h_samp_factor = (hsamp), \
00400 compptr->v_samp_factor = (vsamp), \
00401 compptr->quant_tbl_no = (quant), \
00402 compptr->dc_tbl_no = (dctbl), \
00403 compptr->ac_tbl_no = (actbl) )
00404
00405
00406 if (cinfo->global_state != CSTATE_START)
00407 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00408
00409
00410
00411
00412
00413 cinfo->jpeg_color_space = colorspace;
00414
00415 cinfo->write_JFIF_header = FALSE;
00416 cinfo->write_Adobe_marker = FALSE;
00417
00418 switch (colorspace) {
00419 case JCS_GRAYSCALE:
00420 cinfo->write_JFIF_header = TRUE;
00421 cinfo->num_components = 1;
00422
00423 SET_COMP(0, 1, 1,1, 0, 0,0);
00424 break;
00425 case JCS_RGB:
00426 cinfo->write_Adobe_marker = TRUE;
00427 cinfo->num_components = 3;
00428 SET_COMP(0, 0x52 , 1,1, 0, 0,0);
00429 SET_COMP(1, 0x47 , 1,1, 0, 0,0);
00430 SET_COMP(2, 0x42 , 1,1, 0, 0,0);
00431 break;
00432 case JCS_YCbCr:
00433 cinfo->write_JFIF_header = TRUE;
00434 cinfo->num_components = 3;
00435
00436
00437 SET_COMP(0, 1, 2,2, 0, 0,0);
00438 SET_COMP(1, 2, 1,1, 1, 1,1);
00439 SET_COMP(2, 3, 1,1, 1, 1,1);
00440 break;
00441 case JCS_CMYK:
00442 cinfo->write_Adobe_marker = TRUE;
00443 cinfo->num_components = 4;
00444 SET_COMP(0, 0x43 , 1,1, 0, 0,0);
00445 SET_COMP(1, 0x4D , 1,1, 0, 0,0);
00446 SET_COMP(2, 0x59 , 1,1, 0, 0,0);
00447 SET_COMP(3, 0x4B , 1,1, 0, 0,0);
00448 break;
00449 case JCS_YCCK:
00450 cinfo->write_Adobe_marker = TRUE;
00451 cinfo->num_components = 4;
00452 SET_COMP(0, 1, 2,2, 0, 0,0);
00453 SET_COMP(1, 2, 1,1, 1, 1,1);
00454 SET_COMP(2, 3, 1,1, 1, 1,1);
00455 SET_COMP(3, 4, 2,2, 0, 0,0);
00456 break;
00457 case JCS_UNKNOWN:
00458 cinfo->num_components = cinfo->input_components;
00459 if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
00460 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
00461 MAX_COMPONENTS);
00462 for (ci = 0; ci < cinfo->num_components; ci++) {
00463 SET_COMP(ci, ci, 1,1, 0, 0,0);
00464 }
00465 break;
00466 default:
00467 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
00468 }
00469 }
00470
00471
00472 #ifdef C_PROGRESSIVE_SUPPORTED
00473
00474 LOCAL(jpeg_scan_info *)
00475 fill_a_scan (jpeg_scan_info * scanptr, int ci,
00476 int Ss, int Se, int Ah, int Al)
00477
00478 {
00479 scanptr->comps_in_scan = 1;
00480 scanptr->component_index[0] = ci;
00481 scanptr->Ss = Ss;
00482 scanptr->Se = Se;
00483 scanptr->Ah = Ah;
00484 scanptr->Al = Al;
00485 scanptr++;
00486 return scanptr;
00487 }
00488
00489 LOCAL(jpeg_scan_info *)
00490 fill_scans (jpeg_scan_info * scanptr, int ncomps,
00491 int Ss, int Se, int Ah, int Al)
00492
00493 {
00494 int ci;
00495
00496 for (ci = 0; ci < ncomps; ci++) {
00497 scanptr->comps_in_scan = 1;
00498 scanptr->component_index[0] = ci;
00499 scanptr->Ss = Ss;
00500 scanptr->Se = Se;
00501 scanptr->Ah = Ah;
00502 scanptr->Al = Al;
00503 scanptr++;
00504 }
00505 return scanptr;
00506 }
00507
00508 LOCAL(jpeg_scan_info *)
00509 fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
00510
00511 {
00512 int ci;
00513
00514 if (ncomps <= MAX_COMPS_IN_SCAN) {
00515
00516 scanptr->comps_in_scan = ncomps;
00517 for (ci = 0; ci < ncomps; ci++)
00518 scanptr->component_index[ci] = ci;
00519 scanptr->Ss = scanptr->Se = 0;
00520 scanptr->Ah = Ah;
00521 scanptr->Al = Al;
00522 scanptr++;
00523 } else {
00524
00525 scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
00526 }
00527 return scanptr;
00528 }
00529
00530
00531
00532
00533
00534
00535
00536 GLOBAL(void)
00537 jpeg_simple_progression (j_compress_ptr cinfo)
00538 {
00539 int ncomps = cinfo->num_components;
00540 int nscans;
00541 jpeg_scan_info * scanptr;
00542
00543
00544 if (cinfo->global_state != CSTATE_START)
00545 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00546
00547
00548 if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
00549
00550 nscans = 10;
00551 } else {
00552
00553 if (ncomps > MAX_COMPS_IN_SCAN)
00554 nscans = 6 * ncomps;
00555 else
00556 nscans = 2 + 4 * ncomps;
00557 }
00558
00559
00560
00561
00562
00563
00564
00565
00566 if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
00567 cinfo->script_space_size = MAX(nscans, 10);
00568 cinfo->script_space = (jpeg_scan_info *)
00569 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00570 cinfo->script_space_size * SIZEOF(jpeg_scan_info));
00571 }
00572 scanptr = cinfo->script_space;
00573 cinfo->scan_info = scanptr;
00574 cinfo->num_scans = nscans;
00575
00576 if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
00577
00578
00579 scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
00580
00581 scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
00582
00583 scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
00584 scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
00585
00586 scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
00587
00588 scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
00589
00590 scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
00591
00592 scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
00593 scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
00594
00595 scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
00596 } else {
00597
00598
00599 scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
00600 scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
00601 scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
00602
00603 scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
00604
00605 scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
00606 scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
00607 }
00608 }
00609
00610 #endif