Subversion Repositories filter_foundry

Rev

Rev 45 | Rev 89 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 toby 1
/*
18 toby 2
    This file is part of Filter Foundry, a filter plugin for Adobe Photoshop
2 toby 3
    Copyright (C) 2003-5 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 "ff.h"
23
 
29 toby 24
#include "PIProperties.h"
2 toby 25
 
26
#include "node.h"
27
#include "funcs.h"
28
#include "y.tab.h"
29
#include "choosefile.h"
30
 
31
PSPixelMap preview_pmap;
32
PSPixelMask preview_pmask;
33
Handle preview_handle;
34
UIRECT preview_rect;
37 toby 35
int preview_w,preview_h,previewerr = false;
2 toby 36
Point preview_scroll;
37
Boolean preview_complete = false;
38
 
39
Boolean setup_preview(FilterRecordPtr pb){
40
 
41
        //dbg("setup_preview");
42
 
43
        if(pb->displayPixels && pb->advanceState){
44
 
45
                preview_w = MIN(preview_rect.right - preview_rect.left - 2,pb->filterRect.right - pb->filterRect.left);
46
                preview_h = MIN(preview_rect.bottom - preview_rect.top - 2,pb->filterRect.bottom - pb->filterRect.top);
47
 
48
                preview_pmap.version = 1;
49
                preview_pmap.bounds.left = preview_pmap.bounds.top = 0;
50
                preview_pmap.bounds.right = preview_w;
51
                preview_pmap.bounds.bottom = preview_h;
52
                preview_pmap.imageMode = nplanes>1 ? plugInModeRGBColor : plugInModeGrayScale;
53
                preview_pmap.rowBytes = nplanes*preview_w;
54
                preview_pmap.colBytes = nplanes;
55
                preview_pmap.planeBytes = 1; /*interleaved*/
56
        //      preview_pmap.baseAddr = preview_data;
57
        /* baseAddr must be set before using pixelmap */
58
 
59
                //---------------------------------------------------------------------------
60
                // Fields new in version 1:
61
                //---------------------------------------------------------------------------   
62
                preview_pmap.mat = NULL;
63
 
64
                if(nplanes==4){
65
                        preview_pmask.next = NULL;
66
        //              preview_pmask.maskData = preview_data+3;
67
                        preview_pmask.rowBytes = preview_pmap.rowBytes;
68
                        preview_pmask.colBytes = nplanes;
69
                        preview_pmask.maskDescription = kSimplePSMask;
70
                        preview_pmap.masks = &preview_pmask;
71
                }else
72
                        preview_pmap.masks = NULL;
73
 
74
                preview_handle = PINEWHANDLE((long)preview_h * preview_pmap.rowBytes);
75
        }else
76
                preview_handle = NULL;
77
        return preview_handle != NULL;
78
 
79
        //---------------------------------------------------------------------------
80
        // Fields new in version 2:
81
        //---------------------------------------------------------------------------   
82
//      preview_pmap.pixelOverlays;
83
//      preview_pmap.colorManagementOptions;
84
 
85
//      setup(pb); // prepare for evaluations
86
}
87
 
88
void dispose_preview(){
89
        if(preview_handle){
90
                PIDISPOSEHANDLE(preview_handle);
91
                preview_handle = NULL;
92
        }
93
}
94
 
