home *** CD-ROM | disk | FTP | other *** search
- //
- // Copyright (c) 1998 Colosseum Builders, Inc.
- // All rights reserved.
- //
- // Colosseum Builders, Inc. makes no warranty, expressed or implied
- // with regards to this software. It is provided as is.
- //
- // Permission to use, redistribute, and copy this file is granted
- // without a fee so long as as the following conditions are adhered to:
- //
- // o The user assumes all risk for using this software. The authors of this
- // software shall be liable for no damages of any kind.
- //
- // o If the source code is distributed then this copyright notice must
- // remain unaltered and any modification must be noted.
- //
- // o If this code is shipped in binary format the accompanying documentation
- // should state that "this software is based, in part, on the work of
- // Colosseum Builders, Inc."
- //
-
- //
- // Title: Sample Image Viewer/Format Conversion Application
- //
- // Author: John M. Miano miano@colosseumbuilders.com
- //
- // Description:
- //
- // MDI Child Class
- //
- //---------------------------------------------------------------------
- #include <vcl.h>
-
- #include <fstream>
- #pragma hdrstop
-
- #include "ChildWin.h"
- #include "main.h"
-
- //---------------------------------------------------------------------
- #pragma resource "*.dfm"
-
- static void ProgressFunction (BitmapImageCoder &coder,
- void *data,
- unsigned int currentpass,
- unsigned int passcount,
- unsigned int progress,
- bool &cancel)
- {
- MainForm->ProgressBar->Position = (100 * (currentpass - 1) + progress)
- / passcount ;
- if (progress == 100 && MainForm->ShowProgressive)
- {
- coder.UpdateImage () ;
- TMDIChild *child = (TMDIChild *) data ;
- child->image_type = JpegImage ;
- child->PaintBox->SetBounds (0, 0, child->image.Width (),
- child->image.Height ()) ;
- child->LoadBitmapInfoHeader () ;
- child->PaintBox->Refresh () ;
- child->PaintBox->Update () ;
- }
- Application->ProcessMessages () ;
- return ;
- }
-
- void ImageProgressFunction (BitmapImage &image,
- void *data,
- unsigned int currentpass,
- unsigned int passcount,
- unsigned int progress,
- bool &cancel)
- {
- MainForm->ProgressBar->Position = (100 * (currentpass - 1) + progress)
- / passcount ;
- Application->ProcessMessages () ;
- return ;
- }
-
- //---------------------------------------------------------------------
- __fastcall TMDIChild::TMDIChild(TComponent *Owner)
- : TForm(Owner)
- {
- image_type = UnknownImage ;
- gamma_value = 1.0 ;
- return ;
- }
- //---------------------------------------------------------------------
- void __fastcall TMDIChild::FormClose(TObject *Sender, TCloseAction &Action)
- {
- Action = caFree;
- return ;
- }
- //---------------------------------------------------------------------
- void TMDIChild::ReadImage (const String &filename)
- {
- MainForm->ShowProgressBar () ;
- Cursor = crHourGlass ;
- Caption = filename ;
- image_type = UnknownImage ;
- ifstream strm (filename.c_str (), ios::binary) ;
- if (! strm)
- {
- Caption = "" ;
- Cursor = crDefault ;
- throw Exception (String ("Can't open file ") + filename) ;
- }
-
- try
- {
- image_type = ::ReadImage (strm, image, ProgressFunction, (void*) this) ;
- }
- catch (EGraphicsException &ee)
- {
- Caption = "" ;
- Cursor = crDefault ;
- MainForm->ProgressBar->Visible = false ;
- throw Exception (ee.what ()) ;
- }
-
- // If we have a 24-bit image that is being displayed on an 8-bit
- // (or fewer) display then we quantize the colors. From there we
- // allow the system to do the color manipultions (e.g. we do not do
- // quantization for 24-bit images on a 16-bit display).
- HDC dc = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
- int caps = GetDeviceCaps (dc, BITSPIXEL) ;
- if (caps <= 8 && image.BitCount () > 8)
- {
- BitmapImage newimage ;
- image.SetProgressFunction (ImageProgressFunction, NULL) ;
- newimage.EightBitQuantization (image) ;
- image = newimage ;
- }
- DeleteDC (dc) ;
-
- LoadBitmapInfoHeader () ;
-
- PaintBox->SetBounds (0, 0, image.Width (), image.Height ()) ;
- MainForm->ProgressBar->Visible = false ;
- Cursor = crDefault ;
- return ;
- }
-
-
- void __fastcall TMDIChild::PaintBoxPaint(TObject *Sender)
- {
- if (image_type == UnknownImage)
- return ;
-
- SetDIBitsToDevice (PaintBox->Canvas->Handle,
- 0,
- 0,
- image.Width (),
- image.Height (),
- 0,
- 0,
- 0,
- image.Height (),
- image.ImageData (),
- (BITMAPINFO *) bitinfobuffer,
- DIB_RGB_COLORS) ;
- return ;
- }
- //---------------------------------------------------------------------------
-
-
- void TMDIChild::LoadBitmapInfoHeader ()
- {
- BITMAPINFO *bitmapinfo = (BITMAPINFO *) bitinfobuffer ;
- bitmapinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
- bitmapinfo->bmiHeader.biWidth = image.Width () ;
- bitmapinfo->bmiHeader.biHeight = image.Height () ;
- bitmapinfo->bmiHeader.biPlanes = 1 ;
- bitmapinfo->bmiHeader.biBitCount = (WORD) image.BitCount () ;
- bitmapinfo->bmiHeader.biCompression = BI_RGB ;
- bitmapinfo->bmiHeader.biSizeImage = 0 ;
- bitmapinfo->bmiHeader.biXPelsPerMeter = 0 ;
- bitmapinfo->bmiHeader.biYPelsPerMeter = 0 ;
- bitmapinfo->bmiHeader.biClrUsed = 0 ;
- bitmapinfo->bmiHeader.biClrImportant = 0 ;
-
- for (unsigned int ii = 0 ; ii < image.ColorCount () ; ++ ii)
- {
- bitmapinfo->bmiColors [ii].rgbRed = image.ColorMap (ii).red ;
- bitmapinfo->bmiColors [ii].rgbGreen = image.ColorMap (ii).green ;
- bitmapinfo->bmiColors [ii].rgbBlue = image.ColorMap (ii).blue ;
- bitmapinfo->bmiColors [ii].rgbReserved = 0 ;
- }
- return ;
- }
-
- void __fastcall TMDIChild::SetGammaValue (double value)
- {
- double adjust = value / gamma_value ;
- Cursor = crHourGlass ;
- Application->ProcessMessages () ;
- image.GammaCorrect (adjust) ;
- gamma_value = value ;
- LoadBitmapInfoHeader () ;
- PaintBox->Refresh () ;
- Cursor = crDefault ;
- return ;
- }
-
- void TMDIChild::Grayscale ()
- {
- Cursor = crHourGlass ;
- Application->ProcessMessages () ;
- image.ToGrayscale () ;
- LoadBitmapInfoHeader () ;
- PaintBox->Refresh () ;
- Cursor = crDefault ;
- return ;
- }
-
- void TMDIChild::CopyToClipboard ()
- {
- // The clipboard format for a Device Independent Bitmap
- // is a BITMAPINFOHEADER followed by the image data.
- unsigned int headersize = sizeof (BITMAPINFOHEADER) ;
- if (image.BitCount () != 24)
- headersize += (1 << image.BitCount ()) * sizeof (RGBQUAD) ;
- unsigned int datasize = image.BytesPerRow () * image.Height () ;
-
- // Allocate a global memory block for the iamge.
- HANDLE hmem = GlobalAlloc (GMEM_MOVEABLE, headersize + datasize) ;
- if (hmem == NULL)
- throw Exception ("Cannot Allocate Global Memory for Clipboard") ;
- char *data = (char *) GlobalLock (hmem) ;
-
- // We have already created a BITMAPINFOHEADER structure for the
- // image in order to display it. Here we copy that structure into
- // the buffer followed by the image data.
- memcpy (data, bitinfobuffer, headersize) ;
- memcpy (&data [headersize], image.ImageData (), datasize) ;
-
- // Now we write the image to the clipboard. This transfers ownership
- // of the global block to the system.
- bool status = OpenClipboard (Application->Handle) ;
- if (! status)
- {
- GlobalFree (hmem) ;
- throw Exception ("Cannot Open the Clipboard") ;
- }
- HANDLE clipboard = SetClipboardData (CF_DIB, hmem) ;
- CloseClipboard () ;
- // Cleanup.
- GlobalUnlock (hmem) ;
- return ;
- }
-
- void TMDIChild::CopyFromClipboard ()
- {
- OpenClipboard (Application->Handle) ;
- HANDLE hmem = GetClipboardData (CF_DIB) ;
- if (hmem == NULL)
- {
- CloseClipboard () ;
- return ;
- }
-
- char *buffer = (char *) GlobalLock (hmem) ;
- BITMAPINFOHEADER *header = (BITMAPINFOHEADER *) buffer ;
-
- unsigned int colorcount ;
- if (header->biBitCount < 24)
- colorcount = 1 << header->biBitCount ;
- else
- colorcount = 0 ;
- image.SetSize (colorcount,
- header->biBitCount,
- header->biWidth,
- header->biHeight) ;
- BITMAPINFO *info = (BITMAPINFO *) buffer ;
- if (image.BitCount () != 24)
- {
- for (unsigned int ii = 0 ; ii < colorcount ; ++ ii)
- {
- image.ColorMap (ii).red = info->bmiColors [ii].rgbRed ;
- image.ColorMap (ii).green = info->bmiColors [ii].rgbGreen ;
- image.ColorMap (ii).blue = info->bmiColors [ii].rgbBlue ;
- }
- }
- char *imagedata = &buffer [sizeof (BITMAPINFOHEADER)
- + colorcount * sizeof (RGBQUAD)] ;
- unsigned int datasize = image.BytesPerRow () * image.Height () ;
- memcpy (image.ImageData (), imagedata, datasize) ;
- CloseClipboard () ;
- GlobalFree (hmem) ;
- LoadBitmapInfoHeader () ;
- image_type = BmpImage ;
- PaintBox->SetBounds (0, 0, image.Width (), image.Height ()) ;
-
- HDC dc = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
- int caps = GetDeviceCaps (dc, BITSPIXEL) ;
- if (caps <= 8 && image.BitCount () > 8)
- {
- BitmapImage newimage ;
- image.SetProgressFunction (ImageProgressFunction, NULL) ;
- newimage.EightBitQuantization (image) ;
- image = newimage ;
- }
- DeleteDC (dc) ;
-
- Caption = "Untitled" ;
- return ;
- }
-
-