home *** CD-ROM | disk | FTP | other *** search
/ fxPAINT 1.0 / fxPAINT 1.0.iso / tools / ics / software / ics.lzh / ICS / Programmer / Example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-01  |  8.1 KB  |  185 lines

  1. /**********************************************************/
  2. /* Copyright   ©1999 Wolf-Juergen Faust                   */
  3. /* Am Dorfgarten 10                                       */
  4. /* 60435 Frankfurt     EXAMPLE.c                          */
  5. /* Germany                                                */
  6. /* Tel: +(49) 69 5486556                                  */
  7. /* Fax: +(49) 69 95409598                                 */
  8. /* email: wfaust@stud.uni-frankfurt.de                    */
  9. /*                                                        */
  10. /* This piece of source code was based on ICSConvert      */
  11. /* and shows how to call ICS for color correction.        */
  12. /* See the included comments and ICS.doc for more         */
  13. /* info when adding ICS support to your software          */
  14. /*                                                        */
  15. /* The source opens a source image file and saves         */
  16. /* the corrected values in a new destination file         */
  17. /*                                                        */
  18. /**********************************************************/
  19.  
  20. #include    <proto/ics.h>
  21.  
  22. struct Library *ICSBase=NULL;
  23.  
  24.  
  25. /*** MAIN ***/
  26. {
  27.     UWORD ret = 1;
  28.  
  29.     UWORD wi,wn,wx;
  30.     ULONG x,y, h, size, li;
  31.     struct IFFHandle *iffhandle;
  32.     UBYTE *temp;
  33.     void *idc;
  34.     void *idctrans;
  35.     uint_fast32_t oldmode;
  36.     COLOR incolor, outcolor;
  37.     UBYTE *r,*g,*b;
  38.  
  39.     if (ICSBase = OpenLibrary("ics.library",2L))  // Note first public release will be V2!
  40.     {                                        
  41.         if(idc = CreateIDC(                // create ICS device context object
  42.             ICS_APPNAME, PRGMNAME,           // name of this application
  43.             ICS_APPNAMEVERSION, NUMVERSION,  // version of this application
  44.             ICS_APPICSVERSION, 2,            // version of ICS library this application was made for/with.  Note V2 will be the first public release.
  45.             TAG_DONE))
  46.         {
  47.             if(SetupIDCColorMatching(idc, dscreen, TAG_DONE)>1)  // display ICS preference window. ICSConvert does abort if the user selects cancel (returns 1)
  48.             {
  49.                 if(oldmode = SetIDCMode(idc, ICS_ON)) // make sure ICS is enabled during CreateIDCTransform even if the current preference setting is off... in order to may enable it later. See docs about SetIDCMode().
  50.                 {
  51.                     if(idctrans = CreateIDCTransform(idc,
  52.                         ICS_TransferDevices, ICS_INPUT_DEVICE,        // We want to convert from input to display device (not printer device)
  53.                         ICS_StatusOpen, dscreen,                      // Screen to use for status window or NULL for default public screen
  54.                         ICS_StatusTitle, "ICS Image Conversion",      // Overwrite status window title with our own...
  55.                         //ICS_StatusActivate, TRUE,                     // Activate status window
  56.                         ICS_StatusKeep, TRUE,                         // keep status window open as we use the ics status window during conversion of image data later
  57.                         // ICS_StatusDisableAbort, TRUE,                 // use this to disable Abort/Stop button
  58.                         TAG_DONE)) // create input (scanner) to device (display/monitor) color corretion and show status window while processing.. leave status window open for user below...
  59.                     {
  60.                         if(SetIDCMode(idc, oldmode)) // here we reset the ICS mode to the wanted preferences or application program setting...
  61.                         {
  62.                             ICSStatusWin(idctrans, ICS_StatusText,  "Opening image file...", TAG_DONE);  // change text of status window created by ICS library during CreateIDCTransform()
  63.                             if(ret)
  64.                             {
  65.                                 if(ICSStatusWin(idctrans, ICS_StatusCheck,  0, TAG_DONE)) // check if user pressed abort in the status window
  66.                                 {
  67.                                     ret = 0;
  68.                                 }
  69.                             }
  70.                             if(ret)
  71.                             {
  72.                                 if (GetMain(ilbm, POPEN, win2, 0))  // open source image file for conversion
  73.                                 {
  74.                                     if (GetMain(ilbm, PREADINIT, win2, 0))  // init read routines for file
  75.                                     {
  76.                                         if(iffhandle = OPENMYFILE(ilbm, win2)) // open destination image file for color corrected image
  77.                                         {
  78.                                             for(ret=1,y=0; (ret) && (y<ilbm->hssize); y++)  // loop for number of lines in image
  79.                                             {
  80.                                                 // Display progress status and check for user abort...
  81.                                                 sprintf(tempstr,"Converting Image  -  %ld%% complete",(y*100)/ilbm->hssize);
  82.                                                  // change status text and update progress bar. Also check if user wants to abort... tempstr may be used after StatusWin call as ICS does make a copy of tempstr.
  83.                                                 if(ICSStatusWin(idctrans, ICS_StatusText,  tempstr, ICS_StatusProgress, (y*100)/ilbm->hssize, ICS_StatusCheck,  0, TAG_DONE))
  84.                                                 {
  85.                                                     ret = 0;
  86.                                                     break;
  87.                                                 }
  88.                                                 // read a single image line into buffer.
  89.                                                 if(temp = (UBYTE *)GetMain(ilbm, PREADLINE, win2, y))
  90.                                                 {
  91.                                                     r = temp;
  92.                                                     g = temp + ilbm->wssize;
  93.                                                     b = temp + ilbm->wssize;
  94.                                                     // color correct a single line using ICS. If ICS is disabled, this function TranslateColors() will copy in to output unchanged.
  95.                                                     for(x=0; (ret) && (x<ilbm->wssize); x++)  // loop for number of pixels in line
  96.                                                     {
  97.                                                         if(ilbm->destmode) // is source a grayscale image?
  98.                                                         {
  99.                                                             incolor.ugray.gray = (temp[x]<<8) | temp[x];  // convert 8 bit to 16 bit unsigned graylevel. IMPORTANT: Black=0   White=65535
  100.                                                             if(!(TranslateColors(idctrans, &incolor, 1, COLOR_UGRAY, &outcolor, COLOR_UGRAY)))
  101.                                                             {
  102.                                                                 temp = ICSFault(idc, "Error setting ICS mode by "PRGMNAME".");
  103.                                                                 if(!temp) temp = "Error: Can't enable or disable ICS.";
  104.                                                                 if(ICSErr(idc)>ICS_ERRMSGLIMIT) Mes(temp, NULL);
  105.                                                                 ret = 0;
  106.                                                                 break;
  107.                                                             }
  108.                                                             temp[x] = outcolor.ugray.gray >> 8;
  109.                                                         } else
  110.                                                         {
  111.                                                             incolor.urgb.red   = (r[x]<<8) | r[x];  // convert 8 bit to 16 bit unsigned RGB color
  112.                                                             incolor.urgb.green = (g[x]<<8) | g[x];
  113.                                                             incolor.urgb.blue  = (b[x]<<8) | b[x];
  114.                                                             if(!(TranslateColors(idctrans, &incolor, 1, COLOR_URGB, &outcolor, COLOR_URGB))) // Note: This just corrects a single pixel. It's much faster to correct at least a complete row!
  115.                                                                 temp = ICSFault(idc, "Error setting ICS mode by "PRGMNAME".");
  116.                                                                 if(!temp) temp = "Error: Can't enable or disable ICS.";
  117.                                                                 if(ICSErr(idc)>ICS_ERRMSGLIMIT) Mes(temp, NULL);
  118.                                                                 ret = 0;
  119.                                                                 break;
  120.                                                             }
  121.                                                             r[x] = outcolor.urgb.red >> 8;  // Convert 16 to 8 Bit - Correct would be to devide by 65535/255=257 and not 256... but we ignore the invisible fault introduced...
  122.                                                             g[x] = outcolor.urgb.green >> 8;
  123.                                                             b[x] = outcolor.urgb.blue >> 8;
  124.                                                         }
  125.                                                     }
  126.                                                     if(!(WRITEMYLINE(ilbm, win2, iffhandle, temp)))
  127.                                                     {
  128.                                                         ret = 0;
  129.                                                         break;
  130.                                                     }
  131.                                                 } else
  132.                                                 {
  133.                                                     ret = 0;
  134.                                                     break;
  135.                                                 }
  136.                                             }
  137.                                             CLOSEMYFILE(iffhandle);
  138.                                         }
  139.                                         GetMain(ilbm, PREADEND, win2, 0);
  140.                                     } else
  141.                                     {
  142.                                         ret = 0;
  143.                                     }
  144.                                     GetMain(ilbm, PCLOSE, win2, 0);
  145.                                 }
  146.                             }
  147.                         } else
  148.                         {
  149.                             temp = ICSFault(idc, "Error setting ICS mode by "PRGMNAME".");
  150.                             if(!temp) temp = "Error: Can't enable or disable ICS.";
  151.                             if(ICSErr(idc)>ICS_ERRMSGLIMIT) Mes(temp, NULL);
  152.                             ret = 0;
  153.                         }
  154.                         DeleteIDCTransform(idctrans);
  155.                     } else
  156.                     {
  157.                         temp = ICSFault(idc, "Error while running "PRGMNAME".");
  158.                         if(!temp) temp = "Error: Can't create color transformation using ics.library";
  159.                         if(ICSErr(idc)>ICS_ERRMSGLIMIT) Mes(temp, NULL);
  160.                         ret = 0;
  161.                     }
  162.                 } else
  163.                 {
  164.                     temp = ICSFault(idc, "Error enabling ICS by "PRGMNAME".");
  165.                     if(!temp) temp = "Error: Can't enable ICS.";
  166.                     if(ICSErr(idc)>ICS_ERRMSGLIMIT) Mes(temp, NULL);
  167.                     ret = 0;
  168.                 }
  169.             } else
  170.             {
  171.                 temp = ICSFault(idc, "Error while running "PRGMNAME".");
  172.                 if(!temp) temp = "Error: Can't open ICS preferences.";
  173.                 if(ICSErr(idc)>ICS_ERRMSGLIMIT) Mes(temp, NULL);
  174.                 ret = 0;
  175.             }
  176.             DeleteIDC(idc);
  177.         } else
  178.         {
  179.             Mes("Error: Can't create device context using ics.library\n      Maybe not enough memory?", win2);
  180.         }
  181.         CloseLibrary(ICSBase);
  182.     }
  183.     return(ret);
  184. }
  185.