Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gianpierodiblasi committed Jun 10, 2022
0 parents commit 8e66ffb
Show file tree
Hide file tree
Showing 21 changed files with 377 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nbproject/private/
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ImageProcessingResource
An extension to manage images.

**This Extension is provided as-is and without warranty or support. It is not part of the PTC product suite and there is no PTC support.**

## Description
This extension adds a Resource object providing utility services for image processing.

## Services
- *resize*: resizes an image (if both width and height are not defined then no resizing is performed)
- input
- image: the image - IMAGE (No default value)
- width: the new width, none (or a number less or equal to zero) to compute width relative to height - INTEGER (No default value)
- height: the new height, none (or a number less or equal to zero) to compute height relative to width - INTEGER (No default value)
- output: IMAGE
- *scale*: scales an image (if both scaleX and scaleY are not defined then no scaling is performed)
- input
- image: the image - IMAGE (No default value)
- scaleX: the X scale, none (or a number less or equal to zero) to make scaleX = scaleY - NUMBER (No default value)
- scaleY: the Y scale, none (or a number less or equal to zero) to make scaleY = scaleX (No default value)
- output: IMAGE

## Donate
If you would like to support the development of this and/or other extensions, consider making a [donation](https://www.paypal.com/donate/?business=HCDX9BAEYDF4C&no_recurring=0&currency_code=EUR).
116 changes: 116 additions & 0 deletions build-extension.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="ImageProcessingResource" basedir="." default="build">

<property name="extension.jar" value="imageprocessingresource.jar" />

<property name="target" value="1.8" />
<property name="source" value="1.8" />
<property name="debuglevel" value="source,lines,vars" />
<property name="common" value="common" />
<property name="ui" value="ui" />
<property name="lib" value="lib" />
<property name="entities" value="Entities" />
<property name="localization" value="Localization" />
<property name="src.dir" value="${basedir}/src" />
<property name="build.dir" value="${user.home}/builds/${ant.project.name}/bin" />
<property name="config.dir" value="${basedir}/configfiles" />
<property name="ui.dir" value="${basedir}/${ui}" />
<property name="lib.dir" value="${basedir}/${lib}" />
<property name="zip.dir" value="${user.home}/builds/${ant.project.name}/build/distributions" />
<property name="entity.dir" value="${basedir}/Entities" />
<property name="localization.dir" value="${basedir}/${localization}" />

<property file="extension.properties" />

<!-- ExtensionPackage directory structure props -->
<property name="package.lib.basedir" value="${lib}" />
<property name="package.ui.basedir" value="${ui}" />
<property name="package.common.lib.dir" value="${package.lib.basedir}/${common}" />
<property name="package.common.ui.dir" value="${package.ui.basedir}/${common}" />

<!-- Extension file info -->
<property name="zip.file.name" value="${ant.project.name}.zip" />

<tstamp>
<format property="NOW" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<!-- define the classpath so it picks up the ThingWorx SDK jar relative to this basedir -->
<path id="jar.classpath">
<pathelement location="${build.dir}" />
<fileset dir="${basedir}/twx-lib" includes="*.jar" />
<fileset dir="${lib.dir}" includes="*.jar" erroronmissingdir="false" />
</path>

<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${zip.dir}" />
</target>

<target name="init" depends="clean">
<mkdir dir="${build.dir}" />

<copy includeemptydirs="false" todir="${build.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.launch" />
<exclude name="**/*.java" />
</fileset>
</copy>
</target>

<target name="build-source" depends="init">
<echo message="${ant.project.name}: ${ant.file}" />
<javac debug="true" debuglevel="${debuglevel}" destdir="${build.dir}" source="${source}" target="${target}" includeantruntime="false">
<src path="${src.dir}" />
<classpath refid="jar.classpath" />
</javac>
</target>

<target name="check-bin" depends="build-source">
<fileset dir="${build.dir}" id="binfileset" />
<condition property="bindir.empty">
<length length="0">
<fileset refid="binfileset" />
</length>
</condition>
</target>

<target name="build-jars" depends="check-bin" unless="bindir.empty">
<echo message="building ${extension.jar} to ${build.dir}..." />
<jar destfile="${build.dir}/${extension.jar}">
<!-- generate MANIFEST inline -->
<manifest>
<attribute name="Built-By" value="Eclipse Plugin for ThingWorx Extension Development ${twx_eclipse_plugin_version}" />
<attribute name="Build-Date" value="${NOW}" />
<section name="${ant.project.name}">
<attribute name="Package-Title" value="${ant.project.name}" />
<attribute name="Package-Version" value="${package_version}" />
<attribute name="Package-Vendor" value="${project_vendor}" />
</section>
</manifest>

<fileset dir="${build.dir}" />
</jar>
</target>

<target name="package-extension" depends="build-jars">
<zip destfile="${zip.dir}/${zip.file.name}">
<mappedresources>
<fileset dir="${build.dir}" includes="${extension.jar}" />
<globmapper from="*" to="${package.common.lib.dir}/*" />
</mappedresources>

<zipfileset dir="${config.dir}" includes="metadata.xml" />

<zipfileset dir="${basedir}" includes="${entities}/**/*.xml" />
<zipfileset dir="${basedir}" includes="${localization}/**/*.json" />
<zipfileset dir="${basedir}" includes="${ui}/**/*.*" />
<zipfileset dir="${lib.dir}" includes="**/*.jar" prefix="${package.common.lib.dir}/"/>
</zip>
</target>

<target name="build" depends="package-extension">
<echo message="Building ${ant.project.name} extension package..."/>
</target>

</project>
22 changes: 22 additions & 0 deletions configfiles/metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Entities>
<ExtensionPackages>
<ExtensionPackage
name="ImageProcessingResource"
packageVersion="0.11.0"
vendor="Gianpiero Di Blasi"
description="Extension to manage images"
minimumThingWorxVersion="8.3.0"
haCompatible="true"
dependsOn="">

<JarResources>
<FileResource description="" file="imageprocessingresource.jar" type="JAR"/>
</JarResources>
</ExtensionPackage>
</ExtensionPackages>

<Resources>
<Resource aspect.isCreatable="false" aspect.isEditableExtensionObject="false" className="com.thingworx.extension.custom.imageprocessing.ImageProcessingResource" description="" name="ImageProcessingResource"/>
</Resources>
</Entities>
Empty file added lib/NOFILE.txt
Empty file.
101 changes: 101 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.ant.freeform</type>
<configuration>
<general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
<name>ImageProcessingResource</name>
</general-data>
<general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
<!-- Do not use Project Properties customizer when editing this file manually.
To prevent the customizer from showing, create nbproject/project.properties file and enter
auxiliary.show.customizer=false
property there. Adding
auxiliary.show.customizer.message=<message>
will show your customized message when someone attempts to open the customizer. -->
<name>ImageProcessingResource</name>
<properties>
<property name="ant.script">build-extension.xml</property>
</properties>
<folders>
<source-folder>
<label>ImageProcessingResource</label>
<location>.</location>
<encoding>windows-1252</encoding>
</source-folder>
<source-folder>
<label>Entities</label>
<type>java</type>
<location>Entities</location>
<encoding>windows-1252</encoding>
</source-folder>
<source-folder>
<label>src</label>
<type>java</type>
<location>src</location>
<encoding>windows-1252</encoding>
</source-folder>
<source-folder>
<label>configfiles</label>
<type>java</type>
<location>configfiles</location>
<encoding>windows-1252</encoding>
</source-folder>
</folders>
<ide-actions>
<action name="build">
<script>${ant.script}</script>
<target>build</target>
</action>
<action name="clean">
<script>${ant.script}</script>
<target>clean</target>
</action>
<action name="rebuild">
<script>${ant.script}</script>
<target>clean</target>
<target>build</target>
</action>
</ide-actions>
<view>
<items>
<source-folder style="packages">
<label>Entities</label>
<location>Entities</location>
</source-folder>
<source-folder style="packages">
<label>src</label>
<location>src</location>
</source-folder>
<source-folder style="packages">
<label>configfiles</label>
<location>configfiles</location>
</source-folder>
<source-file>
<location>${ant.script}</location>
</source-file>
</items>
<context-menu>
<ide-action name="build"/>
<ide-action name="rebuild"/>
<ide-action name="clean"/>
</context-menu>
</view>
<subprojects/>
</general-data>
<java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/4">
<compilation-unit>
<package-root>src</package-root>
<classpath mode="compile">twx-lib/custom-rhino-js-5.1.0.jar;twx-lib/jackson-annotations-2.9.5.jar;twx-lib/jackson-core-2.9.5.jar;twx-lib/jackson-databind-2.9.5.jar;twx-lib/jackson-dataformat-smile-2.9.5.jar;twx-lib/joda-time-2.9.4.jar;twx-lib/logback-classic-1.2.3.jar;twx-lib/logback-core-1.2.3.jar;twx-lib/slf4j-api-1.7.25.jar;twx-lib/thingworx-communications-client-8.3.0-b349.jar;twx-lib/thingworx-communications-server-8.3.0-b349.jar;twx-lib/thingworx-ext-sdk-8.3.0-b349.jar;twx-lib/thingworx-javadocs-8.3.0-b349.jar</classpath>
<source-level>1.8</source-level>
</compilation-unit>
<compilation-unit>
<package-root>configfiles</package-root>
<source-level>1.8</source-level>
</compilation-unit>
<compilation-unit>
<package-root>Entities</package-root>
<source-level>1.8</source-level>
</compilation-unit>
</java-data>
</configuration>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.thingworx.extension.custom.imageprocessing;

import com.thingworx.logging.LogUtilities;
import com.thingworx.metadata.annotations.ThingworxServiceDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceParameter;
import com.thingworx.metadata.annotations.ThingworxServiceResult;
import com.thingworx.resources.Resource;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import org.slf4j.Logger;

public class ImageProcessingResource extends Resource {

private final static Logger SCRIPT_LOGGER = LogUtilities.getInstance().getScriptLogger(ImageProcessingResource.class);
private static final long serialVersionUID = 1L;

@ThingworxServiceDefinition(name = "resize", description = "", category = "", isAllowOverride = false, aspects = {"isAsync:false"})
@ThingworxServiceResult(name = "result", description = "", baseType = "IMAGE", aspects = {})
public byte[] resize(
@ThingworxServiceParameter(name = "image", description = "", baseType = "IMAGE") byte[] image,
@ThingworxServiceParameter(name = "width", description = "", baseType = "INTEGER") Integer width,
@ThingworxServiceParameter(name = "height", description = "", baseType = "INTEGER") Integer height) throws Exception {
SCRIPT_LOGGER.debug("ImageProcessingResource - resize -> Start");

ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(image));
String format = ImageIO.getImageReaders(iis).next().getFormatName();
BufferedImage bufferedImage = ImageIO.read(iis);

if (width != null && width > 0 && height != null && height > 0) {
} else if (width != null && width > 0) {
height = bufferedImage.getHeight() * width / bufferedImage.getWidth();
} else if (height != null && height > 0) {
width = bufferedImage.getWidth() * height / bufferedImage.getHeight();
} else {
return image;
}
SCRIPT_LOGGER.debug("ImageProcessingResource - resize -> width = {}, height = {}", width, height);

AffineTransform at = AffineTransform.getScaleInstance(width / (double) bufferedImage.getWidth(), height / (double) bufferedImage.getHeight());
BufferedImage scaledImage = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC).filter(bufferedImage, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(scaledImage, format, baos);

SCRIPT_LOGGER.debug("ImageProcessingResource - resize -> Stop");
return baos.toByteArray();
}

@ThingworxServiceDefinition(name = "scale", description = "", category = "", isAllowOverride = false, aspects = {"isAsync:false"})
@ThingworxServiceResult(name = "result", description = "", baseType = "IMAGE", aspects = {})
@SuppressWarnings("SuspiciousNameCombination")
public byte[] scale(
@ThingworxServiceParameter(name = "image", description = "", baseType = "IMAGE") byte[] image,
@ThingworxServiceParameter(name = "scaleX", description = "", baseType = "NUMBER") Double scaleX,
@ThingworxServiceParameter(name = "scaleY", description = "", baseType = "NUMBER") Double scaleY) throws Exception {
SCRIPT_LOGGER.debug("ImageProcessingResource - scale -> Start");

ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(image));
String format = ImageIO.getImageReaders(iis).next().getFormatName();
BufferedImage bufferedImage = ImageIO.read(iis);

if (scaleX != null && scaleX > 0 && scaleY != null && scaleY > 0) {
} else if (scaleX != null && scaleX > 0) {
scaleY = scaleX;
} else if (scaleY != null && scaleY > 0) {
scaleX = scaleY;
} else {
return image;
}

int width = (int) (bufferedImage.getWidth() * scaleX);
int height = (int) (bufferedImage.getHeight() * scaleY);
SCRIPT_LOGGER.debug("ImageProcessingResource - scale -> width = {}, height = {}", width, height);

AffineTransform at = AffineTransform.getScaleInstance(scaleX, scaleY);
BufferedImage scaledImage = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC).filter(bufferedImage, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(scaledImage, format, baos);

SCRIPT_LOGGER.debug("ImageProcessingResource - scale -> Stop");
return baos.toByteArray();
}
}
Binary file added twx-lib/custom-rhino-js-5.1.0.jar
Binary file not shown.
Binary file added twx-lib/jackson-annotations-2.9.5.jar
Binary file not shown.
Binary file added twx-lib/jackson-core-2.9.5.jar
Binary file not shown.
Binary file added twx-lib/jackson-databind-2.9.5.jar
Binary file not shown.
Binary file added twx-lib/jackson-dataformat-smile-2.9.5.jar
Binary file not shown.
Binary file added twx-lib/joda-time-2.9.4.jar
Binary file not shown.
Binary file added twx-lib/logback-classic-1.2.3.jar
Binary file not shown.
Binary file added twx-lib/logback-core-1.2.3.jar
Binary file not shown.
Binary file added twx-lib/slf4j-api-1.7.25.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added twx-lib/thingworx-ext-sdk-8.3.0-b349.jar
Binary file not shown.
Binary file added twx-lib/thingworx-javadocs-8.3.0-b349.jar
Binary file not shown.

0 comments on commit 8e66ffb

Please sign in to comment.