f_main.cpp
上传用户:tongfa
上传日期:2007-01-06
资源大小:1071k
文件大小:2k
源码类别:

图片显示

开发平台:

WINDOWS

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <stdio.h>
  5. #define HAVE_BOOLEAN
  6. #include "jpglib/jpeglib.h"
  7. #include "f_main.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. #pragma resource "*.dfm"
  11. TForm1 *Form1;
  12. //---------------------------------------------------------------------------
  13. __fastcall TForm1::TForm1(TComponent* Owner)
  14. : TForm(Owner)
  15. {
  16. }
  17. //---------------------------------------------------------------------------
  18. void TForm1::ReadJPEGBitmap(const char * file_name, Graphics::TBitmap *bitmap)
  19. {
  20. jpeg_decompress_struct cinfo;
  21.     jpeg_error_mgr jerr;
  22.     FILE *input_file;
  23.     input_file = fopen(file_name, "rb");
  24.     if (input_file == NULL)
  25.      throw "Can't open file";
  26. /* Initialize the JPEG decompression object with default error handling. */
  27. cinfo.err = jpeg_std_error(&jerr);
  28. jpeg_create_decompress(&cinfo);
  29.     jpeg_stdio_src(&cinfo, input_file);
  30.     if (!jpeg_read_header(&cinfo, TRUE))
  31.      throw "Jpeg header do not match";
  32.     jpeg_start_decompress(&cinfo);
  33.     bitmap->HandleType = bmDIB;
  34.     bitmap->PixelFormat = pf24bit;
  35.     bitmap->Width = cinfo.output_width;
  36.     bitmap->Height = cinfo.output_height;
  37.     while (cinfo.output_scanline < cinfo.output_height)
  38.     {
  39.         unsigned char *scanline = (unsigned char *)bitmap->ScanLine[cinfo.output_scanline];
  40.      int num_scanlines = jpeg_read_scanlines(&cinfo, &scanline, 1);
  41.         if (num_scanlines != 1)
  42.          throw "Error while decompressing Jpeg file";
  43.         // exchange the order of R & B
  44.         for (unsigned int i=0; i<cinfo.output_width; i++)
  45.         {
  46.          unsigned char t;
  47.          t = scanline[i*3];
  48.             scanline[i*3] = scanline[i*3+2];
  49.             scanline[i*3+2] = t;
  50.         }
  51.     }
  52. jpeg_finish_decompress(&cinfo);
  53. jpeg_destroy_decompress(&cinfo);
  54. }
  55. void __fastcall TForm1::Button1Click(TObject *Sender)
  56. {
  57. if (OpenDialog1->Execute())
  58.     {
  59.      Graphics::TBitmap *bitmap = new Graphics::TBitmap;
  60.      ReadJPEGBitmap(OpenDialog1->FileName.c_str(), bitmap);
  61.         PaintBox1->Canvas->Draw(0, 0, bitmap);
  62.         delete bitmap;
  63.     }
  64. }
  65. //---------------------------------------------------------------------------