Hitesh,
Here is my code for saving as PNG. It might help you to work out how to save as JPEG.
Robin
extern "C" AIImageOptSuite* sImageOptimization;
extern "C" AIDataFilterSuite* sAIDataFilter;
extern "C" AIDocumentSuite* sDocument;
extern "C" AILayerSuite* sLayer;
extern "C" AIArtSuite* sAIArt;
extern "C" AIPathSuite* sAIPath;
extern "C" AIPathStyleSuite* sAIPathStyle;
static AIActionParamValueRef CreateRasteriseFileParams ()
{
AIActionParamValueRef valueParameterBlock = NULL;
ReturnIfError (sAIActionManager->AINewActionParamValue (&valueParameterBlock));
if (valueParameterBlock)
{
string theStr ("Adobe PNG Format");
ReturnIfError (sAIActionManager->AIActionSetString (valueParameterBlock, kAIExportDocumentFormatKey, theStr.c_str ()));
theStr = "png";
ReturnIfError (sAIActionManager->AIActionSetString (valueParameterBlock, kAIExportDocumentExtensionKey, theStr.c_str ()));
}
return valueParameterBlock;
}
static bool gFirstTimePNG = true;
static AIBoolean MyRasterizeProgressProc (long current, long total)
{
return true;
}
static void RasteriseFileToPNG (const ai::UnicodeString& inFilePath, AIActionParamValueRef valueParameterBlock, AIArtHandle art, AIRealRect& bounds)
{
if (sImageOptimization && sAIDataFilter && art)
{
AIDataFilter* dstfilter = NULL;
AIDataFilter* filter;
ai::FilePath file (inFilePath);
AIErr result = sAIDataFilter->NewFileDataFilter (file, "write", 'prvw', 'PNGf', &filter);
if (!result)
{
result = sAIDataFilter->LinkDataFilter (dstfilter, filter);
dstfilter = filter;
}
AIDocumentSetup setup;
if ((bounds.bottom == 0.0) && (bounds.right == 0.0f))
{
if (sDocument->GetDocumentSetup (&setup))
{
setup.width = 0.0f;
setup.height = 0.0f;
}
}
else
{
setup.width = bounds.right - bounds.left;
if (setup.width < 0.0f)
setup.width = -setup.width;
setup.height = bounds.bottom - bounds.top;
if (setup.height < 0.0f)
setup.height = -setup.height;
}
float maxDims = setup.height; // in points
if (maxDims < setup.width)
maxDims = setup.width;
float resolution;
if (maxDims > 0.0f)
{
resolution = 72000.0f / maxDims; // 72 dpi x 1000 pixels = ? dpi X maxDims pixels so ? dpi = 72 dpi x 1000 pixels / maxDims pixels
if (resolution < 5.0f)
resolution = 5.0f;
else
if (resolution > 300.0f)
resolution = 300.0f;
}
else
resolution = 36.0f;
AIImageOptPNGParams2 params;
params.versionOneSuiteParams.interlaced = false;
params.versionOneSuiteParams.numberOfColors = 255;
params.versionOneSuiteParams.transparentIndex = 0;
params.versionOneSuiteParams.resolution = resolution;
params.versionOneSuiteParams.outAlpha = false;
params.versionOneSuiteParams.outWidth = 0;
params.versionOneSuiteParams.outHeight = 0;
params.antialias = true;
/* A cropping box for the art. If empty or degenerate, do not crop. */
params.cropBox = bounds;
params.backgroundIsTransparent = true;
/* When backgroundIsTransparent is false, rasterize against this matte color. */
params.matteColor.red = 1.0f;
params.matteColor.green = 1.0f;
params.matteColor.blue = 1.0f;
AIArtHandle path = NULL;
AILayerHandle newLayer = NULL;
result = sLayer->InsertLayer (NULL, kPlaceAboveAll, &newLayer);
if (newLayer && !result)
{
sLayer->SetLayerVisible (newLayer, true); // show layer
sLayer->SetLayerIsTemplate (newLayer, false); // mark as NOT a template layer so does get exported
sLayer->SetLayerEditable (newLayer, true);
AIPathSegment segments[4];
AIPathStyle pathStyle;
AIPathStyleMap pathStyleMap;
result = sAIArt->NewArt (kPathArt, kPlaceAboveAll, NULL, &path);
if (path && !result)
{
result = sAIPath->SetPathSegmentCount (path, 4);
if (!result)
{
segments[0].p.h = bounds.left;
segments[0].p.v = bounds.top;
segments[0].in = segments[0].out = segments[0].p;
segments[0].corner = true;
segments[1].p.h = bounds.right;
segments[1].p.v = bounds.top;
segments[1].in = segments[1].out = segments[1].p;
segments[1].corner = true;
segments[2].p.h = bounds.right;
segments[2].p.v = bounds.bottom;
segments[2].in = segments[2].out = segments[2].p;
segments[2].corner = true;
segments[3].p.h = bounds.left;
segments[3].p.v = bounds.bottom;
segments[3].in = segments[3].out = segments[3].p;
segments[3].corner = true;
sAIPath->SetPathSegments (path, 0, 4, segments);
sAIPath->SetPathClosed (path, true);
sAIPathStyle->GetCurrentPathStyle (&pathStyle, &pathStyleMap);
pathStyle.fillPaint = false;
pathStyle.fill.color.kind = kGrayColor;
pathStyle.fill.color.c.g.gray = kAIRealZero;
pathStyle.strokePaint = false;
pathStyle.stroke.color.kind = kGrayColor;
pathStyle.stroke.color.c.g.gray = kAIRealZero;
pathStyle.stroke.width = kAIRealZero;
sAIPathStyle->SetPathStyle (path, &pathStyle);
}
}
}
result = sImageOptimization->MakePNG24 (art, dstfilter, params, MyRasterizeProgressProc);
if (dstfilter)
sAIDataFilter->UnlinkDataFilter (dstfilter, &dstfilter);
if (path)
sAIArt->DisposeArt (path);
if (newLayer)
sLayer->DeleteLayer (newLayer);
}
else
if (valueParameterBlock)
{
ReturnIfError (sAIActionManager->AIActionSetStringUS (valueParameterBlock, kAISaveDocumentAsNameKey, inFilePath));
ReturnIfError (sAIActionManager->PlayActionEvent (kAIExportDocumentAction, gFirstTimePNG ? kDialogOn : kDialogOff, valueParameterBlock));
}
gFirstTimePNG = false;
}