diff options
author | scuri <scuri> | 2008-10-17 06:10:15 +0000 |
---|---|---|
committer | scuri <scuri> | 2008-10-17 06:10:15 +0000 |
commit | 5a422aba704c375a307a902bafe658342e209906 (patch) | |
tree | 5005011e086bb863d8fb587ad3319bbec59b2447 /test |
First commit - moving from LuaForge to SourceForge
Diffstat (limited to 'test')
-rw-r--r-- | test/glut_capture.c | 415 | ||||
-rw-r--r-- | test/glut_capture.vcproj | 160 | ||||
-rw-r--r-- | test/im_copy.cpp | 145 | ||||
-rw-r--r-- | test/im_copy.vcproj | 125 | ||||
-rw-r--r-- | test/im_info.cpp | 211 | ||||
-rw-r--r-- | test/im_info.mak | 11 | ||||
-rw-r--r-- | test/im_info.vcproj | 191 | ||||
-rw-r--r-- | test/im_view.c | 177 | ||||
-rw-r--r-- | test/im_view.mak | 16 | ||||
-rw-r--r-- | test/im_view.vcproj | 194 | ||||
-rw-r--r-- | test/iupglview.c | 237 | ||||
-rw-r--r-- | test/lua/analyze.lua | 28 | ||||
-rw-r--r-- | test/lua/capture.lua | 63 | ||||
-rw-r--r-- | test/lua/error.lua | 10 | ||||
-rw-r--r-- | test/lua/fft.lua | 17 | ||||
-rw-r--r-- | test/lua/flower.jpg | bin | 0 -> 17915 bytes | |||
-rw-r--r-- | test/lua/index.lua | 18 | ||||
-rw-r--r-- | test/lua/info.lua | 149 | ||||
-rw-r--r-- | test/lua/lena.jpg | bin | 0 -> 7145 bytes | |||
-rw-r--r-- | test/lua/palette.lua | 6 | ||||
-rw-r--r-- | test/lua/process.lua | 50 | ||||
-rw-r--r-- | test/lua/process_new.lua | 44 | ||||
-rw-r--r-- | test/lua/render.lua | 50 | ||||
-rw-r--r-- | test/lua/render_cd.lua | 15 | ||||
-rw-r--r-- | test/lua/screencapture.lua | 13 | ||||
-rw-r--r-- | test/lua/show_flower.wlua | 31 | ||||
-rw-r--r-- | test/lua/view.wlua | 158 |
27 files changed, 2534 insertions, 0 deletions
diff --git a/test/glut_capture.c b/test/glut_capture.c new file mode 100644 index 0000000..80aadd2 --- /dev/null +++ b/test/glut_capture.c @@ -0,0 +1,415 @@ +/* GLUT Capture Sample + + Uses GLUT for user interface + OpenGL for drawing + IM for image I/O and capture + + Needs "opengl32.lib", "glu32.lib", "glut32.lib", "vfw32.lib", "strmiids.lib", + "im.lib", "im_capture.lib", "im_avi.lib" and "im_process.lib". + + Control Keys: + + <Esc> - Terminates + <Space> - Activates/Deactivates the capturing. + <F1>, <F2> , <F3>, etc - Shows capture configuration dialogs, in general 2, but can have more. + <v> - Starts to save every frame in an AVI file. + <b> - Process a background image using an average of N frames. + <s> - Saves the background image in a BMP file. + <1>, <2>, etc - Activates an processing operation. + Only operation 1 is working, it subtracts the background image if one was created. + <0> - Deactivates all the processing operations. + + ATENTION: These keys works at the GLUT window. + But the text input in done at the console window. + Check the correct window focus before typing keys. +*/ + +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <memory.h> + +#include <GL/glut.h> +#include <im.h> +#include <im_capture.h> +#include <im_image.h> +#include <im_convert.h> +#include <im_process.h> +#include <im_format_avi.h> +#include <im_util.h> + + +/* Global Variables */ +imVideoCapture* myVideoCap; /* capture control */ +imImage* image = NULL; /* capture buffer */ +unsigned char* gl_data = NULL; /* opengl display buffer */ + +char video_filename[512] = ""; +imFile* video_file = NULL; + +imImage* back_image = NULL; /* background image */ +imImage* back_acum = NULL; /* aux image for background image calculation */ +int back_count = 0; /* number of images to average */ +int back_index = 0; /* average image counter */ + +int user_key[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + +static void SimpleBackSub(imbyte *map, imbyte *back_map, int count, float tol) +{ + int i; + for (i = 0; i < count; i++) + { + int diff = map[i] - back_map[i]; + if (diff < 0) diff = -diff; + + if(diff <= tol) + map[i] = 0; + } +} + +static float tol = 10; /* you should use some key to change this */ + +void capture_process(int* user_key, imImage* image, imImage* back_image) +{ + if (user_key[0] && back_image) /* '1' */ + { + int i; + for (i = 0; i < image->depth; i++) /* notice that here depth is always 3 */ + { + SimpleBackSub((imbyte*)image->data[i], (imbyte*)back_image->data[i], image->count, tol); + } + } + + /***** call other operations here ******/ +} + + +/* Aux to draw a number in the display */ +void display_number(int num) +{ + int i; + char msg[30]; + sprintf(msg,"%4d", num); + glColor3f(1.0f,0.0f,0.0f); + glRasterPos2f(10.f,10.f); + for(i = 0; msg[i]; i++) + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, msg[i]); +} + +/* GLUT display callback */ +/* called everytime the window needs to be updated */ +void display(void) +{ + if (!image) + return; + + /* Draws the captured image at (0,0) */ + glRasterPos2f(0.f, 0.f); + glDrawPixels(image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, gl_data); + + glutSwapBuffers(); +} + + +/* GLUT reshape callback */ +/* called everytime the window changes its size */ +void reshape(int w, int h) +{ + glViewport(0, 0, w, h); +} + + +/* GLUT idle callback */ +/* called when there is no events to be processed */ +void idle(void) +{ + if (imVideoCaptureLive(myVideoCap, -1)) + { + imVideoCaptureFrame(myVideoCap, image->data[0], IM_RGB, 1000); + + if (back_image && back_index < back_count) + { + /* calculating the background image */ + + imProcessUnArithmeticOp(image, back_acum, IM_UN_INC); /* back_image += image */ + back_index++; + + if (back_index == back_count) /* last sum, divide by N */ + { + imProcessArithmeticConstOp(back_acum, (float)back_count, back_image, IM_BIN_DIV); + printf("Background image updated.\n"); + } + } + else + { + /* call some processing */ + capture_process(user_key, image, back_image); + + if (video_file) + { + imFileWriteImageInfo(video_file, image->width, image->height, IM_RGB, IM_BYTE); + imFileWriteImageData(video_file, image->data[0]); + } + } + + imConvertPacking(image->data[0], gl_data, image->width, image->height, image->depth, image->data_type, 0); + display(); + } +} + + +/* OpenGL initialization */ +void glinit(void) +{ + if (!image) + return; + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D (0.0, (GLdouble)image->width, 0.0, (GLdouble)image->height); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + +/* updates the capture image size and display buffer size */ +void updatebuffer(void) +{ + int width, height; + + /* retrieve the image size */ + imVideoCaptureGetImageSize(myVideoCap, &width, &height); + + if (width != image->width || height != image->height) + { + /* fix the buffer size */ + imImageReshape(image, width, height); + gl_data = realloc(gl_data, image->size); + + /* fix the window size */ + glutReshapeWindow(image->width, image->height); + + /* re-inititalizes the OpenGL */ + glinit(); + } +} + + +/* GLUT function key callback */ +/* called everytime a function key is pressed */ +void parsefunckey(int key, int x, int y) +{ + switch (key) { + case GLUT_KEY_F1: /* F1, F2, F.. = shows the capture configuration dialogs */ + case GLUT_KEY_F2: + case GLUT_KEY_F3: + case GLUT_KEY_F4: + case GLUT_KEY_F5: + case GLUT_KEY_F6: + case GLUT_KEY_F7: + case GLUT_KEY_F8: + imVideoCaptureLive(myVideoCap, 0); /* deactivate the capture before calling the dialog */ + imVideoCaptureShowDialog(myVideoCap, key - GLUT_KEY_F1, NULL); + updatebuffer(); + imVideoCaptureLive(myVideoCap, 1); + break; + } +} + +/* GLUT key callback */ +/* called everytime an ASCII key is pressed */ +void parsekey(unsigned char key, int x, int y) +{ + int error, index; + switch (key) { + case 27: /* Esc = terminates */ + printf("\nTerminating...\n"); + imVideoCaptureDisconnect(myVideoCap); + imVideoCaptureDestroy(myVideoCap); + imImageDestroy(image); + if (video_file) + { + imFileClose(video_file); + printf("AVI file created.\n"); + } + free(gl_data); + exit(1); + case ' ': /* Space = activates/deactivates the capturing */ + if (imVideoCaptureLive(myVideoCap, -1)) + imVideoCaptureLive(myVideoCap, 0); + else + imVideoCaptureLive(myVideoCap, 1); + break; + case 'v': + if (video_file) + { + imFileClose(video_file); + printf("AVI file created.\n"); + video_file = NULL; + break; + } + printf("Enter the AVI file name:\n >"); + scanf("%s", video_filename); + video_file = imFileNew(video_filename, "AVI", &error); + if (!video_file) + printf("Error creating video file.\n"); + else + { + imFileSetInfo(video_file, "CUSTOM"); /* shows the compression options dialog */ + imFileWriteImageInfo(video_file, image->width, image->height, IM_RGB, IM_BYTE); + } + break; + case 'b': + if (back_image) + { + imImageDestroy(back_image); + imImageDestroy(back_acum); + } + printf("Enter the number of images to average:\n >"); + scanf("%d", &back_count); + back_acum = imImageCreate(image->width, image->height, IM_RGB, IM_USHORT); + back_image = imImageClone(image); + back_index = 0; + break; + case 's': + if (back_image) + { + char filename[512]; + imFile* ifile; + printf("Enter the BMP file name:\n >"); + scanf("%s", filename); + ifile = imFileNew(filename, "BMP", &error); + if (!ifile) { + printf("Error creating image file.\n"); return; + } + imFileSaveImage(ifile, back_image); + imFileClose(ifile); + printf("BMP file created.\n"); + } + break; + case '0': + memset(user_key, 0, 9*sizeof(int)); + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + index = key - '1'; + user_key[index] = user_key[index]? 0: 1; /* switch state */ + if (user_key[index]) + printf("Processing %c activated. \n", key); + else + printf("Processing %c deactivated. \n", key); + return; + default: + glutPostRedisplay(); + return; + } +} + + +/* Returns a capture device */ +int getcapture(void) +{ + int i; + int cap_count = imVideoCaptureDeviceCount(); + if (cap_count == 1) /* only one device */ + return 0; + + printf("Enter the capture device number to use:\n"); + for (i = 0; i < cap_count; i++) + { + printf(" %s\n", imVideoCaptureDeviceDesc(i)); + } + + printf(" > "); + scanf("%d", &i); + if (i < 0 || i >= cap_count) + return 0; + + return i; +} + + +/* Initializes the capture device */ +int initcapture(void) +{ + int width, height; + + /* creates an IM video capture manager */ + myVideoCap = imVideoCaptureCreate(); + if (!myVideoCap) { + printf("No capture device found.\n"); return 0; + } + + /* connects the device */ + if (!imVideoCaptureConnect(myVideoCap, getcapture())) { + imVideoCaptureDestroy(myVideoCap); + printf("Can not connect to capture device.\n"); return 0; + } + + if (!imVideoCaptureLive(myVideoCap, 1)) { + imVideoCaptureDisconnect(myVideoCap); + imVideoCaptureDestroy(myVideoCap); + printf("Can not activate capturing.\n"); return 0; + } + + /* retrieve the image size */ + imVideoCaptureGetImageSize(myVideoCap, &width, &height); + + /* alocates the buffers */ + image = imImageCreate(width, height, IM_RGB, IM_BYTE); + gl_data = malloc(image->size); + + return 1; +} + + +int main(int argc, char* argv[]) +{ + printf("GLUT Capture\n"); + printf(" <Esc> - Terminates.\n" + " <Space> - Activates/Deactivates the capturing.\n" + " <F1>, <F2> , <F3>, ... - Shows capture configuration dialogs.\n" + " <v> - Starts to save every frame in an AVI file.\n" + " <b> - Process a background image using an average of N frames.\n" + " <s> - Saves the background image in a BMP file.\n" + " <1>, <2>, ... - Activates an processing operation.\n" + " <0> - Deactivates all the processing operations.\n\n"); + + /* Initializes the capture device */ + if (!initcapture()) + return 1; + + imFormatRegisterAVI(); + + /* GLUT initialization */ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutInitWindowPosition(100, 100); + glutInitWindowSize(image->width, image->height); + glutCreateWindow("GLUT Capture"); + + glClearColor(0., 0., 0., 1.0); /* window background */ + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* data alignment is 1 */ + + /* register GLUT callbacks */ + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutKeyboardFunc(parsekey); + glutSpecialFunc(parsefunckey); + glutIdleFunc(idle); + + /* OpenGL initialization */ + glinit(); + + /* GLUT message loop */ + glutMainLoop(); + + return 0; +} diff --git a/test/glut_capture.vcproj b/test/glut_capture.vcproj new file mode 100644 index 0000000..33e297e --- /dev/null +++ b/test/glut_capture.vcproj @@ -0,0 +1,160 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="glut_capture" + ProjectGUID="{A2B0F595-B494-49DD-A978-0E70CF27F2AD}" + SccProjectName=""$/glut_capture", VAAAAAAA" + SccLocalPath="." + Keyword="MFCProj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory=".\Debug" + IntermediateDirectory=".\Debug" + ConfigurationType="1" + UseOfMFC="0" + ATLMinimizesCRunTimeLibraryUsage="FALSE" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="..\..\include" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + BasicRuntimeChecks="3" + RuntimeLibrary="4" + UsePrecompiledHeader="0" + PrecompiledHeaderThrough="stdafx.h" + PrecompiledHeaderFile=".\Debug/glut_capture.pch" + AssemblerListingLocation=".\Debug/" + ObjectFile=".\Debug/" + ProgramDataBaseFileName=".\Debug/" + WarningLevel="3" + SuppressStartupBanner="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + AdditionalOptions="/MACHINE:I386" + AdditionalDependencies="glut32.lib vfw32.lib strmiids.lib im.lib im_capture.lib im_avi.lib im_process.lib" + OutputFile=".\Debug/glut_capture.exe" + LinkIncremental="2" + SuppressStartupBanner="TRUE" + AdditionalLibraryDirectories="D:\tecgraf\im\lib\vc7" + IgnoreAllDefaultLibraries="FALSE" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile=".\Debug/glut_capture.pdb" + SubSystem="1"/> + <Tool + Name="VCMIDLTool" + TypeLibraryName=".\Debug/glut_capture.tlb"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool" + PreprocessorDefinitions="_AFXDLL;_DEBUG" + Culture="1033"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory=".\Release" + IntermediateDirectory=".\Release" + ConfigurationType="1" + UseOfMFC="1" + ATLMinimizesCRunTimeLibraryUsage="FALSE" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + InlineFunctionExpansion="1" + AdditionalIncludeDirectories="..\im\include,..\glut\include" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="TRUE" + RuntimeLibrary="4" + EnableFunctionLevelLinking="TRUE" + UsePrecompiledHeader="0" + PrecompiledHeaderThrough="" + PrecompiledHeaderFile="" + AssemblerListingLocation=".\Release/" + ObjectFile=".\Release/" + ProgramDataBaseFileName=".\Release/" + WarningLevel="4" + SuppressStartupBanner="TRUE"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + AdditionalOptions="/MACHINE:I386" + AdditionalDependencies="vfw32.lib" + OutputFile=".\Release/glut_capture.exe" + LinkIncremental="1" + SuppressStartupBanner="TRUE" + AdditionalLibraryDirectories="" + ProgramDatabaseFile=".\Release/glut_capture.pdb" + SubSystem="1"/> + <Tool + Name="VCMIDLTool" + TypeLibraryName=".\Release/glut_capture.tlb"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool" + PreprocessorDefinitions="_AFXDLL;NDEBUG" + Culture="1033"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="sources" + Filter=""> + <File + RelativePath="glut_capture.c"> + <FileConfiguration + Name="Debug|Win32" + ExcludedFromBuild="TRUE"> + <Tool + Name="VCCLCompilerTool"/> + </FileConfiguration> + </File> + <File + RelativePath=".\main.cpp"> + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/test/im_copy.cpp b/test/im_copy.cpp new file mode 100644 index 0000000..dd0b82e --- /dev/null +++ b/test/im_copy.cpp @@ -0,0 +1,145 @@ +/* IM 3 sample that copies an image from one file to another. + It is good to test the file formats read and write. + If the destiny does not supports the input image it aborts and returns an error. + + Needs "im.lib". + + Usage: im_copy <input_file_name> <output_file_name> [<output_format> [<output_compression]] + + Example: im_copy test.tif test_proc.tif +*/ + +#include <im.h> +#include <im_util.h> +#include <im_format_avi.h> +#include <im_format_wmv.h> + +#include <stdio.h> +#include <stdlib.h> + + +void PrintError(int error) +{ + switch (error) + { + case IM_ERR_OPEN: + printf("Error Opening File.\n"); + break; + case IM_ERR_MEM: + printf("Insuficient memory.\n"); + break; + case IM_ERR_ACCESS: + printf("Error Accessing File.\n"); + break; + case IM_ERR_DATA: + printf("Image type not Suported.\n"); + break; + case IM_ERR_FORMAT: + printf("Invalid Format.\n"); + break; + case IM_ERR_COMPRESS: + printf("Invalid or unsupported compression.\n"); + break; + default: + printf("Unknown Error.\n"); + } +} + +int main(int argc, char* argv[]) +{ + if (argc < 3) + { + printf("Invalid number of arguments.\n"); + return 0; + } + + imFormatRegisterAVI(); + imFormatRegisterWMV(); + + void* data = NULL; + imFile* ifile = NULL; + imFile* ofile = NULL; + + int error; + ifile = imFileOpen(argv[1], &error); + if (!ifile) + goto man_error; + + char format[10]; + char compression[20]; + int image_count; + imFileGetInfo(ifile, format, compression, &image_count); + + ofile = imFileNew(argv[2], (argc < 3)? format: argv[3], &error); + if (!ofile) + goto man_error; + + if (argc < 4) + imFileSetInfo(ofile, compression); + else + imFileSetInfo(ofile, argv[4]); + + for (int i = 0; i < image_count; i++) + { + int size, max_size = 0; + int width, height, color_mode, data_type; + error = imFileReadImageInfo(ifile, i, &width, &height, &color_mode, &data_type); + if (error != IM_ERR_NONE) + goto man_error; + + size = imImageDataSize(width, height, color_mode, data_type); + + if (size > max_size) + { + data = realloc(data, size); + max_size = size; + } + + error = imFileReadImageData(ifile, data, 0, -1); + if (error != IM_ERR_NONE) + goto man_error; + + char* attrib_list[50]; + int attrib_list_count; + imFileGetAttributeList(ifile, attrib_list, &attrib_list_count); + + for (int a = 0; a < attrib_list_count; a++) + { + int attrib_data_type, attrib_count; + const void* attrib_data = imFileGetAttribute(ifile, attrib_list[a], &attrib_data_type, &attrib_count); + imFileSetAttribute(ofile, attrib_list[a], attrib_data_type, attrib_count, attrib_data); + } + + if (imColorModeSpace(color_mode) == IM_MAP) + { + long palette[256]; + int palette_count; + imFileGetPalette(ifile, palette, &palette_count); + imFileSetPalette(ifile, palette, palette_count); + } + + error = imFileWriteImageInfo(ofile, width, height, color_mode, data_type); + if (error != IM_ERR_NONE) + goto man_error; + + error = imFileWriteImageData(ofile, data); + if (error != IM_ERR_NONE) + goto man_error; + + printf("."); + } + printf("done"); + + free(data); + imFileClose(ifile); + imFileClose(ofile); + + return 1; + +man_error: + PrintError(error); + if (data) free(data); + if (ifile) imFileClose(ifile); + if (ofile) imFileClose(ofile); + return 0; +} diff --git a/test/im_copy.vcproj b/test/im_copy.vcproj new file mode 100644 index 0000000..051b110 --- /dev/null +++ b/test/im_copy.vcproj @@ -0,0 +1,125 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="im_copy" + ProjectGUID="{909637FA-8229-45A9-9F42-53D8ED5F91C5}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="1" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="../../include" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="4" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="im.lib im_avi.lib im_wmv.lib vfw32.lib wmvcore.lib" + OutputFile="$(OutDir)/im_copy.exe" + LinkIncremental="2" + AdditionalLibraryDirectories="C:\APP\LNG\WMFSDK9\lib;../../lib/vc7" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/im_copy.pdb" + SubSystem="1" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="1" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + RuntimeLibrary="4" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/im_copy.exe" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath=".\im_copy.cpp"> + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/test/im_info.cpp b/test/im_info.cpp new file mode 100644 index 0000000..89ec8c4 --- /dev/null +++ b/test/im_info.cpp @@ -0,0 +1,211 @@ +/* IM 3 sample that returns information about a file. + + Needs "im.lib". + + Usage: im_info <file_name> + + Example: im_info test.tif +*/ + +#include <im.h> +#include <im_util.h> +#include <im_binfile.h> +#include <im_format_jp2.h> +#include <im_format_avi.h> +#include <im_format_wmv.h> + +#include <stdio.h> +#include <stdlib.h> +#include <memory.h> + +void PrintError(int error) +{ + switch (error) + { + case IM_ERR_OPEN: + printf("Error Opening File.\n"); + break; + case IM_ERR_MEM: + printf("Insuficient memory.\n"); + break; + case IM_ERR_ACCESS: + printf("Error Accessing File.\n"); + break; + case IM_ERR_DATA: + printf("Image type not Suported.\n"); + break; + case IM_ERR_FORMAT: + printf("Invalid Format.\n"); + break; + case IM_ERR_COMPRESS: + printf("Invalid or unsupported compression.\n"); + break; + default: + printf("Unknown Error.\n"); + } +} + +int FindZero(imbyte* data, int count) +{ + for (int i = 0; i < count; i++) + { + if (data[i] == 0) + return 1; + } + return 0; +} + +char* AttribData2Str(const void* data, int data_type) +{ + static char data_str[50] = ""; + + switch(data_type) + { + case IM_BYTE: + sprintf(data_str, "%3d", (int)(*((imbyte*)data))); + break; + case IM_USHORT: + sprintf(data_str, "%5d", (int)(*((imushort*)data))); + break; + case IM_INT: + sprintf(data_str, "%5d", *((int*)data)); + break; + case IM_FLOAT: + sprintf(data_str, "%5.2f", (double)(*((float*)data))); + break; + case IM_CFLOAT: + { + float *c = (float*)data; + sprintf(data_str, "%5.2g, %5.2f", (double)*c, (double)*(c+1)); + } + break; + } + + return data_str; +} + +char* GetSizeDesc(double *size) +{ + char* size_desc; + + if (*size < 1024) + size_desc = "b"; + else + { + *size /= 1024; + + if (*size < 1024) + size_desc = "Kb"; + else + { + *size /= 1024; + size_desc = "Mb"; + } + } + + return size_desc; +} + +unsigned long FileSize(const char* file_name) +{ + imBinFile* bfile = imBinFileOpen(file_name); + if (!bfile) return 0; + + unsigned long file_size = imBinFileSize(bfile); + + imBinFileClose(bfile); + return file_size; +} + +void PrintImageInfo(const char* file_name) +{ + printf("IM Info\n"); + printf(" File Name:\n %s\n", file_name); + + int error; + imFile* ifile = imFileOpen(file_name, &error); + if (!ifile) + { + PrintError(error); + return; + } + + double file_size = FileSize(file_name); + printf(" File Size: %.2f %s\n", file_size, GetSizeDesc(&file_size)); + + char format[10]; + char compression[20]; + int image_count; + imFileGetInfo(ifile, format, compression, &image_count); + + char format_desc[50]; + imFormatInfo(format, format_desc, NULL, NULL); + printf(" Format: %s - %s\n", format, format_desc); + printf(" Compression: %s\n", compression); + printf(" Image Count: %d\n", image_count); + + for (int i = 0; i < image_count; i++) + { + int width, height, color_mode, data_type; + + error = imFileReadImageInfo(ifile, i, &width, &height, &color_mode, &data_type); + if (error != IM_ERR_NONE) + { + PrintError(error); + imFileClose(ifile); + return; + } + + printf(" Image #%d\n", i); + printf(" Width: %d\n", width); + printf(" Height: %d\n", height); + printf(" Color Space: %s\n", imColorModeSpaceName(color_mode)); + printf(" Has Alpha: %s\n", imColorModeHasAlpha(color_mode)? "Yes": "No"); + printf(" Is Packed: %s\n", imColorModeIsPacked(color_mode)? "Yes": "No"); + printf(" Is Top Down: %s\n", imColorModeIsTopDown(color_mode)? "Yes": "No"); + printf(" Data Type: %s\n", imDataTypeName(data_type)); + + double image_size = imImageDataSize(width, height, color_mode, data_type); + printf(" Data Size: %.2f %s\n", image_size, GetSizeDesc(&image_size)); + + char* attrib_list[50]; // should be dynamic allocated + int attrib_list_count; + imFileGetAttributeList(ifile, attrib_list, &attrib_list_count); + + for (int a = 0; a < attrib_list_count; a++) + { + if (a == 0) + printf(" Attributes:\n"); + + int attrib_data_type, attrib_count; + const void* attrib_data = imFileGetAttribute(ifile, attrib_list[a], &attrib_data_type, &attrib_count); + + if (attrib_count == 1) + printf(" %s: %s\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type)); + else if (attrib_data_type == IM_BYTE && FindZero((imbyte*)attrib_data, attrib_count)) + printf(" %s: %s\n", attrib_list[a], attrib_data); + else + printf(" %s: %s %s ...\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type), AttribData2Str((imbyte*)attrib_data + imDataTypeSize(attrib_data_type), attrib_data_type)); + } + } + + imFileClose(ifile); +} + +int main(int argc, char* argv[]) +{ +// imFormatRegisterJP2(); +// imFormatRegisterAVI(); +// imFormatRegisterWMV(); + + if (argc < 2) + { + printf("Invalid number of arguments.\n"); + return 0; + } + + PrintImageInfo(argv[1]); + + return 1; +} + diff --git a/test/im_info.mak b/test/im_info.mak new file mode 100644 index 0000000..211e7af --- /dev/null +++ b/test/im_info.mak @@ -0,0 +1,11 @@ +APPNAME = im_info +APPTYPE = CONSOLE +LINKER = g++ + +SRC = im_info.cpp + +USE_IM = Yes + +IM = .. + +USE_STATIC = Yes diff --git a/test/im_info.vcproj b/test/im_info.vcproj new file mode 100644 index 0000000..e78ee5a --- /dev/null +++ b/test/im_info.vcproj @@ -0,0 +1,191 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="8.00" + Name="im_info" + ProjectGUID="{909637FA-8229-45A9-9F42-53D8ED5F91C5}" + Keyword="Win32Proj" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="../../include" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="true" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="im.lib" + OutputFile="$(OutDir)/im_info.exe" + LinkIncremental="2" + AdditionalLibraryDirectories="C:\APP\LNG\WMFSDK9\lib;../../lib" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/im_info.pdb" + SubSystem="1" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="true" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/im_info.exe" + LinkIncremental="1" + GenerateDebugInformation="true" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\im_info.cpp" + > + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/test/im_view.c b/test/im_view.c new file mode 100644 index 0000000..f1804d8 --- /dev/null +++ b/test/im_view.c @@ -0,0 +1,177 @@ +/* IM 3 sample that shows an image. + + Needs "im.lib", "iup.lib", "cd.lib" and "cdiup.lib". + + Usage: im_view <file_name> + + Example: im_view test.tif + + Click on image to open another file. +*/ + +#include <iup.h> +#include <cd.h> +#include <cdiup.h> +#include <im.h> +#include <im_image.h> + +#include <stdio.h> +#include <string.h> + +static int disable_repaint = 0; /* used to optimize repaint, while opening a new file */ + +static void PrintError(int error) +{ + switch (error) + { + case IM_ERR_OPEN: + IupMessage("IM", "Error Opening File."); + break; + case IM_ERR_MEM: + IupMessage("IM", "Insuficient memory."); + break; + case IM_ERR_ACCESS: + IupMessage("IM", "Error Accessing File."); + break; + case IM_ERR_DATA: + IupMessage("IM", "Image type not Suported."); + break; + case IM_ERR_FORMAT: + IupMessage("IM", "Invalid Format."); + break; + case IM_ERR_COMPRESS: + IupMessage("IM", "Invalid or unsupported compression."); + break; + default: + IupMessage("IM", "Unknown Error."); + } +} + +static int cbRepaint(Ihandle* iup_canvas) +{ + cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(iup_canvas, "cdCanvas"); + imImage* image = (imImage*)IupGetAttribute(iup_canvas, "imImage"); + + if (!cd_canvas || disable_repaint) + return IUP_DEFAULT; + + cdCanvasActivate(cd_canvas); + cdCanvasClear(cd_canvas); + + if (!image) + return IUP_DEFAULT; + + imcdCanvasPutImage(cd_canvas, image, 0, 0, image->width, image->height, 0, 0, 0, 0); + + cdCanvasFlush(cd_canvas); + + return IUP_DEFAULT; +} + +static void ShowImage(char* file_name, Ihandle* iup_dialog) +{ + int error; + imImage* image = (imImage*)IupGetAttribute(iup_dialog, "imImage"); + if (image) imImageDestroy(image); + IupSetAttribute(iup_dialog, "imImage", NULL); + + image = imFileImageLoadBitmap(file_name, 0, &error); + if (error) PrintError(error); + if (!image) return; + + IupSetAttribute(iup_dialog, "imImage", (char*)image); + IupStoreAttribute(iup_dialog, "TITLE", file_name); + + cbRepaint(iup_dialog); /* we can do this because canvas inherit attributes from the dialog */ +} + +static int cbButton(Ihandle* iup_canvas, int but, int pressed) +{ + char file_name[200] = "*.*"; + + if (but != IUP_BUTTON1 || !pressed) + return IUP_DEFAULT; + + disable_repaint = 1; + if (IupGetFile(file_name) != 0) + { + disable_repaint = 0; + return IUP_DEFAULT; + } + + disable_repaint = 0; + ShowImage(file_name, IupGetDialog(iup_canvas)); + + return IUP_DEFAULT; +} + +static int cbClose(Ihandle* iup_dialog) +{ + cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(iup_dialog, "cdCanvas"); + imImage* image = (imImage*)IupGetAttribute(iup_dialog, "imImage"); + + if (cd_canvas) cdKillCanvas(cd_canvas); + if (image) imImageDestroy(image); + + IupSetAttribute(iup_dialog, "cdCanvas", NULL); + IupSetAttribute(iup_dialog, "imImage", NULL); + + return IUP_CLOSE; +} + +static Ihandle* CreateDialog(void) +{ + Ihandle *iup_dialog; + Ihandle *iup_canvas; + cdCanvas* cd_canvas; + + iup_canvas = IupCanvas("do_nothing"); + IupSetAttribute(iup_canvas, IUP_BUTTON_CB, "cbButton"); + IupSetAttribute(iup_canvas, IUP_ACTION, "cbRepaint"); + + iup_dialog = IupDialog(iup_canvas); + IupSetAttribute(iup_dialog, IUP_CLOSE_CB, "cbClose"); + IupSetAttribute(iup_dialog, IUP_SIZE, "HALFxHALF"); + + IupSetFunction("cbRepaint", (Icallback)cbRepaint); + IupSetFunction("cbButton", (Icallback)cbButton); + IupSetFunction("cbClose", (Icallback)cbClose); + + IupMap(iup_dialog); + + cd_canvas = cdCreateCanvas(CD_IUP, iup_canvas); + IupSetAttribute(iup_dialog, "cdCanvas", (char*)cd_canvas); + + return iup_dialog; +} + +int main(int argc, char* argv[]) +{ + Ihandle* dlg; + + IupOpen(); + + dlg = CreateDialog(); + + IupShow(dlg); + + /* Try to get a file name from the command line. */ + if (argc > 1) + { + ShowImage(argv[1], dlg); + } + else + { + char file_name[1024] = "*.*"; + if (IupGetFile(file_name) == 0) + { + ShowImage(file_name, dlg); + } + } + + IupMainLoop(); + IupDestroy(dlg); + IupClose(); + + return 1; +} diff --git a/test/im_view.mak b/test/im_view.mak new file mode 100644 index 0000000..6d4e8f7 --- /dev/null +++ b/test/im_view.mak @@ -0,0 +1,16 @@ +APPNAME = im_view + +LINKER = g++ + +SRC = im_view.c + +USE_CD = Yes +USE_IUPCONTROLS = Yes +USE_IUP = Yes +USE_IM = Yes + +IM = .. +IMINC := $(IM)/include +IMLIB := $(IM)/lib/$(TEC_UNAME) + +USE_STATIC = Yes diff --git a/test/im_view.vcproj b/test/im_view.vcproj new file mode 100644 index 0000000..26aeb22 --- /dev/null +++ b/test/im_view.vcproj @@ -0,0 +1,194 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9,00" + Name="im_view" + ProjectGUID="{909637FA-8229-45A9-9F42-53D8ED5F91C5}" + Keyword="Win32Proj" + TargetFrameworkVersion="0" + > + <Platforms> + <Platform + Name="Win32" + /> + <Platform + Name="x64" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="../../../iup/include;../../../cd/include;../../include" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="4" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="1" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="comctl32.lib im.lib iup.lib cd.lib cdiup.lib" + OutputFile="$(OutDir)/im_view.exe" + LinkIncremental="1" + AdditionalLibraryDirectories="../../lib;../../../iup/lib;../../../cd/lib" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/im_view.pdb" + SubSystem="1" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Debug|x64" + OutputDirectory="x64" + IntermediateDirectory="x64" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="../../../iup/include;../../../cd/include;../../include" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="4" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="1" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="comctl32.lib im.lib iup.lib cd.lib iupcd.lib" + OutputFile="$(OutDir)/im_view.exe" + LinkIncremental="1" + AdditionalLibraryDirectories="../../lib/x64;../../../iup/lib/vc9_64;../../../cd/lib/vc9_64" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/im_view.pdb" + SubSystem="1" + TargetMachine="0" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\im_view.c" + > + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/test/iupglview.c b/test/iupglview.c new file mode 100644 index 0000000..93e0054 --- /dev/null +++ b/test/iupglview.c @@ -0,0 +1,237 @@ + +#ifdef WIN32 +#include <windows.h> /* necessary because of the Microsoft OpenGL headers dependency */ +#endif + +#include <GL/gl.h> +#include <GL/glu.h> + +#include <stdlib.h> +#include <stdio.h> +#include <time.h> + +#include <iup.h> +#include <iupgl.h> + +#include <im.h> +#include <im_image.h> +#include <im_util.h> +#include <im_convert.h> + +int app_repaint_cb(Ihandle* self) +{ + unsigned char* gl_data = (unsigned char*)IupGetAttribute(self, "APP_GL_DATA"); + int width = IupGetInt(self, "APP_GL_WIDTH"); + int height = IupGetInt(self, "APP_GL_HEIGHT"); + IupGLMakeCurrent(self); /* activates this GL Canvas as the current drawing area. */ + glClear(GL_COLOR_BUFFER_BIT); /* clears the back buffer */ + + if (gl_data) + { + /* Draws the captured image at (0,0) */ + glRasterPos2f(0.f, 0.f); + glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, gl_data); + } + + IupGLSwapBuffers(self); /* swap data from back buffer to front buffer */ + return IUP_DEFAULT; +} + +void appGLInit(int width, int height) +{ + glClearColor(0., 0., 0., 1.0); /* window background */ + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* data alignment is 1 */ + + glViewport(0, 0, width, height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D (0.0, (GLdouble)width, 0.0, (GLdouble)height); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +int app_resize_cb(Ihandle* self, int width, int height) +{ + IupGLMakeCurrent(self); + appGLInit(width, height); + return IUP_DEFAULT; +} + +/* OpenGL does not supports palette based images, so convert to RGB */ +/* this function can also be use for RGBA images */ +void ConvertMapToGLData(unsigned char* data, int count, int depth, long* palette, int palette_count) +{ + int c, i; + unsigned char r[256], g[256], b[256]; + + unsigned char* src_data = data + count-1; + unsigned char* dst_data = data + depth*(count-1); + + for (c = 0; c < palette_count; c++) + imColorDecode(&r[c], &g[c], &b[c], palette[c]); + + for (i = 0; i < count; i++) + { + int index = *src_data; + *dst_data = r[index]; + *(dst_data+1) = g[index]; + *(dst_data+2) = b[index]; + + dst_data -= depth; + src_data--; + } +} + +int app_open_cb(Ihandle* self) +{ + imFile* ifile; /* file input */ + int ret, error; + unsigned char* gl_data = (unsigned char*)IupGetAttribute(self, "APP_GL_DATA"); + char filename[1024] = ".\\*.*"; + + /* get a file name */ + ret = IupGetFile(filename); + if (ret == -1) + return IUP_DEFAULT; + + ifile = imFileOpen(filename, &error); + if (!ifile) + { + IupMessage("Error", "Error reading image file."); + return IUP_DEFAULT; + } + + { + int width = 0, height = 0, file_color_mode, color_space; + Ihandle* dialog = IupGetDialog(self); + imFileReadImageInfo(ifile, 0, &width, &height, &file_color_mode, NULL); + + /* alocates the buffers */ + if (gl_data) free(gl_data); + gl_data = malloc(width*height*3); + IupSetAttribute(dialog, "APP_GL_DATA", gl_data); + IupSetfAttribute(dialog, "APP_GL_WIDTH", "%d", width); + IupSetfAttribute(dialog, "APP_GL_HEIGHT", "%d", height); + + imFileReadImageData(ifile, gl_data, 1, IM_PACKED); + + color_space = imColorModeSpace(file_color_mode); + if (color_space == IM_MAP || color_space == IM_GRAY || color_space == IM_BINARY) + { + long palette[256]; + int palette_count; + imFileGetPalette(ifile, palette, &palette_count); + ConvertMapToGLData(gl_data, width*height, 3, palette, palette_count); + } + } + + imFileClose(ifile); + + return IUP_DEFAULT; +} + +int app_exit_cb(Ihandle *self) +{ + unsigned char* gl_data = (unsigned char*)IupGetAttribute(self, "APP_GL_DATA"); + + /* destroy buffers */ + if (gl_data) + free(gl_data); + + return IUP_CLOSE; +} + +int app_about_cb(Ihandle *self) +{ + IupMessagef("About", "IUPGLView 1.0\n" + "Tecgraf/PUC-Rio\n" + " ---------------- \n" + "IUP Version %s\n" + "IM Version %s\n" + " ---------------- \n" + "OpenGL:\n" + " Vendor: %s\n" + " Renderer: %s\n" + " Version: %s\n" + , IUP_RELEASE_VERSION, IM_VERSION, + glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION)); + return IUP_DEFAULT; +} + +void mainMenuCreate(void) +{ + Ihandle* file_menu = IupMenu( + IupItem( "Open...", "app_open_cb"), + IupSeparator(), + IupItem( "Exit", "app_exit_cb"), + NULL + ); + + Ihandle* menu = IupMenu( + IupSubmenu("File", file_menu), + NULL + ); + + /* this will be used by the dialog */ + IupSetHandle("app_menu", menu); + + IupSetFunction("app_open_cb", (Icallback)app_open_cb); + IupSetFunction("app_exit_cb", (Icallback)app_exit_cb); +} + +void mainDialogCreate(void) +{ + Ihandle *dialog, *box, *canvas; + + /* initialize interface */ + + /* canvas for the image */ + + canvas = IupGLCanvas("app_repaint_cb"); + IupSetAttribute(canvas, "BORDER", "NO"); + IupSetAttribute(canvas, "BUFFER", "DOUBLE"); /* use double buffer */ + IupSetAttribute(canvas, "RESIZE_CB", "app_resize_cb"); /* configure the resize callback */ + + IupSetFunction("app_resize_cb", (Icallback)app_resize_cb); + IupSetFunction("app_repaint_cb", (Icallback)app_repaint_cb); + + /* this is the most external box that puts together + the toolbar, the two canvas and the status bar */ + box = IupSetAttributes(IupHbox( + canvas, + NULL), "MARGIN=10x10"); + + /* create the dialog and set its attributes */ + + mainMenuCreate(); + + dialog = IupDialog(box); + IupSetAttribute(dialog, "MENU", "app_menu"); /* configure the menu */ + IupSetAttribute(dialog, "CLOSE_CB", "app_exit_cb"); + IupSetAttribute(dialog, "TITLE", "IUPGLView"); + IupSetAttribute(dialog, "RASTERSIZE", "680x380"); /* initial size */ + IupSetAttribute(dialog, "SHRINK", "YES"); + IupSetHandle("app_dialog", dialog); + + IupShowXY(dialog, IUP_CENTER, IUP_CENTER); +} + +int main(int argc, char* argv[]) +{ + /* IUP initialization */ + IupOpen(); + IupGLCanvasOpen(); + + /* Create and show the main dialog */ + mainDialogCreate(); + + /* IUP event loop */ + IupMainLoop(); + + /* IUP closing */ + IupClose(); + + return 0; +} diff --git a/test/lua/analyze.lua b/test/lua/analyze.lua new file mode 100644 index 0000000..7f06496 --- /dev/null +++ b/test/lua/analyze.lua @@ -0,0 +1,28 @@ +require"imlua" +require"imlua_process" + +local filename = "lena.jpg" + +local image = im.FileImageLoad(filename) +local gray = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType()) +local binary = im.ImageCreate(image:Width(), image:Height(), im.BINARY, image:DataType()) +local region = im.ImageCreate(image:Width(), image:Height(), im.GRAY, im.USHORT) + +-- make it grayscale +im.ConvertColorSpace(image, gray) +gray:Save("lena_gray.jpg", "JPEG") + +-- make it binary +im.ProcessSliceThreshold(gray, binary, 0, 128) +binary:Save("lena_binary.jpg", "JPEG") + +local count = im.AnalyzeFindRegions(binary, region, 4, 1) +print("regions: ", count) + +local region2 = im.ImageCreate(image:Width(), image:Height(), im.GRAY, im.BYTE) +im.ConvertDataType(region, region2, 0, 0, 0, 0) + +local region3 = im.ImageCreate(image:Width(), image:Height(), im.MAP, im.BYTE) +im.ConvertColorSpace(region2, region3) +region3:SetPalette(im.PaletteHighContrast(), 256) +region3:Save("lena_region.gif", "GIF") diff --git a/test/lua/capture.lua b/test/lua/capture.lua new file mode 100644 index 0000000..491518a --- /dev/null +++ b/test/lua/capture.lua @@ -0,0 +1,63 @@ +require"imlua" +require"imlua_capture" + +im.VideoCaptureReloadDevices() + +print("--- Devices ---") +local n = im.VideoCaptureDeviceCount() + +for i = 0, n - 1 do + desc = im.VideoCaptureDeviceDesc(i) + print(desc) +end + +local vc = im.VideoCaptureCreate() +print("connect: ", vc:Connect(0)) +print() + +print("--- Dialogs ---") + +local dc = vc:DialogCount() +for i = 0, dc - 1 do + desc = vc:DialogDesc(i) + print(i, desc) + vc:ShowDialog(i) +end +print() + + +print("--- Formats ---") + +local fc = vc:FormatCount() +for i = 0, fc - 1 do + local success, width, height, desc = vc:GetFormat(i) + print(i, string.format("%dx%d", width, height), desc) +end +print() + +print("--- Image Size ---") +local width, height = vc:GetImageSize() +print(width, height) +print() + +print("--- Attributes ---") +attribs = vc:GetAttributeList() +for i, name in ipairs(attribs) do + local error, percent = vc:GetAttribute(name) + if error == 0 then percent = "get error" end + print(i, name, percent) +end +--vc:SetAttribute("FlipVertical", 1) +--vc:SetAttribute("FlipHorizontal", 1) +print() + +print("--- Capture ---") +local image = im.ImageCreate(width, height, im.RGB, im.BYTE) +local res = vc:Live(1) +if (res > 0) then + print("grabbing frame") + print(vc:Frame(image, 3000)) +end +image:Save("capture.jpg", "JPEG") + +vc:Disconnect() diff --git a/test/lua/error.lua b/test/lua/error.lua new file mode 100644 index 0000000..f8c71a8 --- /dev/null +++ b/test/lua/error.lua @@ -0,0 +1,10 @@ +require"imlua" + +local filename = "lena.jpg" +local image = im.FileImageLoad(filename) +local image2 = im.ImageCreate(image:Width(), image:Height(), im.GRAY, im.USHORT) + +-- Both calls will signal an error because of incompatible parameters + +--im.ConvertDataType(image, image2, im.CPX_REAL, im.GAMMA_LINEAR, 0, im.CAST_MINMAX) +im.ConvertColorSpace(image, image2, im.CPX_REAL, im.GAMMA_LINEAR, 0, im.CAST_MINMAX) diff --git a/test/lua/fft.lua b/test/lua/fft.lua new file mode 100644 index 0000000..3589635 --- /dev/null +++ b/test/lua/fft.lua @@ -0,0 +1,17 @@ +require"imlua" +require"imlua_process" +require"imlua_fftw" + +local filename = "lena.jpg" +local image = im.FileImageLoad(filename) + +local complex = im.ImageCreate(image:Width(), image:Height(), image:ColorSpace(), im.CFLOAT) +im.ProcessFFT(image, complex) + +local c = complex[0][5][10] -- component=0(Red), y = 5 x =10 +print(c[1], c[2]) + +complex[0][5][10] = { 2*c[1], c[2]/2 } + +local c = complex[0][5][10] +print(c[1], c[2]) diff --git a/test/lua/flower.jpg b/test/lua/flower.jpg Binary files differnew file mode 100644 index 0000000..2b9dbf1 --- /dev/null +++ b/test/lua/flower.jpg diff --git a/test/lua/index.lua b/test/lua/index.lua new file mode 100644 index 0000000..1c4c1e4 --- /dev/null +++ b/test/lua/index.lua @@ -0,0 +1,18 @@ +require"imlua" + +local filename = "lena.jpg" +local image = im.FileImageLoad(filename) + +local r = image[0] +local g = image[1] +local b = image[2] + +for row = 0, image:Height() - 1, 10 do + for column = 0, image:Width() - 1, 10 do + r[row][column] = 0 + g[row][column] = 0 + b[row][column] = 0 + end +end + +image:Save("lena_indexing.bmp", "BMP") diff --git a/test/lua/info.lua b/test/lua/info.lua new file mode 100644 index 0000000..5b4b151 --- /dev/null +++ b/test/lua/info.lua @@ -0,0 +1,149 @@ +require"imlua" +require"lfs" + +function PrintError(error) + local msg = {} + msg[im.ERR_OPEN] = "Error Opening File." + msg[im.ERR_MEM] = "Insuficient memory." + msg[im.ERR_ACCESS] = "Error Accessing File." + msg[im.ERR_DATA] = "Image type not Suported." + msg[im.ERR_FORMAT] = "Invalid Format." + msg[im.ERR_COMPRESS] = "Invalid or unsupported compression." + + if msg[error] then + print(msg[error]) + else + print("Unknown Error.") + end +end + +function FindZero(data) + if (not data) then return false end + for i = 1, table.getn(data) do + if data[i] == 0 then + return true + end + end + return false +end + +function AttribData2Str(data, data_type) + local data_str + + if data_type == im.BYTE then + data_str = string.format("%3d", data[1]) + elseif data_type == im.USHORT then + data_str = string.format("%5d", data[1]) + elseif data_type == im.INT then + data_str = string.format("%5d", data[1]) + elseif data_type == im.FLOAT then + data_str = string.format("%5.2f", data[1]) + elseif data_type == im.CFLOAT then + data_str = string.format("%5.2f, %5.2f", data[1], data[2]) + end + + return data_str +end + +function GetSizeDesc(size) + local size_desc + + if size < 1024 then + size_desc = "b" + else + size = size / 1024 + + if size < 1024 then + size_desc = "Kb" + else + size = size / 1024 + size_desc = "Mb" + end + end + + return size, size_desc +end + +function FileSize(file_name) + if lfs then + local attr = lfs.attributes(file_name) + return attr.size + else + return 0 + end +end + +function PrintImageInfo(file_name) + print("IM Info") + print(string.format(" File Name:\n %s", file_name)) + + local ifile, error = im.FileOpen(file_name) + if not ifile then + PrintError(error) + return nil + end + + local file_size = FileSize(file_name) + + print(string.format(" File Size: %.2f %s", GetSizeDesc(file_size))) + + local format, compression, image_count = ifile:GetInfo() + + local error, format_desc = im.FormatInfo(format) + print(string.format(" Format: %s - %s", format, format_desc)) + print(string.format(" Compression: %s", compression)) + print(string.format(" Image Count: %d", image_count)) + for i = 1, image_count do + local error, width, height, color_mode, data_type = ifile:ReadImageInfo(i-1) + if width == nil then + PrintError(height) + ifile:Close() + return nil + end + + print(string.format(" Image #%d", i)) + print(string.format(" Width: %d", width)) + print(string.format(" Height: %d", height)) + print(string.format(" Color Space: %s", im.ColorModeSpaceName(color_mode))) + print(string.format(" Has Alpha: %s", im.ColorModeHasAlpha(color_mode) and "Yes" or "No")) + print(string.format(" Is Packed: %s", im.ColorModeIsPacked(color_mode) and "Yes" or "No")) + print(string.format(" Is Top Down: %s", im.ColorModeIsTopDown(color_mode) and "Yes" or "No")) + print(string.format(" Data Type: %s", im.DataTypeName(data_type))) + + local image_size = im.ImageDataSize(width, height, color_mode, data_type) + print(string.format(" Data Size: %.2f %s", GetSizeDesc(image_size))) + + local attrib_list = ifile:GetAttributeList() + for a = 1, table.getn(attrib_list) do + if a == 1 then + print(" Attributes:") + end + + local attrib_data, attrib_data_type = ifile:GetAttribute(attrib_list[a]) + + if table.getn(attrib_data) == 1 then + print(string.format(" %s: %s", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type))) + elseif attrib_data_type == im.BYTE and FindZero(attrib_data) then + attrib_data = ifile:GetAttribute(attrib_list[a], true) + print(string.format(" %s: %s", attrib_list[a], attrib_data)) + else + print(string.format(" %s: %s ...", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type))) + end + end + end + + ifile:Close() +end + +function main(arg) + if (not arg or table.getn(arg) < 1) then + print("Invalid number of arguments.") + return nil + end + + PrintImageInfo(arg[1]) + return 1 +end + +--main(arg) +PrintImageInfo("lena.jpg") diff --git a/test/lua/lena.jpg b/test/lua/lena.jpg Binary files differnew file mode 100644 index 0000000..b6bad61 --- /dev/null +++ b/test/lua/lena.jpg diff --git a/test/lua/palette.lua b/test/lua/palette.lua new file mode 100644 index 0000000..5ec7168 --- /dev/null +++ b/test/lua/palette.lua @@ -0,0 +1,6 @@ +require"imlua" + +local impal = im.PaletteHotIron() +print(impal) +print(im.ColorDecode(impal[1])) + diff --git a/test/lua/process.lua b/test/lua/process.lua new file mode 100644 index 0000000..5a32586 --- /dev/null +++ b/test/lua/process.lua @@ -0,0 +1,50 @@ +require"imlua" +require"imlua_process" + +function save_histogram (hist, filename, format) + local height = 200 -- altura da imagem + local max = math.max(unpack(hist)) -- pega o maior valor do histograma + local n = table.getn(hist) + 1 -- zero-based + local image = im.ImageCreate(n, height, im.GRAY, im.BYTE) -- cria a imagem + local white = 255 + local black = 0 + + local render = function (x, y, d, param) + local v = hist[x] / max + local h = v * height + if y <= h then return black end + return white + end + + im.ProcessRenderOp(image, render, "histogram", {}, 0) + image:Save(filename, format) +end + +local filename = "lena.jpg" + +local image = im.FileImageLoad(filename) + +save_histogram(im.CalcHistogram(image, 0, 0), "lena_histogram_R.gif", "GIF") +save_histogram(im.CalcHistogram(image, 1, 0), "lena_histogram_G.gif", "GIF") +save_histogram(im.CalcHistogram(image, 2, 0), "lena_histogram_B.gif", "GIF") +save_histogram(im.CalcGrayHistogram(image, 0), "lena_histogram_gray.gif", "GIF") + +local r = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType()) +local g = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType()) +local b = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType()) +im.ProcessSplitComponents(image, { r, g, b}) +r:Save("lena_r.jpg", "JPEG") +g:Save("lena_g.jpg", "JPEG") +b:Save("lena_b.jpg", "JPEG") + +local rgb = image:Clone() +im.ProcessMergeComponents({r, g, b}, rgb) +rgb:Save("lena_rgb.jpg", "JPEG") + +local replace = image:Duplicate() +im.ProcessReplaceColor(image, replace, { 146, 93, 145 }, { 255, 0, 255 }) +replace:Save("lena_replace.jpg", "JPEG") + +local bitmask = image:Duplicate() +im.ProcessBitMask(image, bitmask, "01111010", im.BIT_XOR) +replace:Save("lena_bitmask.jpg", "JPEG") diff --git a/test/lua/process_new.lua b/test/lua/process_new.lua new file mode 100644 index 0000000..93ebcce --- /dev/null +++ b/test/lua/process_new.lua @@ -0,0 +1,44 @@ +require"imlua" +require"imlua_process" + +function save_histogram (hist, filename, format) + local height = 200 -- altura da imagem + local max = math.max(unpack(hist)) -- pega o maior valor do histograma + local n = table.getn(hist) + 1 -- zero-based + local image = im.ImageCreate(n, height, im.GRAY, im.BYTE) -- cria a imagem + local white = 255 + local black = 0 + + local render = function (x, y, d, param) + local v = hist[x] / max + local h = v * height + if y <= h then return black end + return white + end + + im.ProcessRenderOp(image, render, "histogram", {}, 0) + image:Save(filename, format) +end + +local filename = "lena.jpg" + +local image = im.FileImageLoad(filename) + +save_histogram(im.CalcHistogram(image, 0, 0), "lena_histogram_R.gif", "GIF") +save_histogram(im.CalcHistogram(image, 1, 0), "lena_histogram_G.gif", "GIF") +save_histogram(im.CalcHistogram(image, 2, 0), "lena_histogram_B.gif", "GIF") +save_histogram(im.CalcGrayHistogram(image, 0), "lena_histogram_gray.gif", "GIF") + +local r, g, b = im.ProcessSplitComponentsNew(image) +r:Save("lena_r.jpg", "JPEG") +g:Save("lena_g.jpg", "JPEG") +b:Save("lena_b.jpg", "JPEG") + +local rgb = im.ProcessMergeComponentsNew({r, g, b}) +rgb:Save("lena_rgb.jpg", "JPEG") + +local replace = im.ProcessReplaceColorNew(image, { 146, 93, 145 }, { 255, 0, 255 }) +replace:Save("lena_replace.jpg", "JPEG") + +local bitmask = im.ProcessBitMaskNew(image, "01111010", im.BIT_XOR) +replace:Save("lena_bitmask.jpg", "JPEG") diff --git a/test/lua/render.lua b/test/lua/render.lua new file mode 100644 index 0000000..b57f906 --- /dev/null +++ b/test/lua/render.lua @@ -0,0 +1,50 @@ +require"imlua" +require"imlua_process" + +local image = im.ImageCreate(500, 500, im.RGB, im.BYTE) + +im.ProcessRenderRandomNoise(image) +image:Save("render_noise.bmp", "BMP") + +im.ProcessRenderConstant(image, { 128.0, 0.0, 255.0 }) +image:Save("render_constant.bmp", "BMP") + +im.ProcessRenderWheel(image, 100, 200) +image:Save("render_wheel.bmp", "BMP") + +im.ProcessRenderTent(image, 300, 200) +image:Save("render_tent.bmp", "BMP") + +im.ProcessRenderRamp(image, 0, 500, 0) +image:Save("render_ramp.bmp", "BMP") + +im.ProcessRenderBox(image, 200, 200) +image:Save("render_box.bmp", "BMP") + +im.ProcessRenderSinc(image, 100.0, 100.0) +image:Save("render_sinc.bmp", "BMP") + +im.ProcessRenderGaussian(image, 100.0) +image:Save("render_gaussian.bmp", "BMP") + +im.ProcessRenderLapOfGaussian(image, 100.0) +image:Save("render_lapofgaussian.bmp", "BMP") + +im.ProcessRenderCosine(image, 100.0, 100.0) +image:Save("render_cosine.bmp", "BMP") + +im.ProcessRenderGrid(image, 100.0, 100.0) +image:Save("render_grid.bmp", "BMP") + +im.ProcessRenderChessboard(image, 100.0, 100.0) +image:Save("render_chess.bmp", "BMP") + +im.ProcessRenderCone(image, 200) +image:Save("render_cone.bmp", "BMP") + +local render_func = function (x, y, d, param) + return math.mod(x + y, 256) +end + +im.ProcessRenderOp(image, render_func, "test", {}, 0) +image:Save("render_func.bmp", "BMP") diff --git a/test/lua/render_cd.lua b/test/lua/render_cd.lua new file mode 100644 index 0000000..532f906 --- /dev/null +++ b/test/lua/render_cd.lua @@ -0,0 +1,15 @@ +require"imlua" +require"cdlua" +require"imlua_cd" + +local image = im.ImageCreate(500, 500, im.RGB, im.BYTE) +local canvas = image:cdCreateCanvas() -- Creates a CD_IMAGERGB canvas + +canvas:Activate() +canvas:Clear() +canvas:Font("Times", cd.BOLD, 24) +canvas:Text(100, 100, "Test") +canvas:Line(0,0,100,100) +canvas:Kill() + +image:Save("new.bmp", "BMP") diff --git a/test/lua/screencapture.lua b/test/lua/screencapture.lua new file mode 100644 index 0000000..5b1a93d --- /dev/null +++ b/test/lua/screencapture.lua @@ -0,0 +1,13 @@ +require"imlua" +require"cdlua" +require"imlua_cd" + +local canvas = cd.CreateCanvas(cd.NATIVEWINDOW, nil) +canvas:Activate() +local w, h = canvas:GetSize() +local image = im.ImageCreate(w, h, im.RGB, im.BYTE) +image:cdCanvasGetImage(canvas, 0, 0) +error = image:Save("screencapture.jpg", "JPEG") +image:Destroy() +if (error) then print("error = "..error) end + diff --git a/test/lua/show_flower.wlua b/test/lua/show_flower.wlua new file mode 100644 index 0000000..a3803d7 --- /dev/null +++ b/test/lua/show_flower.wlua @@ -0,0 +1,31 @@ +require"imlua" +require"iuplua" +require"cdlua" +require"cdluaiup" +require"imlua_cd" + +image = im.FileImageLoad("flower.jpg") -- directly load the image at index 0. it will open and close the file +cnv = iup.canvas{rastersize = image:Width().."x"..image:Height(), border = "NO"} +cnv.image = image -- store the new image in the IUP canvas as an attribute + +function cnv:map_cb() -- the CD canvas can only be created when the IUP canvas is mapped + self.canvas = cd.CreateCanvas(cd.IUP, self) +end + +function cnv:action() -- called everytime the IUP canvas needs to be repainted + self.canvas:Activate() + self.canvas:Clear() + self.image:cdCanvasPutImageRect(self.canvas, 0, 0, 0, 0, 0, 0, 0, 0) -- use default values +end + +dlg = iup.dialog{cnv} + +function dlg:close_cb() + cnv.image:Destroy() + cnv.canvas:Kill() + self:destroy() + return iup.IGNORE -- because we destroy the dialog +end + +dlg:show() +iup.MainLoop() diff --git a/test/lua/view.wlua b/test/lua/view.wlua new file mode 100644 index 0000000..9eb9eed --- /dev/null +++ b/test/lua/view.wlua @@ -0,0 +1,158 @@ +require"imlua" +require"iuplua" +require"cdlua" +require"cdluaiup" +require"imlua_cd" + +function PrintError(func, error) + local msg = {} + msg[im.ERR_OPEN] = "Error Opening File." + msg[im.ERR_MEM] = "Insuficient memory." + msg[im.ERR_ACCESS] = "Error Accessing File." + msg[im.ERR_DATA] = "Image type not Suported." + msg[im.ERR_FORMAT] = "Invalid Format." + msg[im.ERR_COMPRESS] = "Invalid or unsupported compression." + + if msg[error] then + print(func..": "..msg[error]) + else + print("Unknown Error.") + end +end + +function LoadImage(file_name) + local image + local ifile, error = im.FileOpen(file_name) + if not ifile then + PrintError("open", error) + return + end + + -- load the first image in the file. + -- force the image to be converted to a bitmap + image, error = ifile:LoadBitmap() + if not image then + PrintError("load", error) + return + end + + ifile:Close() + return image +end + + +dlg = nil -- only one dlg + +function ShowImage(file_name) + + local image = LoadImage(file_name) + if not image then + return false + end + + if dlg then + local old_canvas = dlg.canvas + local old_image = dlg.image + + if old_canvas ~= nil then old_canvas:Kill() end + if old_image ~= nil then old_image:Destroy() end + + iup.Destroy(dlg) + end + + cnv = iup.canvas{} + + function cnv:action() + local canvas = dlg.canvas + local image = dlg.image + + if (not canvas) then return end + + canvas:Activate() + canvas:Clear() + image:cdCanvasPutImageRect(canvas, 0, 0, image:Width(), image:Height(), 0, 0, 0, 0) + canvas:Flush() + + return iup.DEFAULT + end + + function cnv:button_cb() + local file_name = "*.*" + local error + + file_name, error = iup.GetFile(file_name) + if error ~= 0 then + return iup.DEFAULT + end + + ShowImage(file_name) + return iup.DEFAULT + end + + + -- Set the Canvas inicial size (IUP will retain this value). + cnv.rastersize = string.format("%dx%d", image:Width(), image:Height()) + + dlg = iup.dialog{cnv} + dlg.title = file_name + + function dlg:close_cb() + local canvas = self.canvas + local image = self.image + + if canvas then canvas:Kill() end + if image then image:Destroy() end + + return iup.CLOSE + end + + dlg:show() + + canvas = cd.CreateCanvas(cd.IUP, cnv) + assert(canvas) + + dlg.canvas = canvas + dlg.image = image + + cnv:action() + + return true +end + +function main(arg) + local file_name = "*.*" + local error + + -- Try to get a file name from the command line. + if (arg == nil or table.getn(arg) < 2) then + file_name, error = iup.GetFile(file_name) + if error ~= 0 then + return true + end + else + file_name = arg[1] + end + + if not ShowImage(file_name) then + local Try = true + -- If ShowImage returns an error I will try to read another image. + -- I can give up on File Open dlg choosing "Cancel". + while Try do + file_name = "*.*" + + file_name, error = iup.GetFile(file_name) + if error ~= 0 then + return true + end + + if ShowImage(file_name) then + Try = false + end + end + end + + iup.MainLoop() + return true +end + +main(arg) |