[895964]: cpyBuf.cpp Maximize Restore History

Download this file

cpyBuf.cpp    300 lines (274 with data), 11.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// This program is free software; you can redistribute
// it and/or modify it under the terms of the GNU
// General Public License as published by the Free
// Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// Public License along with this program; if not,
// write to the Free Software Foundation, Inc., 675
// Mass Ave, Cambridge, MA 02139, USA.
/**
* @file CpyBuf.cpp
* This file is included in all four plug-ins (PTAdjust, PTCorrect, PTPerspect, and PTRemap)
* It contains two functions for converting image buffers between photoshop and
* panotools internal formats.
* @date January 2007
*/
#include "main.h"
/**
* Copy Image Buffer data from Panotool to Photoshop format
* @param[in,out] buf the buffer to copy
* @return 0 on success
**/
int cpyBufToPs(void *buf)
{
// TODO: Actualy impliment tiling - loop through the Hort and Vert tiles retrieving a little data at a time.
// for now use a tile the size of the filter rect.
// TODO: Use the crop feature of PanoTools to only process the data of the selection, use less memory.
// some local variables for efficiency
int16 //tileHeight = gFr->inTileHeight;// Prefered tile size.
//if(tileHeight == 0)
tileHeight = gFr->filterRect.bottom - gFr->filterRect.top;
int16 //tileWidth = gFr->inTileWidth;
//if(tileWidth == 0)
tileWidth = gFr->filterRect.right - gFr->filterRect.left;
Rect *outRect = &gFr->outRect;
Rect *filterRect = &gFr->filterRect;
int16 imageWidth = gData->pho.imageWidth;
int16 imageHeight = gData->pho.imageHeight;
int32 imageDepth = gData->pho.imageDepth;
int32 bytesPerPixel = (imageDepth/8) * gData->pho.imagePlanes;
// round up to the nearest horizontal and vertical tile count
uint16 tilesVert = (uint16)((tileHeight - 1 + imageHeight) / tileHeight);
uint16 tilesHoriz = (uint16)((tileWidth - 1 + imageWidth) / tileWidth);
// Fixed numbers are 16.16 values
// the first 16 bits represent the whole number
// the last 16 bits represent the fraction
gFr->inputRate = (int32)1 << 16;
gFr->maskRate = (int32)1 << 16;
// loop through each tile makeing sure we don't go over the bounds
// of the imageHeight or imageWidth
for (uint16 vertTile = 0; vertTile < tilesVert; vertTile++)
{
// layers can be bigger or smaller than whole image area but we want to copy only the area of whole image
outRect->top = (int16)max(0, (vertTile * tileHeight - gFr->floatCoord.v));
outRect->bottom = (int16)min(tileHeight, imageHeight - gFr->floatCoord.v);
uint16 OffSetV = (uint16)max(0, gFr->floatCoord.v);
uint16 rectHeight = (uint16)(outRect->bottom - outRect->top);
for (uint16 horizTile = 0; horizTile < tilesHoriz; horizTile++)
{
// layers can be bigger or smaller than the whole image area
outRect->left = (int16)max(0, (horizTile * tileWidth - gFr->floatCoord.h));
outRect->right = (int16)min(tileWidth, imageWidth - gFr->floatCoord.h);
uint16 OffSetH = (uint16)max(0, gFr->floatCoord.h);
uint16 rectWidth = (uint16)(outRect->right - outRect->left);
// Traverse through the planes to fill
for (uint16 plane = 0; plane < gData->pho.imagePlanes; plane++)
{
// we want one plane at a time, small memory foot print is good
int16 outplane = plane;
if(gData->pho.imagePlanes > 3)
{
if(plane == 0)
{
if(gFr->planes == 4)
continue;//ignor
outplane = 4;
}
else
outplane = plane -1;
}
gFr->outLoPlane = outplane;
gFr->outHiPlane = outplane;
// update the gFr with our latest request
*gResult = gFr->advanceState();
if (*gResult != noErr)
{
return display("cpyBufToPs: advanceState");
}
// copy one channel at a time
if (imageDepth == 8)
{
uint8 *pixel = (uint8*)gFr->outData;
for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
{
Ptr dissolve = (char *)buf + bytesPerPixel *
(imageWidth * (OffSetH + pixelY) + OffSetV) + plane;
for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
{
*pixel++ = *dissolve;
dissolve += bytesPerPixel;
}
pixel += gFr->outRowBytes - rectWidth;
}//pixelY
}//8 bit
else
{
// assume 16-bit
uint16 *pixel = (uint16*)gFr->outData;
uint16 rectHeight = (uint16)(outRect->bottom - outRect->top);
uint16 rectWidth = (uint16)(outRect->right - outRect->left);
int bytesPerPixel2 = bytesPerPixel >> 1;
for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
{
uint16 *dissolve = (uint16 *)((char *)buf + bytesPerPixel *
(imageWidth * (OffSetH + pixelY) + OffSetV) + 2*plane);
for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
{
// convert 0..65535 to 0..32768
*pixel++ = (*dissolve + 1U) >> 1;
dissolve += bytesPerPixel2;
}
pixel += (gFr->outRowBytes - 2*rectWidth) >> 1;
}//pixelY
}//16 bit
}//plane
}//horizTile
}//vertTile
return 0;
}
/**
* Copy Image Buffer data from Photoshop to Panotool format
* Most plugins would handle a layer and its border as an image.
* For Panotools we need to look at the image dimentions.
* A layer can float over a image partly in or out of the image.
* @param[in,out] buf the Panotools image buffer to copy to
* @return none
**/
int cpyPsToBuf(void *buf)
{
// TODO: Actualy impliment tiling - loop through the Hort and Vert tiles retrieving a little data at a time.
// for now use a tile the size of the filter rect.
// TODO: Use the crop feature of PanoTools to only process the data of the selection, use less memory.
// some local variables for efficiency
int16 //tileHeight = gFr->inTileHeight;// Prefered tile size.
//if(tileHeight == 0)
tileHeight = gFr->filterRect.bottom - gFr->filterRect.top;
int16 //tileWidth = gFr->inTileWidth;
//if(tileWidth == 0)
tileWidth = gFr->filterRect.right - gFr->filterRect.left;
Rect *inRect = &gFr->inRect;
Rect *filterRect = &gFr->filterRect;
int16 imageWidth = gData->pho.imageWidth;
int16 imageHeight = gData->pho.imageHeight;
int32 imageDepth = gData->pho.imageDepth;
int32 bytesPerPixel = (imageDepth/8) * gData->pho.imagePlanes;
// round up to the nearest horizontal and vertical tile count
uint16 tilesVert = (uint16)((tileHeight - 1 + imageHeight) / tileHeight);
uint16 tilesHoriz = (uint16)((tileWidth - 1 + imageWidth) / tileWidth);
// Fixed numbers are 16.16 values
// the first 16 bits represent the whole number
// the last 16 bits represent the fraction
gFr->inputRate = (int32)1 << 16;
gFr->maskRate = (int32)1 << 16;
// loop through each tile makeing sure we don't go over the bounds
// of the imageHeight or imageWidth
// First Vert tile with image data.
//uint16 vertTileFirst = (uint16)(max(0, -gFr->floatCoord.v/tileHeight));
//for (uint16 vertTile = vertTileFirst; vertTile < tilesVert; vertTile++)
uint16 vertTile = 0;
{
// layers can be bigger or smaller than whole image area but we want to copy only the area of whole image
inRect->top = (int16)max(0, (vertTile * tileHeight - gFr->floatCoord.v));
inRect->bottom = (int16)min(tileHeight, imageHeight - gFr->floatCoord.v);
uint16 OffSetV = (uint16)max(0, gFr->floatCoord.v);
uint16 rectHeight = (uint16)(inRect->bottom - inRect->top);
// First Hort tile with image data.
//uint16 horizTileFirst = (uint16)(max(0, gFr->floatCoord.h/tileWidth));
//for (uint16 horizTile = horizTileFirst; horizTile < tilesHoriz; horizTile++)
uint16 horizTile = 0;
{
// layers can be bigger or smaller than the whole image area
inRect->left = (int16)max(0, (horizTile * tileWidth - gFr->floatCoord.h));
inRect->right = (int16)min(tileWidth, imageWidth - gFr->floatCoord.h);
uint16 OffSetH = (uint16)max(0, gFr->floatCoord.h);
uint16 rectWidth = (uint16)(inRect->right - inRect->left);
for (uint16 plane = 0; plane < gFr->planes; plane++)
{
// we want one plane at a time, small memory foot print is good
gFr->inLoPlane = plane;
gFr->inHiPlane = plane;
int16 outPlane = plane;
if(gData->pho.imagePlanes>3)
{
if(plane<3)
outPlane = plane +1;
else
outPlane = 0;
}
// update the gFr with our latest request
*gResult = gFr->advanceState();
if (*gResult != noErr)
{
return display("cpyPsToBuf: advanceState");
}
// copy one channel at a time
if (imageDepth == 8)
{
uint8 *pixel = (uint8*)gFr->inData;
for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
{
Ptr dissolve = (char *)buf + bytesPerPixel *
(imageWidth * (OffSetH + pixelY) + OffSetV) + outPlane;
if(plane <4)
{
for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
{
*dissolve = *pixel++;
dissolve += bytesPerPixel;
}
}
else
{
for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
{
*dissolve = min((char)*dissolve, (char)*pixel);
pixel++;
dissolve += bytesPerPixel;
}
}
pixel += gFr->inRowBytes - rectWidth;
}//pixelY
}//8 bit
else
{
// assume 16-bit
uint16 *pixel = (uint16*)gFr->inData;
int bytesPerPixel2 = bytesPerPixel >> 1;
for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
{
uint16 *dissolve = (uint16 *)((char *)buf + bytesPerPixel *
(imageWidth * (OffSetH + pixelY) + OffSetV) + 2*outPlane);
if(plane <4)
{
for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
{
// convert 0..32768 to 0..65535
*dissolve = ((uint16)*pixel++ * 65535U + 16384U) >> 15;
dissolve += bytesPerPixel2;
}
}
else
{
for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
{
// convert 0..32768 to 0..65535
*dissolve = min(*dissolve , (*pixel++ * 65535U + 16384U) >> 15);
dissolve += bytesPerPixel2;
}
}
pixel += (gFr->inRowBytes - 2*rectWidth) >> 1;
}//pixelY
}//16 bit
}//plane
}//horizTile
}//vertTile
return 0;
}