95
void recalc_preview(FilterRecordPtr pb,DIALOGREF dp){
96
        extern int srcradused,needinput;
97
        extern double zoomfactor;
98
        OSErr e;
45 toby 99
        int j,n,scaledw,scaledh,imgw,imgh;
2 toby 100
        Rect r,outRect;
101
        Ptr outrow;
102
 
37 toby 103
        preview_complete = false;
104
 
2 toby 105
        if(preview_handle){
106
                /* size of previewed area, of source image; but no larger than filtered area (selection) */
107
                scaledw = zoomfactor*preview_w;
108
                if(scaledw > (pb->filterRect.right - pb->filterRect.left))
109
                        scaledw = (pb->filterRect.right - pb->filterRect.left);
110
                scaledh = zoomfactor*preview_h;
111
                if(scaledh > (pb->filterRect.bottom - pb->filterRect.top))
112
                        scaledh = (pb->filterRect.bottom - pb->filterRect.top);
113
 
114
                /* scale clipped preview area down again - this becomes the pixel size of preview */
115
                imgw = scaledw/zoomfactor;
116
                if(imgw > preview_w)
117
                        imgw = preview_w;
118
                imgh = scaledh/zoomfactor;
119
                if(imgh > preview_h)
120
                        imgh = preview_h;
121
 
122
                // Use to set the phase of the checkerboard:
123
                preview_pmap.maskPhaseRow = preview_scroll.v/zoomfactor;
124
                preview_pmap.maskPhaseCol = preview_scroll.h/zoomfactor;
125
 
126
                /* compute source data rectangle (inRect) */
127
 
128
                /* centre preview on filtered part of input image, adding scroll offset */
53 toby 129
                r.left = (pb->filterRect.left + pb->filterRect.right - scaledw)/2 + preview_scroll.h;
2 toby 130
                /* make sure it does not go outside the input area */
131
                if(r.left < pb->filterRect.left)
132
                        r.left = pb->filterRect.left;
133
                else if(r.left > pb->filterRect.right-scaledw)
53 toby 134
                        r.left = pb->filterRect.right - scaledw;
2 toby 135
                r.right = r.left + scaledw;
136
 
137
                /* now compute for vertical */
53 toby 138
                r.top = (pb->filterRect.top+pb->filterRect.bottom - scaledh)/2 + preview_scroll.v;
2 toby 139
                if(r.top < pb->filterRect.top)
140
                        r.top = pb->filterRect.top;
141
                else if(r.top > pb->filterRect.bottom-scaledh)
53 toby 142
                        r.top = pb->filterRect.bottom - scaledh;
2 toby 143
                r.bottom = r.top + scaledh;
144
 
145
                /* if formulae need random access to image - src(), rad() - we must request entire area: */
146
                if(srcradused){
147
                        SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
148
                }else
149
                        pb->inRect = r;
150
 
151
                pb->outRect = pb->inRect;
152
                SETRECT(pb->maskRect,0,0,0,0);
153
                pb->inLoPlane = pb->outLoPlane = 0;
154
                pb->inHiPlane = pb->outHiPlane = nplanes-1;
155
 
156
//dbg("recalc_preview: about to call advanceState()");
37 toby 157
 
2 toby 158
                if( !needinput || !(e = pb->advanceState()) ){
159
                        Ptr outptr = PILOCKHANDLE(preview_handle,false);
53 toby 160
                        int blankrows = (preview_h - imgh)/2,
161
                                blankcols = (preview_w - imgw)/2, pmrb = preview_pmap.rowBytes;
2 toby 162
 
163
                        INITRANDSEED();
164
//dbg("recalc_preview: about to call process()");
165
 
166
                        SETRECT(outRect,0,0,imgw,imgh);
167
 
53 toby 168
                        e = process_scaled(pb, false, &r, &outRect,
169
                                        outptr + pmrb*blankrows + nplanes*blankcols, pmrb, zoomfactor);
2 toby 170
                        if(blankrows){
53 toby 171
                                memset(outptr, 0xff, pmrb*blankrows);
2 toby 172
                                n = preview_h - blankrows - imgh; /* blank rows below preview */
53 toby 173
                                memset(outptr + pmrb*(blankrows+imgh), 0xff, pmrb*n);
2 toby 174
                        }
175
                        if(blankcols){
176
                                n = preview_w - blankcols - imgw; /* blank columns on right side of preview */
53 toby 177
                                outrow = outptr + pmrb*blankrows;
2 toby 178
                                for( j = blankrows ; j < preview_h - blankrows ; ++j ){
53 toby 179
                                        memset(outrow, 0xff, nplanes*blankcols);
180
                                        memset(outrow + nplanes*(blankcols+imgw), 0xff, nplanes*n);
181
                                        outrow += pmrb;
2 toby 182
                                }
183
                        }
184
 
185
                        if(!e){
186
                                preview_complete = true;
187
 
188
#ifdef WIN_ENV
189
                                {
190
                                extern HWND preview_hwnd;
191
                                HDC hdc = GetDC(preview_hwnd);
192
 
193
//dbg("recalc_preview: about to call drawpreview()");
194
                                drawpreview(dp,hdc,outptr);
195
 
196
                                ReleaseDC(preview_hwnd,hdc);
197
                                }
198
#else
199
                                {
200
                                GrafPtr saveport;
201
 
202
                                GetPort(&saveport);
203
                                SetPortDialogPort(dp);
204
 
205
                                drawpreview(dp,NULL,outptr);
206
 
207
                                SetPort(saveport);
208
                                }
209
#endif
210
                        }
211
 
212
                        PIUNLOCKHANDLE(preview_handle);
213
 
37 toby 214
                }/*else{
215
                        char s[0x100];
2 toby 216
                        sprintf(s,"recalc_preview: advanceState failed (%d)\n\
217
inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d) inLoPlane=%d inHiPlane=%d ",
218
                                e,
219
                                pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
220
                                pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom,
221
                                pb->inLoPlane,pb->inHiPlane);
222
                        dbg(s);
37 toby 223
                }*/
224
 
225
                if(e && !previewerr){
39 toby 226
                        alertuser("Could not build preview at chosen zoom level.",
37 toby 227
                                e == memFullErr && !srcradused ? "The image is too large for available memory. Try zooming in.\nIf that does not help, Cancel and retry the filter." : "");
228
                        previewerr = true;
2 toby 229
                }
37 toby 230
 
2 toby 231
        }
232
}
233
 
234
OSErr drawpreview(DIALOGREF dp,void *hdc,Ptr imageptr){
235
        int32 watchsusp;
236
        OSErr e = noErr;
237
        VRect srcRect;
238
        UIRECT imagebounds;
239
 
240
        if(preview_handle && preview_complete){
241
 
242
                srcRect = preview_pmap.bounds;
243
 
53 toby 244
                imagebounds.left = (preview_rect.left + preview_rect.right - preview_w)/2;
245
                imagebounds.top = (preview_rect.top + preview_rect.bottom - preview_h)/2;
2 toby 246
                imagebounds.right = imagebounds.left + preview_w;
247
                imagebounds.bottom = imagebounds.top + preview_h;
248
 
249
                preview_pmap.baseAddr = imageptr;//PILOCKHANDLE(preview_handle,false);
250
                preview_pmask.maskData = imageptr+3;
251
 
252
                if(gpb->propertyProcs->getPropertyProc){
253
                        gpb->propertyProcs->getPropertyProc(kPhotoshopSignature,propWatchSuspension,0,&watchsusp,NULL);
254
                        gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp+1,NULL);
255
                }
256
                e = gpb->displayPixels(&preview_pmap,&srcRect,imagebounds.top,imagebounds.left,hdc);
257
 
258
                if(gpb->propertyProcs->getPropertyProc)
259
                        gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp,NULL);
260
        }
261
        return e;
262
}