Subversion Repositories filter_foundry

Rev

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