Subversion Repositories filter_foundry

Rev

Rev 159 | Rev 185 | 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
171 dmarschall 3
    Copyright (C) 2003-2019 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
110 dmarschall 6
    it under the terms of the GNU General Public License as published by
2 toby 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
 
110 dmarschall 15
    You should have received a copy of the GNU General Public License
2 toby 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
 
94 toby 22
#include "preview.h"
2 toby 23
 
94 toby 24
#ifdef MAC_ENV
25
        #include <fp.h>
26
#endif
27
#include <math.h>
28
 
29 toby 29
#include "PIProperties.h"
2 toby 30
 
94 toby 31
extern FilterRecordPtr gpb;
2 toby 32
 
33
PSPixelMap preview_pmap;
34
PSPixelMask preview_pmask;
35
Handle preview_handle;
36
UIRECT preview_rect;
94 toby 37
int preview_w,preview_h,previewerr = false,needall = false,needinput = true;
2 toby 38
Point preview_scroll;
39
Boolean preview_complete = false;
94 toby 40
double zoomfactor,fitzoom;
2 toby 41
 
94 toby 42
Boolean setup_preview(FilterRecordPtr pb, int nplanes){
43
        double zh,zv;
44
 
2 toby 45
        if(pb->displayPixels && pb->advanceState){
157 dmarschall 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
                /*
155 dmarschall 49
                preview_w = MIN(preview_rect.right - preview_rect.left,
89 toby 50
                                                pb->filterRect.right - pb->filterRect.left);
155 dmarschall 51
                preview_h = MIN(preview_rect.bottom - preview_rect.top,
89 toby 52
                                                pb->filterRect.bottom - pb->filterRect.top);
157 dmarschall 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
 
94 toby 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;
110 dmarschall 62
 
2 toby 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;
94 toby 67
                preview_pmap.imageMode = pb->imageMode;
2 toby 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 */
110 dmarschall 73
 
2 toby 74
                //---------------------------------------------------------------------------
75
                // Fields new in version 1:
110 dmarschall 76
                //---------------------------------------------------------------------------
2 toby 77
                preview_pmap.mat = NULL;
110 dmarschall 78
 
94 toby 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) )
89 toby 83
                {
2 toby 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;
110 dmarschall 92
 
2 toby 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:
110 dmarschall 100
        //---------------------------------------------------------------------------
2 toby 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
 
158 dmarschall 112
void* memset_bgcolor(void* ptr, size_t num) {
157 dmarschall 113
        int i;
156 dmarschall 114
        byte* p;
157 dmarschall 115
 
116
        i = 0;
156 dmarschall 117
        p = (byte*)ptr;
118
        for (i=0; i<num; ++i) {
158 dmarschall 119
#ifdef WIN_ENV
159 dmarschall 120
                DWORD color;
121
 
158 dmarschall 122
                color = GetSysColor(COLOR_APPWORKSPACE);
159 dmarschall 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
                }
158 dmarschall 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
156 dmarschall 191
        }
192
        return ptr;
193
}
194
 
2 toby 195
void recalc_preview(FilterRecordPtr pb,DIALOGREF dp){
196
        OSErr e;
45 toby 197
        int j,n,scaledw,scaledh,imgw,imgh;
2 toby 198
        Rect r,outRect;
199
        Ptr outrow;
200
 
37 toby 201
        preview_complete = false;
110 dmarschall 202
 
2 toby 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))
89 toby 207
                        scaledw = pb->filterRect.right - pb->filterRect.left;
2 toby 208
                scaledh = zoomfactor*preview_h;
209
                if(scaledh > (pb->filterRect.bottom - pb->filterRect.top))
89 toby 210
                        scaledh = pb->filterRect.bottom - pb->filterRect.top;
110 dmarschall 211
 
2 toby 212
                /* scale clipped preview area down again - this becomes the pixel size of preview */
121 dmarschall 213
                imgw = ceil(scaledw/zoomfactor);
2 toby 214
                if(imgw > preview_w)
215
                        imgw = preview_w;
121 dmarschall 216
                imgh = ceil(scaledh/zoomfactor);
2 toby 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 */
53 toby 223
                r.left = (pb->filterRect.left + pb->filterRect.right - scaledw)/2 + preview_scroll.h;
2 toby 224
                /* make sure it does not go outside the input area */
110 dmarschall 225
                if(r.left < pb->filterRect.left) {
120 dmarschall 226
                        preview_scroll.h += pb->filterRect.left - r.left;
2 toby 227
                        r.left = pb->filterRect.left;
110 dmarschall 228
                }
229
                else if(r.left+scaledw > pb->filterRect.right) {
120 dmarschall 230
                        preview_scroll.h += pb->filterRect.right - (r.left+scaledw);
53 toby 231
                        r.left = pb->filterRect.right - scaledw;
110 dmarschall 232
                }
2 toby 233
                r.right = r.left + scaledw;
119 dmarschall 234
                preview_pmap.maskPhaseCol = (preview_scroll.h)/zoomfactor; // phase of the checkerboard
2 toby 235
 
236
                /* now compute for vertical */
