I just show people one of lootines that gives us nspiration and we must care about copyleft of GPL for customizing and delivering.
XV has GPL inside its codes.
The loader opens file and use jpeg library like bold lines below:
=== Copyright of xv ===
447 :/***************************************************************************/ 448 :/* LOAD ROUTINES ***********************************************************/ 449 :/***************************************************************************/ 450 : 451 : 452 :/*******************************************/ 453 :int LoadJFIF(fname, pinfo, quick) 454 : char *fname; 455 : PICINFO *pinfo; 456 : int quick; 457 :{ 458 : /* returns '1' on success, '0' on failure */ 459 : 460 : struct jpeg_decompress_struct cinfo; 461 : struct jpeg_progress_mgr prog; 462 : struct my_error_mgr jerr; 463 : JSAMPROW rowptr[1]; 464 : FILE *fp; 465 : static byte *pic; 466 : long filesize; 467 : int i,w,h,bperpix; 468 : 469 : 470 : fbasename = BaseName(fname); 471 : pic = (byte *) NULL; 472 : comment = (char *) NULL; 473 : 474 : pinfo->type = PIC8; 475 : 476 : if ((fp = xv_fopen(fname, "r")) == NULL) return 0; 477 : 478 : fseek(fp, 0L, 2); 479 : filesize = ftell(fp); 480 : fseek(fp, 0L, 0); 481 : 482 : 483 : cinfo.err = jpeg_std_error(&jerr.pub); 484 : jerr.pub.error_exit = xv_error_exit; 485 : jerr.pub.output_message = xv_error_output; 486 : 487 : if (setjmp(jerr.setjmp_buffer)) { 488 : /* if we're here, it blowed up... */ 489 : jpeg_destroy_decompress(&cinfo); 490 : fclose(fp); 491 : if (pic) free(pic); 492 : if (comment) free(comment); 493 : 494 : pinfo->pic = (byte *) NULL; 495 : pinfo->comment = (char *) NULL; 496 : 497 : return 0; 498 : } 499 : 500 : 501 : jpeg_create_decompress(&cinfo); 502 : jpeg_set_marker_processor(&cinfo, JPEG_COM, xv_process_comment); 503 : 504 : /* hook up progress meter */ 505 : prog.progress_monitor = xv_prog_meter; 506 : cinfo.progress = &prog; 507 : 508 : jpeg_stdio_src(&cinfo, fp); 509 : (void) jpeg_read_header(&cinfo, TRUE); 510 : 511 : 512 : 513 : /* do various cleverness regarding decompression parameters & such... */ 514 : 515 : 516 : 517 : jpeg_calc_output_dimensions(&cinfo); 518 : w = cinfo.output_width; 519 : h = cinfo.output_height; 520 : pinfo->normw = w; pinfo->normh = h; 521 : 522 : if (quick) { 523 : int wfac, hfac, fac; 524 : wfac = w / QUICKWIDE; 525 : hfac = h / QUICKHIGH; 526 : 527 : fac = wfac; if (fac > hfac) fac = hfac; 528 : if (fac > 8) fac = 8; 529 : else if (fac > 4) fac = 4; 530 : else if (fac > 2) fac = 2; 531 : else fac = 1; 532 : 533 : cinfo.scale_num = 1; 534 : cinfo.scale_denom = fac; 535 : cinfo.dct_method = JDCT_FASTEST; 536 : cinfo.do_fancy_upsampling = FALSE; 537 : 538 : pinfo->normw = w; pinfo->normh = h; 539 : 540 : jpeg_calc_output_dimensions(&cinfo); 541 : w = cinfo.output_width; 542 : h = cinfo.output_height; 543 : } 544 : 545 : 546 : if (cinfo.jpeg_color_space == JCS_GRAYSCALE) { 547 : cinfo.out_color_space = JCS_GRAYSCALE; 548 : cinfo.quantize_colors = FALSE; 549 : 550 : SetISTR(ISTR_INFO,"Loading %dx%d Greyscale JPEG (%ld bytes)...", 551 : w,h,filesize); 552 : 553 : for (i=0; i<256; i++) pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i; 554 : } 555 : else { 556 : cinfo.out_color_space = JCS_RGB; 557 : cinfo.quantize_colors = FALSE; /* default: give 24-bit image to XV */ 558 : 559 : if (!quick && picType==PIC8 && conv24MB.flags[CONV24_LOCK] == 1) { 560 : /* 561 : * we're locked into 8-bit mode: 562 : * if CONV24_FAST, use JPEG's one-pass quantizer 563 : * if CONV24_SLOW, use JPEG's two-pass quantizer 564 : * if CONV24_BEST, or other, ask for 24-bit image and hand it to XV 565 : */ 566 : 567 : cinfo.desired_number_of_colors = 256; 568 : 569 : if (conv24 == CONV24_FAST || conv24 == CONV24_SLOW) { 570 : cinfo.quantize_colors = TRUE; 571 : state824=1; /* image was converted from 24 to 8 bits */ 572 : 573 : cinfo.two_pass_quantize = (conv24 == CONV24_SLOW); 574 : } 575 : } 576 : 577 : SetISTR(ISTR_INFO,"Loading %dx%d Color JPEG (%ld bytes)...", 578 : w,h,filesize); 579 : } 580 : 581 : jpeg_calc_output_dimensions(&cinfo); /* note colorspace changes... */ 582 : 583 : 584 : if (cinfo.output_components != 1 && cinfo.output_components != 3) { 585 : SetISTR(ISTR_WARNING, "%s: can't read %d-plane JPEG file!", 586 : fbasename, cinfo.output_components); 587 : jpeg_destroy_decompress(&cinfo); 588 : fclose(fp); 589 : if (comment) free(comment); 590 : return 0; 591 : } 592 : 593 : 594 : bperpix = cinfo.output_components; 595 : pinfo->type = (bperpix == 1) ? PIC8 : PIC24; 596 : 597 : pic = (byte *) malloc((size_t) (w * h * bperpix)); 598 : if (!pic) { 599 : SetISTR(ISTR_WARNING, "%s: can't read JPEG file - out of memory", 600 : fbasename); 601 : jpeg_destroy_decompress(&cinfo); 602 : fclose(fp); 603 : if (comment) free(comment); 604 : return 0; 605 : } 606 : 607 : jpeg_start_decompress(&cinfo); 608 : 609 : while (cinfo.output_scanline < cinfo.output_height) { 610 : rowptr[0] = (JSAMPROW) &pic[cinfo.output_scanline * w * bperpix]; 611 : (void) jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1); 612 : } 613 : 614 : 615 : 616 : /* return 'PICINFO' structure to XV */ 617 : 618 : pinfo->pic = pic; 619 : pinfo->w = w; 620 : pinfo->h = h; 621 : pinfo->frmType = F_JPEG; 622 : 623 : if (cinfo.out_color_space == JCS_GRAYSCALE) { 624 : sprintf(pinfo->fullInfo, "Greyscale JPEG. (%ld bytes)", filesize); 625 : pinfo->colType = F_GREYSCALE; 626 : 627 : for (i=0; i<256; i++) pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i; 628 : } 629 : else { 630 : sprintf(pinfo->fullInfo, "Color JPEG. (%ld bytes)", filesize); 631 : pinfo->colType = F_FULLCOLOR; 632 : 633 : if (cinfo.quantize_colors) { 634 : for (i=0; i<cinfo.actual_number_of_colors; i++) { 635 : pinfo->r[i] = cinfo.colormap[0][i]; 636 : pinfo->g[i] = cinfo.colormap[1][i]; 637 : pinfo->b[i] = cinfo.colormap[2][i]; 638 : } 639 : } 640 : } 641 : 642 : sprintf(pinfo->shrtInfo, "%dx%d %s JPEG. ", w,h, 643 : (cinfo.out_color_space == JCS_GRAYSCALE) ? "Greyscale " : "Color "); 644 : 645 : pinfo->comment = comment; 646 : 647 : jpeg_finish_decompress(&cinfo); 648 : jpeg_destroy_decompress(&cinfo); 649 : fclose(fp); 650 : 651 : comment = (char *) NULL; 652 : return 1; 653 :} 654 :
We would rather love to use cinfo(460 : struct jpeg_decompress_struct cinfo;).