Subversion Repositories filter_foundry

Rev

Rev 194 | Rev 206 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of Filter Foundry, a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
  4.     Copyright (C) 2018-2019 Daniel Marschall, ViaThinkSoft
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. /* This is PLATFORM INDEPENDENT user interface code - mainly dialog logic */
  22.  
  23. #include "preview.h"
  24.  
  25. #ifdef MAC_ENV
  26.         #include <fp.h>
  27. #endif
  28. #include <math.h>
  29.  
  30. #include "PIProperties.h"
  31.  
  32. extern FilterRecordPtr gpb;
  33.  
  34. PSPixelMap preview_pmap;
  35. PSPixelMask preview_pmask;
  36. Handle preview_handle;
  37. UIRECT preview_rect;
  38. int preview_w,preview_h,previewerr = false,needall = false,needinput = true;
  39. Point preview_scroll;
  40. Boolean preview_complete = false;
  41. double zoomfactor,fitzoom;
  42.  
  43. Boolean setup_preview(FilterRecordPtr pb, int nplanes){
  44.         double zh,zv;
  45.  
  46.         if(pb->displayPixels && pb->advanceState){
  47.                 // Possibility 1: Only the part of the preview rect is filled with background color,
  48.                 // which can be occupied by image data if zoom factor becomes 100%
  49.                 /*
  50.                 preview_w = MIN(preview_rect.right - preview_rect.left,
  51.                                                 pb->filterRect.right - pb->filterRect.left);
  52.                 preview_h = MIN(preview_rect.bottom - preview_rect.top,
  53.                                                 pb->filterRect.bottom - pb->filterRect.top);
  54.                 */
  55.                 // Possibility 2: The whole preview rect is always filled with the background color,
  56.                 // so you can easily see what is the preview area and what is not
  57.                 preview_w = preview_rect.right - preview_rect.left;
  58.                 preview_h = preview_rect.bottom - preview_rect.top;
  59.  
  60.                 zh = (pb->filterRect.right - pb->filterRect.left)/(double)preview_w;
  61.                 zv = (pb->filterRect.bottom - pb->filterRect.top)/(double)preview_h;
  62.                 fitzoom = zh > zv ? zh : zv;
  63.  
  64.                 preview_pmap.version = 1;
  65.                 preview_pmap.bounds.left = preview_pmap.bounds.top = 0;
  66.                 preview_pmap.bounds.right = preview_w;
  67.                 preview_pmap.bounds.bottom = preview_h;
  68.                 preview_pmap.imageMode = pb->imageMode;
  69.                 preview_pmap.rowBytes = nplanes*preview_w;
  70.                 preview_pmap.colBytes = nplanes;
  71.                 preview_pmap.planeBytes = 1; /*interleaved*/
  72.         //      preview_pmap.baseAddr = preview_data;
  73.         /* baseAddr must be set before using pixelmap */
  74.  
  75.                 //---------------------------------------------------------------------------
  76.                 // Fields new in version 1:
  77.                 //---------------------------------------------------------------------------
  78.                 preview_pmap.mat = NULL;
  79.  
  80.                 if( (pb->imageMode == plugInModeRGBColor && nplanes == 4)
  81.                  || (pb->imageMode == plugInModeLabColor && nplanes == 4)
  82.                  || (pb->imageMode == plugInModeGrayScale && nplanes == 2)
  83.                  || (pb->imageMode == plugInModeDuotone && nplanes == 2) )
  84.                 {
  85.                         preview_pmask.next = NULL;
  86.         //              preview_pmask.maskData = preview_data+3;
  87.                         preview_pmask.rowBytes = preview_pmap.rowBytes;
  88.                         preview_pmask.colBytes = nplanes;
  89.                         preview_pmask.maskDescription = kSimplePSMask;
  90.                         preview_pmap.masks = &preview_pmask;
  91.                 }else
  92.                         preview_pmap.masks = NULL;
  93.  
  94.                 preview_handle = PINEWHANDLE((long)preview_h * preview_pmap.rowBytes);
  95.         }else
  96.                 preview_handle = NULL;
  97.         return preview_handle != NULL;
  98.  
  99.         //---------------------------------------------------------------------------
  100.         // Fields new in version 2:
  101.         //---------------------------------------------------------------------------
  102. //      preview_pmap.pixelOverlays;
  103. //      preview_pmap.colorManagementOptions;
  104. }
  105.  
  106. void dispose_preview(){
  107.         if(preview_handle){
  108.                 PIDISPOSEHANDLE(preview_handle);
  109.                 preview_handle = NULL;
  110.         }
  111. }
  112.  
  113. void* memset_bgcolor(void* ptr, size_t num) {
  114.         int i;
  115.         unsigned char* p;
  116.  
  117.         i = 0;
  118.         p = (unsigned char*)ptr;
  119.         for (i=0; i<(int)num; ++i) {
  120. #ifdef WIN_ENV
  121.                 DWORD color;
  122.  
  123.                 color = GetSysColor(COLOR_APPWORKSPACE);
  124.  
  125.                 if (gpb->imageMode == plugInModeRGBColor) {
  126.                         if (i%nplanes == 0) p[i] = GetRValue(color);
  127.                         if (i%nplanes == 1) p[i] = GetGValue(color);
  128.                         if (i%nplanes == 2) p[i] = GetBValue(color);
  129.                         if (i%nplanes == 3) p[i] = 255; // alpha channel
  130.                 } else if (gpb->imageMode == plugInModeGrayScale) {
  131.                         uint8_t r, g, b;
  132.  
  133.                         r = GetRValue(color);
  134.                         g = GetGValue(color);
  135.                         b = GetBValue(color);
  136.  
  137.                         if (i%nplanes == 0) p[i] = (uint8_t)(((299L*r)+(587L*g)+(114L*b))/1000);
  138.                         if (i%nplanes == 1) p[i] = 255; // alpha channel
  139.                 } else if (gpb->imageMode == plugInModeCMYKColor) {
  140.                         uint8_t r, g, b;
  141.                         double dmax, dr, dg, db, k, c, m, y;
  142.  
  143.                         r = GetRValue(color);
  144.                         g = GetGValue(color);
  145.                         b = GetBValue(color);
  146.  
  147.                         dr = (double)r / 255;
  148.                         dg = (double)g / 255;
  149.                         db = (double)b / 255;
  150.  
  151.                         dmax = dr;
  152.                         if (dg>dmax) dmax = dg;
  153.                         if (db>dmax) dmax = db;
  154.  
  155.                         k = 1 - dmax;
  156.                         c = (1 - dr - k) / (1 - k);
  157.                         m = (1 - dg - k) / (1 - k);
  158.                         y = (1 - db - k) / (1 - k);
  159.  
  160.                         if (i%nplanes == 0) p[i] = (uint8_t)(255 - c * 255);
  161.                         if (i%nplanes == 1) p[i] = (uint8_t)(255 - m * 255);
  162.                         if (i%nplanes == 2) p[i] = (uint8_t)(255 - y * 255);
  163.                         if (i%nplanes == 3) p[i] = (uint8_t)(255 - k * 255);
  164.                 } else {
  165.                         // FIXME: If we are in such a non supported color mode, then
  166.                         //        these color codes would be all wrong!
  167.                         //        Just to be safe use (what is probably) white
  168.                         p[i] = 0xFF;
  169.  
  170.                         /*
  171.                         #define plugInModeBitmap                        0
  172.                         #define plugInModeGrayScale                     1 supported
  173.                         #define plugInModeIndexedColor          2
  174.                         #define plugInModeRGBColor                      3 supported
  175.                         #define plugInModeCMYKColor                     4 supported
  176.                         #define plugInModeHSLColor                      5
  177.                         #define plugInModeHSBColor                      6
  178.                         #define plugInModeMultichannel          7
  179.                         #define plugInModeDuotone                       8
  180.                         #define plugInModeLabColor                      9
  181.                         #define plugInModeGray16                        10
  182.                         #define plugInModeRGB48                         11
  183.                         #define plugInModeLab48                         12
  184.                         #define plugInModeCMYK64                        13
  185.                         #define plugInModeDeepMultichannel      14
  186.                         #define plugInModeDuotone16                     15
  187.                         #define plugInModeRGB96                         16
  188.                         #define plugInModeGray32                        17
  189.                         */
  190.  
  191.                 }
  192. #else
  193.                 // This is the behavior of FilterFoundry <1.7 was this (filled with 0xFF)
  194.                 // FIXME: Should we do something fancy here, too?
  195.                 p[i] = 0xFF;
  196. #endif
  197.         }
  198.         return ptr;
  199. }
  200.  
  201. void recalc_preview(FilterRecordPtr pb,DIALOGREF dp){
  202.         OSErr e;
  203.         double scaledw, scaledh;
  204.         int j,n,imgw,imgh;
  205.         Rect r,outRect;
  206.         Ptr outrow;
  207.  
  208.         preview_complete = false;
  209.  
  210.         if(preview_handle){
  211.                 /* size of previewed area, of source image; but no larger than filtered area (selection) */
  212.                 scaledw = zoomfactor*preview_w;
  213.                 if(scaledw > (pb->filterRect.right - pb->filterRect.left))
  214.                         scaledw = pb->filterRect.right - pb->filterRect.left;
  215.                 scaledh = zoomfactor*preview_h;
  216.                 if(scaledh > (pb->filterRect.bottom - pb->filterRect.top))
  217.                         scaledh = pb->filterRect.bottom - pb->filterRect.top;
  218.  
  219.                 /* scale clipped preview area down again - this becomes the pixel size of preview */
  220.                 imgw = (int)ceil(scaledw/zoomfactor);
  221.                 if(imgw > preview_w)
  222.                         imgw = preview_w;
  223.                 imgh = (int)ceil(scaledh/zoomfactor);
  224.                 if(imgh > preview_h)
  225.                         imgh = preview_h;
  226.  
  227.                 /* compute source data rectangle (inRect) */
  228.  
  229.                 /* centre preview on filtered part of input image, adding scroll offset */
  230.                 r.left = (int16)((pb->filterRect.left + pb->filterRect.right - scaledw)/2 + preview_scroll.h);
  231.                 /* make sure it does not go outside the input area */
  232.                 if(r.left < pb->filterRect.left) {
  233.                         preview_scroll.h += pb->filterRect.left - r.left;
  234.                         r.left = pb->filterRect.left;
  235.                 }
  236.                 else if(r.left+scaledw > pb->filterRect.right) {
  237.                         preview_scroll.h += (int16)(pb->filterRect.right - (r.left+scaledw));
  238.                         r.left = (int16)(pb->filterRect.right - scaledw);
  239.                 }
  240.                 r.right = (int16)(r.left + scaledw);
  241.                 preview_pmap.maskPhaseCol = (int32)((preview_scroll.h)/zoomfactor); // phase of the checkerboard
  242.  
  243.                 /* now compute for vertical */
  244.                 r.top = (int16)((pb->filterRect.top + pb->filterRect.bottom - scaledh)/2 + preview_scroll.v);
  245.                 if(r.top < pb->filterRect.top) {
  246.                         preview_scroll.v += pb->filterRect.top - r.top;
  247.                         r.top = pb->filterRect.top;
  248.                 }
  249.                 else if(r.top+scaledh > pb->filterRect.bottom) {
  250.                         preview_scroll.v += (int16)(pb->filterRect.bottom - (r.top+scaledh));
  251.                         r.top = (int16)(pb->filterRect.bottom - scaledh);
  252.                 }
  253.                 r.bottom = (int16)(r.top + scaledh);
  254.                 preview_pmap.maskPhaseRow = (int32)((preview_scroll.v)/zoomfactor); // phase of the checkerboard
  255.  
  256.                 /* if formulae need random access to image - src(), rad() - we must request entire area: */
  257.                 if(needall)
  258.                         SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
  259.                 else
  260.                         pb->inRect = r;
  261.  
  262.                 pb->outRect = pb->inRect;
  263.                 SETRECT(pb->maskRect,0,0,0,0);
  264.                 pb->inLoPlane = pb->outLoPlane = 0;
  265.                 pb->inHiPlane = pb->outHiPlane = nplanes-1;
  266.  
  267.                 if( !needinput || !(e = pb->advanceState()) ){
  268.                         Ptr outptr = PILOCKHANDLE(preview_handle,false);
  269.                         int blankrows = (preview_h - imgh)/2,
  270.                                 blankcols = (preview_w - imgw)/2,
  271.                                 pmrb = preview_pmap.rowBytes;
  272.  
  273.                         evalinit();
  274.  
  275.                         SETRECT(outRect,0,0,imgw,imgh);
  276.  
  277.                         e = process_scaled(pb, false, &r, &outRect,
  278.                                         outptr + pmrb*blankrows + nplanes*blankcols, pmrb, zoomfactor);
  279.                         if(blankrows){
  280.                                 // blank rows on top of preview:
  281.                                 memset_bgcolor(outptr, pmrb*blankrows);
  282.                                 // blank rows below preview:
  283.                                 n = preview_h - blankrows - imgh;
  284.                                 memset_bgcolor(outptr + pmrb*(blankrows+imgh), pmrb*n);
  285.                         }
  286.                         if(blankcols){
  287.                                 n = preview_w - blankcols - imgw;
  288.                                 outrow = outptr + pmrb*blankrows;
  289.                                 for(j = blankrows; j < preview_h - blankrows; ++j){
  290.                                         // blank columns on left side of preview (if picture is smaller than the preview area):
  291.                                         memset_bgcolor(outrow, nplanes*blankcols);
  292.                                         // blank columns on right side of preview (if picture is smaller than the preview area):
  293.                                         memset_bgcolor(outrow + nplanes*(blankcols+imgw), nplanes*n);
  294.                                         outrow += pmrb;
  295.                                 }
  296.                         }
  297.  
  298.                         if(!e){
  299.                                 preview_complete = true;
  300.  
  301. #ifdef WIN_ENV
  302.                                 {
  303.                                 extern HWND preview_hwnd;
  304.                                 HDC hdc = GetDC(preview_hwnd);
  305.  
  306.                                 drawpreview(dp,hdc,outptr);
  307.  
  308.                                 ReleaseDC(preview_hwnd,hdc);
  309.                                 }
  310. #else
  311.                                 {
  312.                                 GrafPtr saveport;
  313.  
  314.                                 GetPort(&saveport);
  315.                                 SetPortDialogPort(dp);
  316.  
  317.                                 drawpreview(dp,NULL,outptr);
  318.  
  319.                                 SetPort(saveport);
  320.                                 }
  321. #endif
  322.                         }
  323.  
  324.                         PIUNLOCKHANDLE(preview_handle);
  325.                 }
  326.  
  327.                 if(e && !previewerr){
  328.                         alertuser(_strdup("Could not build preview at chosen zoom level."),
  329.                                 e == memFullErr && !needall ? _strdup("The image is too large for available memory. Try zooming in.\nIf that does not help, cancel and retry the filter.") : _strdup(""));
  330.                         previewerr = true;
  331.                 }
  332.  
  333.         }
  334. }
  335.  
  336. OSErr drawpreview(DIALOGREF dp,void *hdc,Ptr imageptr){
  337.         intptr_t watchsusp;
  338.         OSErr e = noErr;
  339.         VRect srcRect;
  340.         UIRECT imagebounds;
  341.  
  342.         if(preview_handle && preview_complete){
  343.  
  344.                 srcRect = preview_pmap.bounds;
  345.  
  346.                 imagebounds.left = (preview_rect.left + preview_rect.right - preview_w)/2;
  347.                 imagebounds.top = (preview_rect.top + preview_rect.bottom - preview_h)/2;
  348.                 imagebounds.right = imagebounds.left + preview_w;
  349.                 imagebounds.bottom = imagebounds.top + preview_h;
  350.  
  351.                 preview_pmap.baseAddr = imageptr;
  352.                 preview_pmask.maskData = imageptr+3; // FIXME: is this offset correct for all modes?!
  353.  
  354.                 if(gpb->propertyProcs->getPropertyProc){
  355.                         gpb->propertyProcs->getPropertyProc(kPhotoshopSignature,propWatchSuspension,0,&watchsusp,NULL);
  356.                         gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp+1,NULL);
  357.                 }
  358.  
  359.                 e = gpb->displayPixels(&preview_pmap,&srcRect,imagebounds.top,imagebounds.left,hdc);
  360.  
  361.                 if(gpb->propertyProcs->getPropertyProc)
  362.                         gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp,NULL);
  363.         }
  364.         return e;
  365. }
  366.