home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 12 / MA_Cover_12.iso / devs / ics / software / ics.lzh / ICS / Programmer / Example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-22  |  7.9 KB  |  184 lines

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