94 toby 237
                r.top = (pb->filterRect.top + pb->filterRect.bottom - scaledh)/2 + preview_scroll.v;
110 dmarschall 238
                if(r.top < pb->filterRect.top) {
120 dmarschall 239
                        preview_scroll.v += pb->filterRect.top - r.top;
2 toby 240
                        r.top = pb->filterRect.top;
110 dmarschall 241
                }
242
                else if(r.top+scaledh > pb->filterRect.bottom) {
120 dmarschall 243
                        preview_scroll.v += pb->filterRect.bottom - (r.top+scaledh);
53 toby 244
                        r.top = pb->filterRect.bottom - scaledh;
110 dmarschall 245
                }
2 toby 246
                r.bottom = r.top + scaledh;
119 dmarschall 247
                preview_pmap.maskPhaseRow = (preview_scroll.v)/zoomfactor; // phase of the checkerboard
2 toby 248
 
249
                /* if formulae need random access to image - src(), rad() - we must request entire area: */
94 toby 250
                if(needall)
2 toby 251
                        SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
94 toby 252
                else
2 toby 253
                        pb->inRect = r;
110 dmarschall 254
 
2 toby 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;
37 toby 259
 
2 toby 260
                if( !needinput || !(e = pb->advanceState()) ){
261
                        Ptr outptr = PILOCKHANDLE(preview_handle,false);
53 toby 262
                        int blankrows = (preview_h - imgh)/2,
89 toby 263
                                blankcols = (preview_w - imgw)/2,
264
                                pmrb = preview_pmap.rowBytes;
2 toby 265
 
94 toby 266
                        evalinit();
110 dmarschall 267
 
2 toby 268
                        SETRECT(outRect,0,0,imgw,imgh);
110 dmarschall 269
 
53 toby 270
                        e = process_scaled(pb, false, &r, &outRect,
271
                                        outptr + pmrb*blankrows + nplanes*blankcols, pmrb, zoomfactor);
2 toby 272
                        if(blankrows){
157 dmarschall 273
                                // blank rows on top of preview:
158 dmarschall 274
                                memset_bgcolor(outptr, pmrb*blankrows);
157 dmarschall 275
                                // blank rows below preview:
276
                                n = preview_h - blankrows - imgh;
158 dmarschall 277
                                memset_bgcolor(outptr + pmrb*(blankrows+imgh), pmrb*n);
2 toby 278
                        }
279
                        if(blankcols){
157 dmarschall 280
                                n = preview_w - blankcols - imgw;
53 toby 281
                                outrow = outptr + pmrb*blankrows;
94 toby 282
                                for(j = blankrows; j < preview_h - blankrows; ++j){
157 dmarschall 283
                                        // blank columns on left side of preview (if picture is smaller than the preview area):
158 dmarschall 284
                                        memset_bgcolor(outrow, nplanes*blankcols);
157 dmarschall 285
                                        // blank columns on right side of preview (if picture is smaller than the preview area):
158 dmarschall 286
                                        memset_bgcolor(outrow + nplanes*(blankcols+imgw), nplanes*n);
53 toby 287
                                        outrow += pmrb;
2 toby 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);
89 toby 298
 
2 toby 299
                                drawpreview(dp,hdc,outptr);
110 dmarschall 300
 
2 toby 301
                                ReleaseDC(preview_hwnd,hdc);
302
                                }
303
#else
304
                                {
305
                                GrafPtr saveport;
306
 
307
                                GetPort(&saveport);
308
                                SetPortDialogPort(dp);
110 dmarschall 309
 
2 toby 310
                                drawpreview(dp,NULL,outptr);
110 dmarschall 311
 
2 toby 312
                                SetPort(saveport);
313
                                }
314
#endif
315
                        }
316
 
317
                        PIUNLOCKHANDLE(preview_handle);
94 toby 318
                }
2 toby 319
 
37 toby 320
                if(e && !previewerr){
39 toby 321
                        alertuser("Could not build preview at chosen zoom level.",
94 toby 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." : "");
37 toby 323
                        previewerr = true;
2 toby 324
                }
37 toby 325
 
2 toby 326
        }
327
}
328
 
329
OSErr drawpreview(DIALOGREF dp,void *hdc,Ptr imageptr){
114 dmarschall 330
        intptr_t watchsusp;
2 toby 331
        OSErr e = noErr;
332
        VRect srcRect;
333
        UIRECT imagebounds;
334
 
335
        if(preview_handle && preview_complete){
336
 
337
                srcRect = preview_pmap.bounds;
338
 
53 toby 339
                imagebounds.left = (preview_rect.left + preview_rect.right - preview_w)/2;
340
                imagebounds.top = (preview_rect.top + preview_rect.bottom - preview_h)/2;
2 toby 341
                imagebounds.right = imagebounds.left + preview_w;
342
                imagebounds.bottom = imagebounds.top + preview_h;
343
 
89 toby 344
                preview_pmap.baseAddr = imageptr;
94 toby 345
                preview_pmask.maskData = imageptr+3; // FIXME: is this offset correct for all modes?!
2 toby 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
                }
89 toby 351
 
2 toby 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
}