Commit 8b364d01 authored by Ivan.Shulga's avatar Ivan.Shulga Committed by Alexander Trofimov

git-svn-id:...

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@52419 954022d7-b5bf-4e40-9824-e11837661b57
parent 1a9a5423
......@@ -66,11 +66,11 @@ ASCImageStudio3/ASCImageEditor/TestViewManager/Images/MenuViewGridIcon.png svn_m
ASCImageStudio3/ASCImageEditor/TestViewManager/Images/MenuViewRulersIcon.png svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageEditor/TestViewManager/Images/MenuViewZoomInIcon.png svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageEditor/TestViewManager/Images/MenuViewZoomOutIcon.png svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageFile3/Libs/GrayDither.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageFile3/Libs/GrayDither/GrayDither.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageFile3/Libs/LibJ2K.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageFile3/Libs/LibJ2K/libjasper.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageFile3/MetaData.h svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageFile3/Libs/GrayDither.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageFile3/Libs/GrayDither/GrayDither.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageFile3/Libs/LibJ2K.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageFile3/Libs/LibJ2K/libjasper.lib svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/ASCImageFile3/MetaData.h svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageMetaData/trunk/common/xsd.exe svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageMetaData/trunk/doc/AVSImageMetaData.odt svn_mime_002dtype=application%2Foctet-stream
ASCImageStudio3/AVSImageMetaData/trunk/doc/IPTC-PhotoMetadata-2008.pdf svn_mime_002dtype=application%2Foctet-stream
......
// GrayDither.cpp : Defines the entry point for the application.
#include "stdafx.h"
#include "..\GrayDither.h"
#define gray_quant_fsdither1 BMPgray_quant_fsdither1
#define grayHistAlloc BMPgrayHistAlloc
#define grayHistDealloc BMPgrayHistDealloc
#define grayDitherUnprepare BMPgrayDitherUnprepare
#define grayDitherPrepare BMPgrayDitherPrepare
#define gray_quant_fsdither1 BMPgray_quant_fsdither1
extern ULONG** grayHistAlloc(UCHAR* imgMap);
extern void grayHistDealloc(ULONG** p);
extern int grayDitherPrepare(ULONG* raster, ULONG width, ULONG height, ULONG tilelen = 0, int bps = 0, BOOL dither = FALSE, BOOL paletted = FALSE, int numcolors = 0);
extern void grayDitherUnprepare();
extern int gray_quant_fsdither1(ULONG* inbuf, ULONG step, ULONG w, ULONG h, ULONG x, LONG fromskew, LONG toskew, UCHAR* outbuf);
/*
BOOL Dither32Image(BYTE* data, int width, int height)
{
// compute compressed image size
int compressedSize = (width + 7)/8;
// allocate memory for compressed image
BYTE* compressed = new BYTE[compressedSize];
// variables
int x, y;
ULONG* pixels = (ULONG*)data;
// dither image by line
for (y = 0; y < height; ++y)
{
// quantize data
if (gray_quant_fsdither1(pixels + width*y, 1, width, 1, 0, 0, 0, compressed) == 0)
return FALSE;
// decompress bit-image to 32bpp image
// for (x = 0; x < width; ++x)
// *(pixels + x) = (compressed[x/8] & 1 << (x % 8))? 0xFFFFFF : 0;
}
// quantize by whole image
// gray_quant_fsdither1((ULONG*)(bufin+4*cx*i), 1, cx, cy, 0, 0, 0, bufout);
// all ok
return TRUE;
}
*/
BOOL Dither32Image(BYTE* data, int width, int height)
{
BYTE * bufin, * bufout;
int cx, cy, i, j;
bufin = data;
cx = width;
cy = height;
int m_out_bit_depth = 1; // for B/W (1 bit / pixel)
int wout = (m_out_bit_depth * cx + 7)/8;
// BOOL m_bGrayscale = TRUE;
BOOL m_bUseFSdither = TRUE;
int* histogram = NULL;
ULONG** histp;
long step = 1; // Icons (1,2,3...)
static RGBQUAD palette[256];
ZeroMemory(palette, sizeof(palette));
int ncols = 0;
// (R,G,B )
if (m_out_bit_depth <= 8) { // building palette
// if (m_bGrayscale) {
ncols = 1<<m_out_bit_depth;
histp = grayHistAlloc(NULL);
if (histp == NULL) throw (-1);
ncols = grayDitherPrepare((ULONG*)bufin, cx, cy,
0, m_out_bit_depth, m_bUseFSdither, FALSE, ncols);
if (ncols == 0) throw (-1);
USHORT *rm;
rm = (USHORT*)histp[0];
histogram = (int*)histp[1];
// 8, 16
for (int icol = 0; icol < ncols; icol++)
palette[icol].rgbRed = palette[icol].rgbGreen =
palette[icol].rgbBlue = (UCHAR)rm[icol];
// }
}
// if (m_bGrayscale) { ? 16,24,32
// ColorToGrayscale(bpMem, cx, cy);
// }
// -
bufout = (BYTE*)malloc(wout); //
for (i=0; i < cy; i++) {
if (gray_quant_fsdither1(
(ULONG*)bufin+cx*i, 1,
cx, 1, 0, 0, 0, bufout) == 0) throw (-1);
// fwrite(bufout, wout, 1, file);
//
for (j=0; j<cx; j++)
*((ULONG*)bufin+(cx*i+j)) = (bufout[j/8] & 1<<(j%8))? 0xFFFFFF : 0;
}
free(bufout);
// -
/*
if (gray_quant_fsdither1(
(ULONG*)(bufin+4*cx*i), 1,
cx, cy, 0, 0, 0, bufout) == 0) throw (-1);
fwrite(bufout, wout*cy, 1, file);
*/
if (m_out_bit_depth <= 8)
{
grayDitherUnprepare();
grayHistDealloc(histp);
}
return TRUE;
}
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual C++ Express 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GrayDither", "GrayDither.vcproj", "{14A4EA98-9CB5-401A-BDF3-68B70D74F5E9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{14A4EA98-9CB5-401A-BDF3-68B70D74F5E9}.Debug|Win32.ActiveCfg = Debug|Win32
{14A4EA98-9CB5-401A-BDF3-68B70D74F5E9}.Debug|Win32.Build.0 = Debug|Win32
{14A4EA98-9CB5-401A-BDF3-68B70D74F5E9}.Release|Win32.ActiveCfg = Release|Win32
{14A4EA98-9CB5-401A-BDF3-68B70D74F5E9}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="windows-1251"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Name="GrayDither"
ProjectGUID="{14A4EA98-9CB5-401A-BDF3-68B70D74F5E9}"
Keyword="Win32Proj"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
ConfigurationType="4"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC70.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
UsePrecompiledHeader="2"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
OutputFile="$(ProjectName).lib"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
IntermediateDirectory="Release"
ConfigurationType="4"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC70.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
InlineFunctionExpansion="1"
OmitFramePointers="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
StringPooling="true"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="2"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
OutputFile="$(ProjectName).lib"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm"
>
<File
RelativePath="_dither.cpp"
>
</File>
<File
RelativePath="_graydither.cpp"
>
</File>
<File
RelativePath="GrayDither.cpp"
>
</File>
<File
RelativePath="stdafx.cpp"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc"
>
<File
RelativePath="..\GrayDither.h"
>
</File>
<File
RelativePath="stdafx.h"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>
// stdafx.cpp : source file that includes just the standard includes
// GrayDither.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
Microsoft Visual Studio Solution File, Format Version 7.00
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibJ2K", "LibJ2K.vcproj", "{8E176F34-67BA-4F8A-88C2-77BB55EDC06C}"
EndProject
Global
GlobalSection(SolutionConfiguration) = preSolution
ConfigName.0 = Debug
ConfigName.1 = Release
EndGlobalSection
GlobalSection(ProjectDependencies) = postSolution
EndGlobalSection
GlobalSection(ProjectConfiguration) = postSolution
{8E176F34-67BA-4F8A-88C2-77BB55EDC06C}.Debug.ActiveCfg = Debug|Win32
{8E176F34-67BA-4F8A-88C2-77BB55EDC06C}.Debug.Build.0 = Debug|Win32
{8E176F34-67BA-4F8A-88C2-77BB55EDC06C}.Release.ActiveCfg = Release|Win32
{8E176F34-67BA-4F8A-88C2-77BB55EDC06C}.Release.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EndGlobalSection
GlobalSection(ExtensibilityAddIns) = postSolution
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding = "windows-1251"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.00"
Name="LibJ2K"
ProjectGUID="{8E176F34-67BA-4F8A-88C2-77BB55EDC06C}"
Keyword="Win32Proj">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
ConfigurationType="4"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\JASPER-15003\libjasper\include"
PreprocessorDefinitions="WIN32;_DEBUG;_LIB"
MinimalRebuild="TRUE"
BasicRuntimeChecks="3"
RuntimeLibrary="5"
UsePrecompiledHeader="3"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="4"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLibrarianTool"
OutputFile="$(OutDir)/LibJ2K.lib"
AdditionalLibraryDirectories=""
IgnoreAllDefaultLibraries="TRUE"
IgnoreDefaultLibraryNames=""/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
IntermediateDirectory="Release"
ConfigurationType="4"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="2"
InlineFunctionExpansion="1"
OmitFramePointers="TRUE"
AdditionalIncludeDirectories="..\JASPER-15003\libjasper\include"
PreprocessorDefinitions="WIN32;NDEBUG;_LIB"
StringPooling="TRUE"
RuntimeLibrary="4"
EnableFunctionLevelLinking="TRUE"
UsePrecompiledHeader="3"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLibrarianTool"
OutputFile="$(OutDir)/LibJ2K.lib"
AdditionalLibraryDirectories=""
IgnoreAllDefaultLibraries="TRUE"
IgnoreDefaultLibraryNames=""/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
</Configuration>
</Configurations>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm">
<File
RelativePath="realization.cpp">
</File>
<File
RelativePath="stdafx.cpp">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32">
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"/>
</FileConfiguration>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc">
<File
RelativePath="exports.h">
</File>
<File
RelativePath="stdafx.h">
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe">
</Filter>
<File
RelativePath="libjasper.lib"
DeploymentContent="TRUE">
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "exports.h"
/////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#define joe_FAKE_JUSTINTIMEDEBUGGERS() \
do { __try { } __except (1) { } } while (0)
#else
#define joe_FAKE_JUSTINTIMEDEBUGGERS()
#endif
/////////////////////////////////////////////////////////////////////////////
#define OPTSMAX 4096
static char outoptsbuf[OPTSMAX + 1];
static void ColorToGrayscale(LPSTR pmem, int cx, int cy);
#define ErrorCode(x) do {BusyUseStatics=false;return E_FAIL;} while (0) //S_OK
// v ( )
static bool BusyUseStatics = false; // threads
static ImageFormat BmpFormat;
static FILE* infile;
int Jp2Status;
long Jp2NumWarnings; /*num.of corrupt-data warnings*/
#define UINTSWAP(i) \
((i)<<24 | ((i)&0xFF00)<<8 | (i)>>8&0xFF00 | (i)>>24&0xFF)
#define WORDSWAP(w) ((w)<<8&0xFF00 | (w)>>8&0xFF)
#define JP2_IHDR_LEN 14
#define JP2_VALIDATELEN \
max(JP2_IHDR_LEN-JP2_BOX_HDRLEN, \
max(JP2_JP_LEN-JP2_BOX_HDRLEN, 16))
// GetJp2Format ,
// OpenSave.cpp (, Validate() JasPer)
static int bmp_putdata(LPSTR bpMem, jas_image_t *image, UINT w = 0, UINT h = 0, int FrameNum = 0, int SetStep = 0)
{
UINT i, j, y;
jas_matrix_t *cmpts[3];
// int numpad;
unsigned char red;
unsigned char grn;
unsigned char blu;
int ret;
uint_fast32_t width;
uint_fast32_t height;
uint_fast16_t depth;
uint_fast16_t cmptno;
uint_fast16_t numcmpts;
numcmpts = jas_image_numcmpts(image);
width = jas_image_cmptwidth(image, 0);
height = jas_image_cmptheight(image, 0);
depth = jas_image_cmptprec(image, 0);
/* Check to ensure that the image to be saved can actually be represented
using the BMP format. */
if (numcmpts != 3 && numcmpts != 1) return -1;
for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
if (jas_image_cmptwidth(image, cmptno) != width ||
jas_image_cmptheight(image, cmptno) != height ||
jas_image_cmptprec(image, cmptno) != depth ||
jas_image_cmptsgnd(image, cmptno) != false ||
jas_image_cmpttlx(image, cmptno) != 0 ||
jas_image_cmpttly(image, cmptno) != 0) {
// fprintf(stderr, "The BMP format cannot be used to represent an image with this geometry.\n");
return -1;
}
}
SetStep = SetStep <= 0? 1 : SetStep;
UINT savw = (width+SetStep-1) / SetStep;
UINT savh = (height+SetStep-1) / SetStep;
w = (w == 0)? savw : w;
h = (h == 0)? savh : h;
savw = min(w,savw);
savh = min(h,savh);
/* We do not support palettized images. */
// if (BMP_HASPAL(info) && numcmpts == 3) {
// fprintf(stderr, "no palettized image support for BMP format\n");
// return -1;
// }
ret = 0;
for (i = 0; i < numcmpts; ++i) {
cmpts[i] = 0;
}
/* Create temporary matrices to hold component data. */
for (i = 0; i < numcmpts; ++i) {
if (!(cmpts[i] = jas_matrix_create(1, width))) {
ret = -1;
goto bmp_putdata_done;
}
}
/* Put the image data. */
for (i = 0; i < savh; ++i) {
for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
if (jas_image_readcmpt(image, cmptno, 0, i*SetStep, width,
1, cmpts[cmptno])) {
ret = -1;
goto bmp_putdata_done;
}
}
y = (savh - 1 - i);
for (j = 0; j < savw; ++j) {
if (numcmpts == 3) {
red = (jas_matrix_getv(cmpts[0], j*SetStep));
grn = (jas_matrix_getv(cmpts[1], j*SetStep));
blu = (jas_matrix_getv(cmpts[2], j*SetStep));
*(bpMem+4*w*y+4*j+0) = blu;
*(bpMem+4*w*y+4*j+1) = grn;
*(bpMem+4*w*y+4*j+2) = red;
} else if (numcmpts == 1) {
red = (jas_matrix_getv(cmpts[0], j*SetStep));
*(bpMem+4*w*y+4*j+0) = red;
*(bpMem+4*w*y+4*j+1) = red;
*(bpMem+4*w*y+4*j+2) = red;
} // else { abort(); }
}
}
bmp_putdata_done:
/* Destroy the temporary matrices. */
for (i = 0; i < numcmpts; ++i) {
if (cmpts[i]) {
jas_matrix_destroy(cmpts[i]);
}
}
return ret;
}
static jas_image_t* bmp_getdata(LPSTR bpMem, int width, int height)
{
jas_image_t *image;
jas_image_cmptparm_t cmptparms[3];
jas_image_cmptparm_t *cmptparm;
uint_fast16_t cmptno;
uint_fast16_t numcmpts;
numcmpts = 3;
for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno,
++cmptparm) {
cmptparm->tlx = 0;
cmptparm->tly = 0;
cmptparm->hstep = 1;
cmptparm->vstep = 1;
cmptparm->width = width;
cmptparm->height = height;
cmptparm->prec = 8;
cmptparm->sgnd = false;
}
if (!(image = jas_image_create(numcmpts, cmptparms, JAS_IMAGE_CS_RGB)))
return NULL;
/* Read the bitmap data. */
// if (bmp_getdata(in, info, image)) {...}
int i;
int j;
int y;
jas_matrix_t *cmpts[3]; //= {0,0,0};//joe
unsigned char red;
unsigned char grn;
unsigned char blu;
int ret;
ret = 0;
for (i = 0; i < numcmpts; ++i) {
cmpts[i] = 0;
}
/* Create temporary matrices to hold component data. */
for (i = 0; i < numcmpts; ++i) {
if (!(cmpts[i] = jas_matrix_create(1, width))) {
ret = -1;
goto bmp_getdata_done;
}
}
for (i = 0; i < height; ++i) {
y = (height - 1 - i);
for (j = 0; j < width; ++j) {
blu = *(bpMem+4*width*y+4*j+0);
grn = *(bpMem+4*width*y+4*j+1);
red = *(bpMem+4*width*y+4*j+2);
if (numcmpts == 3) {
jas_matrix_setv(cmpts[0], j, red);
jas_matrix_setv(cmpts[1], j, grn);
jas_matrix_setv(cmpts[2], j, blu);
} else {
jas_matrix_setv(cmpts[0], j, red);
}
}
for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
if (jas_image_writecmpt(image, cmptno, 0, i, width,
1, cmpts[cmptno])) {
ret = -1;
goto bmp_getdata_done;
}
}
}
bmp_getdata_done:
/* Destroy the temporary matrices. */
for (i = 0; i < numcmpts; ++i) {
if (cmpts[i]) {
jas_matrix_destroy(cmpts[i]);
}
}
if (ret) {
jas_image_destroy(image);
return NULL;
}
return image;
}
static UINT jp2_getbox(UCHAR* buf, UINT& type, bool bBigEndian)
{
UINT len, tmp;
if (fread(buf, JP2_BOX_HDRLEN, 1, infile) != 1)
return 0;
len = *(UINT*)buf;
if (bBigEndian) len = UINTSWAP(len);
type = *((UINT*)buf+1);
if (bBigEndian) type = UINTSWAP(type);
if (len == 1) {
ULARGE_INTEGER extlen;
if (fread(buf+JP2_BOX_HDRLEN, sizeof(UINT64),
1, infile) != 1) return 0;
extlen.HighPart = *((UINT*)buf+3);
extlen.LowPart = *((UINT*)buf+2);
if (bBigEndian)
tmp = UINTSWAP(extlen.LowPart),
extlen.LowPart = UINTSWAP(extlen.HighPart),
extlen.HighPart = tmp;
//
if (extlen.HighPart != 0) return 0;
if (extlen.LowPart < JP2_BOX_HDRLEN+sizeof(UINT64))
return 0;
len = extlen.LowPart-sizeof(UINT64);
}
// codestream
if (len == 0 && type == JP2_BOX_JP2C)
len = (UINT)-1;
if (len < JP2_BOX_HDRLEN) return 0;
return len;
}
static UINT skip_jp2c(bool bBigEndian)
{
USHORT csid, cslen;
// JP2C codestream
for (;;) {
if (fread(&csid, sizeof(USHORT), 1, infile) != 1)
return 0;
if (bBigEndian) csid = WORDSWAP(csid);
if (csid < JPC_MS_MIN || csid > JPC_MS_MAX)
return 0;
// JP2C codestream
if (csid == JPC_MS_EOC) break;
/* Get the marker segment length and parameters if present. */
/* Note: It is tacitly assumed that a marker segment cannot have
parameters unless it has a length field. That is, there cannot
be a parameters field without a length field and vice versa. */
if (JPC_MS_HASPARMS(csid)) {
// Get the length of the marker segment.
if (fread(&cslen, sizeof(USHORT), 1, infile) != 1)
return 0;
if (bBigEndian) cslen = WORDSWAP(cslen);
if (cslen < 3) return 0;
if (csid != JPC_MS_SOT)
// jasper -
// erroneous cslen ( )
fseek(infile,cslen-2,SEEK_CUR);
else { // JPC_MS_SOT tile-part
USHORT isot;
UINT tplen;
if (cslen < 10) return 0;
if (fread(&isot, sizeof(USHORT), 1, infile) != 1)
return 0;
if (bBigEndian) isot = WORDSWAP(isot);
if (fread(&tplen, sizeof(UINT), 1, infile) != 1)
return 0;
if (bBigEndian) tplen = UINTSWAP(tplen);
if (tplen == 0)
fseek(infile,-2,SEEK_END);
else {
if (tplen < 10) return 0;
fseek(infile,tplen-10,SEEK_CUR);
}
}
}
}
return 1;
}
static BOOL get_JPEG2000_info(FILE* in, LPIMAGEFORMAT pFmt)
{
unsigned char buf[JP2_VALIDATELEN];
bool bBigEndian = true; // always for JasPer!
UINT len, type;
WORD flags = 0;
bool dataflag;
int FrameNo = 0;
if (in == NULL) return FALSE;
infile = in;
fseek(infile,0,SEEK_SET);
// 1) JP box
if ((len = jp2_getbox(buf, type, bBigEndian))==0)
return FALSE;
len -= JP2_BOX_HDRLEN;
flags = (len > 0)? 0 : -1;
dataflag = !(flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
if (!dataflag) return FALSE;
if (len > sizeof(buf)) return FALSE;
if (len < sizeof(UINT)) return FALSE;
if (fread(buf, len, 1, infile) != 1) return FALSE;
UINT magic = *((UINT*)buf);
if (bBigEndian) magic = UINTSWAP(magic);
if (type != JP2_BOX_JP || magic != JP2_JP_MAGIC)
return FALSE;
// 2) FTYP box
if ((len = jp2_getbox(buf, type, bBigEndian))==0)
return FALSE;
if (type != JP2_BOX_FTYP) return FALSE;
len -= JP2_BOX_HDRLEN;
flags = (len > 0)? 0 : -1;
dataflag = !(flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
if (!dataflag) return FALSE;
if (len > sizeof(buf)) return FALSE;
if (len < 2*sizeof(UINT)) return FALSE;
if (fread(buf, 8, 1, infile) != 1) return FALSE;
UINT majver, minver;
majver = *((UINT*)buf);
minver = *((UINT*)buf+1);
if (bBigEndian) majver = UINTSWAP(majver);
if (bBigEndian) minver = UINTSWAP(minver);
// compatcodes
len -= 8;
while (len > 0) {
UINT sz = min(len, sizeof(UINT));
if (fread(buf, sz, 1,
infile) != 1) return FALSE;
len -= sz;
}
// 3) IHDR box JP2C codestream
bool found = false, jp2cfound = false;
long ipos;
UINT ilen;
while (len = jp2_getbox(buf, type, bBigEndian)) {
UINT hlen = len -= JP2_BOX_HDRLEN;
if (type == JP2_BOX_JP2C) {
jp2cfound = true;
FrameNo++;
// JP2C
if (*(UINT*)buf == 0) {
if (!skip_jp2c(bBigEndian)) return FALSE;
continue;
}
}
if (type != JP2_BOX_JP2H) {
fseek(infile,len,SEEK_CUR);
continue;
}
// entering super-box
while ((len = jp2_getbox(buf, type, bBigEndian))
> 0 && hlen >= len) {
hlen -= len;
if (*(UINT*)buf == 0) hlen -= sizeof(UINT64);
len -= JP2_BOX_HDRLEN;
switch (type) {
case JP2_BOX_IHDR:
found = true;
ipos = ftell(infile);
ilen = len;
fseek(infile,len+hlen,SEEK_CUR);
break;
default:
fseek(infile,len,SEEK_CUR);
break;
}
if (found) break;
}
}
if (!found || !jp2cfound) return FALSE;
// 3a) IHDR box
fseek(infile,ipos,SEEK_SET);
len = ilen;
flags = (len > 0)? 0 : -1;
dataflag = !(flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
if (!dataflag) return FALSE;
if (len > sizeof(buf)) return FALSE;
if (len < JP2_IHDR_LEN-JP2_BOX_HDRLEN) return FALSE;
if (fread(buf, len, 1, infile) != 1) return FALSE;
UINT width, height;
USHORT numcmpts;
height = *((UINT*)buf);
width = *((UINT*)buf+1);
numcmpts = *(USHORT*)((UINT*)buf+2);
if (bBigEndian) height = UINTSWAP(height);
if (bBigEndian) width = UINTSWAP(width);
if (bBigEndian) numcmpts = WORDSWAP(numcmpts);
// data for full-resolution image data! or any other level...
pFmt->sizeX = width;
pFmt->sizeY = height;
pFmt->framenum = FrameNo;
return TRUE;
}
// Retrieve image format
HRESULT GetJp2Format(LPCTSTR fileName, LPIMAGEFORMAT pFmt)
{
while (BusyUseStatics); BusyUseStatics = true;
Jp2Status = 0;
Jp2NumWarnings = 0;
if (pFmt==NULL) ErrorCode(-1);
int minifactor = 1;
int frameidx = 0, FrameNo;
FILE * input_file = NULL;
if (lstrlen(fileName) == 0) ErrorCode(-1);
if ((input_file = fopen(fileName, "rb")) == NULL)
ErrorCode(-1);
if (pFmt)
frameidx = pFmt->frameidx;
if (!get_JPEG2000_info(input_file, pFmt))
{
if (input_file != NULL)
fclose(input_file);
BusyUseStatics = false;
return E_FAIL;
}
pFmt->colors = 32;
FrameNo = pFmt->framenum;
pFmt->frameidx = frameidx<0? 0: frameidx>FrameNo? 0: frameidx;
if (input_file != NULL)
fclose(input_file);
HRESULT hr=(Jp2Status==0 && Jp2NumWarnings==0)?S_OK:S_FALSE;
BusyUseStatics = false;
return hr;
}
// Load image from file
HRESULT LoadJp2(LPCTSTR fileName, LPSAFEARRAY *saImage, LPIMAGEFORMAT pFmt /*= NULL*/)
{
// check for valid input parameters
if (lstrlen(fileName) == 0)
return E_FAIL;
if (!saImage || !*saImage)
return E_FAIL;
if (SafeArrayGetDim(*saImage) != 3)
return E_FAIL;
// variables
long lLbound, lUbound;
// read image size
SafeArrayGetLBound(*saImage,1,&lLbound);
SafeArrayGetUBound(*saImage,1,&lUbound);
// check for valid number of channels in the image
if (lUbound - lLbound != 3)
return E_FAIL;
// clear error and warning storages
Jp2Status = 0;
Jp2NumWarnings = 0;
// initialize jasper library
jas_init();
// connecto to the file
jas_stream_t *input_file = jas_stream_fopen(fileName, "rb");
// check for valid opened file
if (!input_file)
{
jas_image_clearfmts();
return E_FAIL;
}
// search format header
jas_image_getfmt(input_file);
// read format from file
int infmt = jas_image_getfmt(input_file);
// check for valid format
if (infmt < 0)
{
if (input_file) jas_stream_close(input_file);
jas_image_clearfmts();
return E_FAIL;
}
// variables
int minifactor = 1;
int frameidx = 0;
// figure out the output format, and set up to write it
ZeroMemory(&BmpFormat, sizeof(BmpFormat));
// lock the array
if (SafeArrayLock(*saImage) != S_OK)
{
if (input_file) jas_stream_close(input_file);
jas_image_clearfmts();
return E_FAIL;
}
// read image width
SafeArrayGetLBound(*saImage, 2, &lLbound);
SafeArrayGetUBound(*saImage, 2, &lUbound);
BmpFormat.sizeX = lUbound - lLbound + 1;
// read image height
SafeArrayGetLBound(*saImage, 3, &lLbound);
SafeArrayGetUBound(*saImage, 3, &lUbound);
BmpFormat.sizeY = lUbound - lLbound + 1;
// setup image data pointer
BmpFormat.image_ptr = (UCHAR*)((*saImage)->pvData);
BmpFormat.colors = 32;
// read image from file
jas_image_t *image = jas_image_decode(input_file, infmt, NULL);
// variables
int errors = 0;
// check for valid iamge
if (!image)
errors = 1;
else
{
// put the image to joe's decoder
errors = bmp_putdata((LPSTR)BmpFormat.image_ptr, image, BmpFormat.sizeX, BmpFormat.sizeY, frameidx, minifactor);
}
// close all resources
if (input_file) jas_stream_close(input_file);
if (image) jas_image_destroy(image);
jas_image_clearfmts();
// unlock the array
if (SafeArrayUnlock(*saImage) != S_OK)
return E_FAIL;
// check for gained errors
if (errors || Jp2Status != 0 || Jp2NumWarnings != 0)
return E_FAIL;
// all ok
return S_OK;
}
// Save image to file
HRESULT SaveJp2(LPCTSTR fileName, LPSAFEARRAY *saImage, LPIMAGEFORMAT pFmt /*= NULL*/)
{
// FILE * output_file = NULL;
while (BusyUseStatics); BusyUseStatics = true;
Jp2Status = 0;
Jp2NumWarnings = 0;
// long num_scanlines;
long lLbound, lUbound;
jas_image_t *image;
// cmdopts_t *cmdopts;
jas_stream_t *output_file;
if (saImage==NULL || *saImage==NULL) ErrorCode(-1);
if (SafeArrayGetDim(*saImage)!= 3) ErrorCode(-1);
SafeArrayGetLBound(*saImage,1,&lLbound);
SafeArrayGetUBound(*saImage,1,&lUbound);
if (lUbound-lLbound!=3) ErrorCode(-1);
if (lstrlen(fileName) == 0) ErrorCode(-1);
joe_FAKE_JUSTINTIMEDEBUGGERS();
//__try {
jas_init();
if ((output_file = jas_stream_fopen(fileName, "w+b")) == NULL) {
// MessageBox( GetActiveWindow(),
// "Jp2DLL: can't open output file\n",
// NULL, MB_ICONQUESTION | MB_OK);
jas_image_clearfmts();
ErrorCode(-1); //exit(EXIT_FAILURE);
}
int outfmt;
if ((outfmt = jas_image_strtofmt("jp2")) < 0)
{ jas_stream_close(output_file);
jas_image_clearfmts(); ErrorCode(-1); }
/* Initialize the JPEG compression object with default error handling. */
/* Figure out the input format, and set up to read it. */
ZeroMemory(&BmpFormat, sizeof(BmpFormat));
if ( SafeArrayLock(*saImage) != S_OK )
{ jas_stream_close(output_file);
jas_image_clearfmts(); ErrorCode(-1); }
SafeArrayGetLBound(*saImage,2,&lLbound);
SafeArrayGetUBound(*saImage,2,&lUbound);
BmpFormat.sizeX = lUbound - lLbound + 1;
SafeArrayGetLBound(*saImage,3,&lLbound);
SafeArrayGetUBound(*saImage,3,&lUbound);
BmpFormat.sizeY = lUbound - lLbound + 1;
BmpFormat.image_ptr = (UCHAR *)(*saImage)->pvData;
BmpFormat.colors = 32;
*outoptsbuf = '\0';
// 𸪨
if (pFmt) {
// if (pFmt->out_color_space == 1) // Force monochrome output.
// bGrayscale = true;
// bit_depth = pFmt->out_bit_depth;
if (pFmt->quality<100) { // Quality factor
double dq = (pFmt->quality-100.0)/25.0;
dq = pow(10,dq);
sprintf(outoptsbuf,"rate=%.4g ",dq);
}
}
// if (BmpFormat.sizeX > 256)
// strcat(outoptsbuf,"tilewidth=256 ");
// if (BmpFormat.sizeY > 256)
// strcat(outoptsbuf,"tileheight=256 ");
/* Process data */
int errors = 0;
if (!(image = bmp_getdata((LPSTR)BmpFormat.image_ptr,
BmpFormat.sizeX, BmpFormat.sizeY)))
errors = -1;
if (!errors)
errors = jas_image_encode(image, output_file,
outfmt, outoptsbuf);
jas_stream_flush(output_file);
if (output_file != NULL) jas_stream_close(output_file);
if (image) jas_image_destroy(image);
jas_image_clearfmts();
if (errors) {
if ( SafeArrayUnlock(*saImage) != S_OK ) ErrorCode(-1);
BusyUseStatics = false;
return E_FAIL;
}
//}
//__except (-1) { }
if ( SafeArrayUnlock(*saImage) != S_OK ) ErrorCode(-1);
HRESULT hr=(Jp2Status==0 && Jp2NumWarnings==0)?S_OK:S_FALSE;
BusyUseStatics = false;
return hr;
}
// Convert color to grayscale : Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
static void ColorToGrayscale(LPSTR pmem, int cx, int cy)
{
int x, y;
UCHAR *p, *pm, col, r, g, b;
ULONG lcol;
p = pm = (UCHAR*)pmem;
for (y = 0; y < cy; y++)
for (x = 0; x < cx; x++) {
b = *p++, g = *p++, r = *p++;
lcol = (114 * b +587 * g +299 * r + 500)/1000;
if (lcol > 255) lcol = 255;
col = (UCHAR) lcol;
*pm++ = col, *pm++ = col, *pm++ = col;
pm++, p++;
}
}
// stdafx.cpp : source file that includes just the standard includes
// LibJ2K.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <atlbase.h>
#include <atlcom.h>
#include <atlsafe.h>
#include <atltypes.h>
#include <WindowsX.h>
#include <comdef.h>
#include <jasper/jasper.h>
#include <../jp2/jp2_cod.h>
#include <../jpc/jpc_cs.h>
/////////////////////////////////////////////////////////////////////////////
#pragma once
/////////////////////////////////////////////////////////////////////////////
// Appendix functions
struct ImageFormat
{
int picture_type; // picture type
int picture_subtype; // clipboard image type
// common variables
long colors;
long sizeX;
long sizeY;
long format; // format details (Win-OS/2 for Bmp)
int out_color_space; // colorspace for output (1=gray, >1=color)
int out_bit_depth; // colordepth for output (<=8, paletted)
unsigned int scale_num; // always=1
unsigned int scale_denom; // fraction by which to scale image
BOOL bUseFSdither; // for paletted image sabings
// internal common variables
UCHAR* image_ptr;
BOOL PropDefaults;
BOOL PreviewAllowed;
long DesiredSizeX;
long DesiredSizeY;
int OEMcompression; // Tiff compression type
DWORD CustData; // temporary data
// unused or unnecessary
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
// JPEG, PNG variables
int quality; // JPEG(0..100), PNG(0..9=>0..100)
BOOL simple_progressive;
// PNG variables
int filter; // 0 - default, 1..5
// GIF variables
int framenum; // in multiimage GIFs: 0=count of images
int frameidx; // 0=all images at once (screen size), >0: index of image
// BMP, TIFF variables
int compression; // 0-based index of compression type
// TIFF variables
int byteorder; // (0=IBM-PC(default), 1-Mac, 2-host)
int planarconfig; // contiguous(default)/separate
BOOL tiled; // stripped(default)/tiled image
int stripsize; // size of strip buffer (index of control)
int tilesize; // size of tile (0-based index in combo)
};
typedef struct ImageFormat* LPIMAGEFORMAT;
HRESULT GetJp2Format(LPCTSTR fileName, LPIMAGEFORMAT pFmt);
HRESULT LoadJp2(LPCTSTR fileName, LPSAFEARRAY *saImage, LPIMAGEFORMAT pFmt = NULL);
HRESULT SaveJp2(LPCTSTR fileName, LPSAFEARRAY *saImage, LPIMAGEFORMAT pFmt = NULL);
/////////////////////////////////////////////////////////////////////////////
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment