home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / BCBViewer / JpegConfig.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-27  |  5.3 KB  |  200 lines

  1. //
  2. // Copyright (c) 1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // Permission to use, redistribute, and copy this file is granted
  9. // without a fee so long as as the following conditions are adhered to:
  10. //
  11. // o The user assumes all risk for using this software. The authors of this
  12. //   software shall be liable for no damages of any kind.
  13. //
  14. // o If the source code is distributed then this copyright notice must
  15. //   remain unaltered and any modification must be noted.
  16. //
  17. // o If this code is shipped in binary format the accompanying documentation
  18. //   should state that "this software is based, in part, on the work of
  19. //   Colosseum Builders, Inc."
  20. //
  21.  
  22. //
  23. //  Title:  Sample Image Viewer/Format Conversion Application
  24. //
  25. //  Author:  John M. Miano miano@colosseumbuilders.com
  26. //
  27. //----------------------------------------------------------------------------
  28. #include <vcl.h>
  29. #pragma hdrstop
  30.  
  31. #include "JpegConfig.h"
  32. //----------------------------------------------------------------------------
  33. #pragma resource "*.dfm"
  34. TJPEGOptions *JPEGOptions;
  35. //----------------------------------------------------------------------------
  36. __fastcall TJPEGOptions::TJPEGOptions(TComponent *Owner)
  37.     : TForm(Owner)
  38. {
  39. }
  40. //----------------------------------------------------------------------------
  41. void __fastcall TJPEGOptions::SequentialButtonClick(TObject *Sender)
  42. {
  43.   SequentialSheet->TabVisible = true ;
  44.   ProgressiveSheet->TabVisible = false ;
  45.   if (PageControl->ActivePage == ProgressiveSheet)
  46.     PageControl->ActivePage = SequentialSheet ;
  47.   return ;
  48. }
  49. //---------------------------------------------------------------------------
  50.  
  51. void __fastcall TJPEGOptions::ProgresiveButtonClick(TObject *Sender)
  52. {
  53.   ProgressiveSheet->TabVisible = true ;
  54.   SequentialSheet->TabVisible = false ;
  55.   if (PageControl->ActivePage == SequentialSheet)
  56.     PageControl->ActivePage = ProgressiveSheet ;
  57.   return ;
  58. }
  59. //---------------------------------------------------------------------------
  60.  
  61. void TJPEGOptions::WriteSettings (JpegEncoder &encoder)
  62. {
  63.   int quality ;
  64.   QualityValue->Text = QualityValue->Text.Trim () ;
  65.   try
  66.   {
  67.     quality = QualityValue->Text.ToInt () ;
  68.   }
  69.   catch (...)
  70.   {
  71.     throw Exception ("Invalid Quality Value") ;
  72.   }
  73.   if (quality < 1 || quality > 100)
  74.     throw Exception ("Invalid Quality Value") ;
  75.  
  76.   TEdit *sss [5] = { SS2, SS3, SS4, SS5, SS6 } ;
  77.   TEdit *ssa [7] = { SA1, SA2, SA3, SA4, SA5, SA6, SA7 } ;
  78.   unsigned int ssv [7] ;
  79.   unsigned int sav [7] ;
  80.   if (SequentialButton->Checked)
  81.   {
  82.     encoder.SetProgressive (false) ;
  83.   }
  84.   else
  85.   {
  86.     int last = 0 ;
  87.     ssv [0] = 0 ;
  88.     ssv [6] = 63 ;
  89.     for (unsigned int ii = 0 ; ii < 5 ; ++ ii)
  90.     {
  91.       if (last == 63)
  92.       {
  93.         sss [ii]->Text = "" ;
  94.       }
  95.       else
  96.       {
  97.         String sv = sss [ii]->Text.Trim () ;
  98.         sss [ii]->Text = sv ;
  99.         int iv ;
  100.         try
  101.         {
  102.           iv = sv.ToInt () ;
  103.         }
  104.         catch (...)
  105.         {
  106.           ModalResult = mrNone ;
  107.           throw ;
  108.         }
  109.         if (iv <= last || iv > 63)
  110.         {
  111.           sss [ii]->SetFocus () ;
  112.           ModalResult = mrNone ;
  113.           throw Exception ("Invalid Spectral Range") ;
  114.         }
  115.         last = iv ;
  116.         ssv [ii + 1] = iv ;
  117.       }
  118.     }
  119.     for (unsigned int ii = 0 ; ii < 7 ; ++ ii)
  120.     {
  121.       String sv = ssa [ii]->Text.Trim () ;
  122.       ssa [ii]->Text = sv ;
  123.  
  124.       int iv ;
  125.       try
  126.       {
  127.         iv = sv.ToInt () ;
  128.       }
  129.       catch (...)
  130.       {
  131.         ModalResult = mrNone ;
  132.         throw ;
  133.       }
  134.       if (iv > 13 || iv < 0)
  135.       {
  136.         ModalResult = mrNone ;
  137.         throw Exception ("Invalid Successive Approximation") ;
  138.       }
  139.       sav [ii] = iv ;
  140.     }
  141.   }
  142.  
  143.   if (SequentialButton->Checked)
  144.   {
  145.     encoder.SetProgressive (false) ;
  146.     int one = 0, two = 0, three = 0 ;
  147.     if (Scan1Y->Checked)
  148.       one = 1 << 1 ;
  149.     else if (Scan2Y->Checked)
  150.       two = 1 << 1 ;
  151.     else
  152.       three = 1 << 1 ;
  153.     if (Scan1Cb->Checked)
  154.       one |= 1 << 2 ;
  155.     else if (Scan2Cb->Checked)
  156.       two |= 1 << 2 ;
  157.     else
  158.       three |= 1 << 2 ;
  159.     if (Scan1Cr->Checked)
  160.       one |= 1 << 3 ;
  161.     else if (Scan2Cr->Checked)
  162.       two |= 1 << 3 ;
  163.     else
  164.       three |= 1 << 3 ;
  165.  
  166.     encoder.SetScanAttributes (0, one, 0, 0) ;
  167.     encoder.SetScanAttributes (1, two, 0, 0) ;
  168.     encoder.SetScanAttributes (2, three, 0, 0) ;
  169.   }
  170.   else
  171.   {
  172.     encoder.SetProgressive (true) ;
  173.     for (unsigned int ii = 0 ; ii < 7 ; ++ ii)
  174.     {
  175.       encoder.SetScanAttributes (3*ii, 1 << 1, ssv [ii], sav [ii]) ;
  176.       encoder.SetScanAttributes (3*ii+1, 1 << 2, ssv [ii], sav [ii]) ;
  177.       encoder.SetScanAttributes (3*ii+2, 1 << 3, ssv [ii], sav [ii]) ;
  178.       if (ssv [ii] == 63)
  179.         break ;
  180.     }
  181.   }
  182.   encoder.SetQuality (quality) ;
  183.   encoder.SetGrayscale (Grayscale->Checked) ;
  184.   return ;
  185. }
  186.  
  187. int TJPEGOptions::ShowModal (JpegEncoder &je)
  188. {
  189.   encoder = &je ;
  190.   return TForm::ShowModal () ;
  191. }
  192.  
  193. void __fastcall TJPEGOptions::OkButtonClick(TObject *Sender)
  194. {
  195.   WriteSettings (*encoder) ;
  196.   return ;
  197. }
  198. //---------------------------------------------------------------------------
  199.  
  200.