As mentioned in an earlier article, I have written a reader for JPEG images with CMYK color data in Java.
This reader can be used with Java ImageIO. I have made a demo including source code: CMYKDemo.jar. You can download this file and then double click it to run the demo. The demo is a window onto which you can drop a CMYK image. You can unzip the file to get the source code.
Here is how to use the CMYKJPEGImageReader with ImageIO:
- Register the reader with ImageIO.This can be done by putting the file CMYKDemo.jar into the class path.Alternatively – if you want to build the reader from source and integrate it into your own .jar file – you can create a file named
META-INF/services/javax.imageio.spi.ImageReaderSpi
and put it in the class path.The file should contain the following line:
ch.randelshofer.media.jpeg.CMYKJPEGImageReaderSpi
- Read an image with ImageIO.Unfortunately, the ImageIO.read method is not a reliable way for reading a JPEG CMYK image.
ImageIO.read(file);
This does not work, because ImageIO already provides a JPEG image reader. There is no way to specify in which order the readers shall be used. So, sometimes, ImageIO will use our CMYKJPEGImageReader and succeed, and sometimes it will try to use its own reader, and fail.
Therefore, we need to use the following code snippet, which tries out all JPEG readers until it succeeds.
Image img = null; ImageInputStream iis = new FileImageInputStream(file); try { for (Iterator<ImageReader> i = ImageIO.getImageReaders(iis); img == null && i.hasNext(); ) { ImageReader r = i.next(); try { r.setInput(iis); img = r.read(0); } catch (IOException e) {} } } finally { iis.close(); } return img;
As always, my code examples are available for free under the Creative Commons Attribution license. Enjoy.
Update 2014-01-22:
I have closed the comment section for this article, because the CMYKJPEGImageReader class is obsolete. Take a look at the Monte Media Library for newer versions of this class.
Rubik’s Cube in WebGL
WebGL is a new 3D graphics context for the <canvas> HTML element. It allows to create high-performance 3D rendering in a browser without the need of plug-ins. The WebGL graphics context is an OpenGL ES API for JavaScript.
I think, the WebGL API should be very familiar for everyone who has already programmed with OpenGL in C++ and GLSL. Creating a 3D rendering with WebGL typically involves the following steps:
- Add a <canvas> element to an HTML page.
- Access the element from JavaScript, and get the WebGL context.
- Load a vertex shader and a fragment shader into the context.
- Load geometry, colors and textures into the context.
- Specify a camera matrix and transformation matrices.
- Draw the geometry through the context.
I have ported one of my virtual Rubik’s Cube applets from Java to WebGL. To ease my work, I used some code from the WebGL demo repository. Below is a screenshot.
You can try it out here.
WebGL is currently supported in Chrome 12 and Firefox 5. Safari and Opera have preview versions which support it. As always, Internet Explorer lags behind.
Pure-Java Screen Recorder (obsolete)
I am working on a new AVIWriter class, which provides a higher level API than the AVIOutputStream class.
There is still a long way to go, so I decided to write a screen recorder as a demo for the upcoming class.
This recorder uses java.awt.Robot to capture the screen, and java.awt.MouseInfo for capturing the mouse cursor.
Screen changes are captured with a frame rate of up to 15 fps. Mouse cursor movements with up to 30 fps.
For encoding the videos with AVI, I have implemented the Techsmith Screen Capture Codec in pure Java. This codec is essentially the Microsoft RLE encoder with ZIP compression applied to each video frame.
To play back a recorded video, you may have to install the free codec from www.techsmith.com.
For QuickTime, the videos are encoded with the Apple Animation codec, which I already had implemented some time ago.
You can download the file ScreenRecorder.jar. Double click the file to start the screen recorder. The .jar file also includes the source code of the recorder. The code is preliminary, and may substantially change in future releases.
Update 2012-02-04:
A newer version of the screen recorder is available at http://www.randelshofer.ch/monte
Update 2012-01-13:
The code described in this article is obsolete. Stay tuned for a new recorder which also supports audio capture in AVI files.
Update 2011-07-05:
The rate of screen and mouse captures can now be adjusted individually.
Update 2011-05-13:
I have added support for audio capture. This feature is currently only available with QuickTime videos, since AVIWriter is not finished yet. Audio is recorded from line-in. Line-in is usually the microphone of the computer.
Update 2011-05-09:
I have changed the code so that it will record into the home folder “Videos” on Windows, and into “Movies” on Mac OS X and everywhere else. In the prior version, recording silently failed if this folder did not exist. Now, an error message is displayed.
Update 2014-01-22:
I have closed the comment section for this article, because the screen recorder class is obsolete. Take a look at the Monte Media Library for newer versions.
Locating features in the JHotDraw code
The paper “A unified approach to feature-centric analysis of object-oriented software” by Andrzej Olszak and Bo Nørregaard Jørgensen discusses an approach for analyzing the code of a software project based on its features. They first identify features that they want to understand, and then relate the code which implements them. They implemented a NetBeans plug-in called Featureous to support this task.
In the introduction chapter of the paper they state:
“In case of object-oriented programs, relating features to their implementations is, however, a difficult task, as object- oriented programming languages provide no means for expressing features explicitly. In object-oriented programs, features are implemented implicitly as inter-class collaborations crosscutting not only multiple classes but also multiple architectural units, e.g. layers in layered architectures. This physical tangling and scattering of features over several source code units makes their implementations difficult to identify and understand.”
They used the SVG sample application of JHotDraw 7.5 as an example. The SVG sample does not include a documentation of its features, and thus makes such an analysis unnecessarily hard. From now on, I am going to extend the package comments and class comments in JHotDraw with a “Features” section, similar to the “Design Patterns” section which is already present. The “Features” section will name a feature, state the part of the feature that the package or class implements, and list participating packages and classes.
Web-Site Statistics 2010
The beginning of a new year is a good time to look back at the previous one.
The web-site statistics for http://www.randelshofer.ch yields the following numbers for the year 2010:
- 344,881 unique visitors.
- 482,857 visits (1.4 visits per visitor).
- 5,359,517 page visits (11.09 pages per visit).
- 10,453,491 hits (21.64 hits per visit).
- 1,045.27 GB bandwidth (2,269.91 KB per visit).
Our visitors came from the following countries:
- 34.7 % Europe
- 32.8 % United States
- 32.5 % Rest of the World
The following operating systems were used:
- 78.5 % Windows
- 10.3 % Macintosh
- 3 % Linux
- 8.2 % Unknown
… and the following browsers:
- 32 % Internet Explorer (17.9 % IE 8, 9.2 % IE 7, 4.7 % IE 6, 0.2 % IE 5)
- 27.8 % Firefox
- 11.4 % Netscape 4.0
- 9.2 % Safari
- 7.6 % Chrome
- 12 % Others
The most popular topic was the Virtual Cubes, followed by the Pretty Patterns and the Quaqua Look and Feel.
This blog attracted 2,600 visitors and received 20,900 hits. 16 comments were made. Thank you!
Oh, and a whopping 1,514 spams were posted – but never made it on the pages – sorry. 😉
JHotDraw 7.6 released
I have released JHotDraw 7.6 yesterday.
This release only does minor tweaks in the drawing framework. My focus was on overall robustness of the code and on cleaning up the framework for creating menu bars.
To improve the robustness of JHotDraw, I have adopted FindBugs annotations. Most packages of JHotDraw define the @NonNull annotation as the default for all classes, fields and methods. Fields, method parameters and return values which can be set to null are annotated individually with @Nullable. I ran JHotDraw through the FindBugs static code analyzer and fixed all potentially ‘harmful’ issues, that the tool discovered and I deemed important.
In the application framework, menus are now generated in small logical groups by the new interface MenuBuilder. A JHotDraw Application can piece the groups together into JMenuBar(s) and JPopupMenu(s) to build the user interface of the application.
Updated JavaDoc and change documentation (generated with JDiff) is available on my JHotDraw 7 pages.
In my article ‘Writing QuickTime videos in pure Java’, I said I would integrate audio support if I would get the right incentives (wine or books). This has happened, and so I have implemented an all new class named QuickTimeWriter, which supports writing of audio and video data into a QuickTime Movie file in pure java.
The JavaDoc of the QuickTimeWriter class is available here.
The class is still in a very early stage. Right now, it can be used for combining a single audio track with a single video track.
Audio has to be encoded before it can be written. I tried to make this as simple as possible by supporting javax.sound.sampled.AudioFormat in one of the addAudioTrack() methods.
As with my previous implementation, QuickTimeWriter provides a minimal set of encoders for video data. It includes encoders for the following compression formats: PNG, Photo-JPEG, Apple Animation (RLE) and None (Raw). Other encodings are possible, but you have to encode the frames by yourself.
QuickTimeWriter stores samples in the same sequence in the movie file as they are written. If a movie is created for further processing, the samples can be written in any order. If the movie shall be playable, samples of multiple tracks should be interleaved about twice per second. I have described this in more detail in the class comments.
I have also created a small demo program which shows how to put it all together:
The demo along with all the source files can be downloaded here: QuickTimeDemo2.jar.
The code can be licensed under Creative Commons Attribution 3.0.
Update 2012-02-04: The demo code described in this article is obsolete. The latest version of the code is available on the following page: http://www.randelshofer.ch/monte
Update 2011-01-07: Supports now web-optimized movies with compressed header at the start of the file.
Update 2011-01-06: Supports now Apple Animation (RLE) compression.
Update 2011-01-04: Supports now MP3 sound files.
Joseph Huckaby has implemented animation with color cycling in HTML 5 with a new color cycling technique called ‘BlendShift Cycling’.
The animations shown on his site are absolutely stunning. The images are exquisitely drawn with great attention to detail. The animations have been quilted into many different layers of a scene. There are so many different things going on, you have to look at an image for a long time to fully appreciate it. The animations were created by artist Mark Ferrari.
Joseph has implemented color cycling using an HTML 5 canvas object. He programmatically creates an image data object from the canvas 2d context by calling context.createImageData, and then writes the pixels into it. Since canvas does not support images with indexed colors, it is not sufficient to just update the color lookup table to achieve a color cycled animation. Instead, each individual pixel has to be updated explicitly. This is quite a burden for JavaScript, causing the fan of my laptop to blow strongly after looking at an animation for a few minutes. Nevertheless, the animations achieve about 30 fps on my 2 GHz Intel Core 2 Duo.
Mark has created the images using Deluxe Paint II for PC compatibles. The color cycling is done using CRNG “color range cycling”, and is thus limited by the number of colors in the palette. (Using DRNG “DPaint enhanced color cycling” available in Deluxe Paint IV for Amiga, it would have been possible to use more colors). Therefore, since the number of colors is limited to 256, and since Mark uses so many different cycles simultaneously, I would have expected to see some kind of jerkiness in the animations – but they are super smooth!
This is thanks to a new technique named ‘BlendShift Cycling’ which Mark has developed. Instead of shifting colors in discrete steps through a cycle, the colors are blended seamlessly in and out of the color registers. – A very simple and effective technique.
Since I am hosting a number of CRNG cycled animations on my web site, I thought it would be great to have the same quality of animation too. Therefore I have updated my Java applets. For example, the Defender of the Crown title screen animation is now smooth like silk.
@Nullable and @NotNull in JHotDraw
This spring I participated in the course formal methods and functional programming at ETH Zurich.
A major subject of this course were correctness proofs of algorithms.With proofs we can show the correctness of a program. This is in contrast to testing, which can only show the presence of errors, but not their absence.
The ultimate goal is showing the complete correctness of a software application. This is not possible as of today, but major progress has been done in this area for .NET with the Spec# programming system. Among many more advanced things, with Spec# we can show the absence of null pointer errors in a program.
For Java, the IDEA IDE supports @Nullable and @NotNull annotations. A proofer is built into the editor and highlights potential null pointer errors directly in the code.
Since I didn’t want to include IDEA code with another license model into JHotDraw, and since I wanted to tinker a little bit with the concept, I have created a new annotations package in JHotDraw 7.5.1.
My @Nullable and @NotNull annotations are almost identical to the ones in IDEA. The only difference is, that the annotations can be set on classes as well. Usually, I set a @NotNull annotation on a class, to say that – by default – all types and methods defined by this class are non-null. And then, I set the @Nullable annotation to define exceptions of this rule. This nicely keeps down the number of annotations needed in a Java class.
I haven’t yet used these annotations on many classes in JHotDraw, but I plan to use them everywhere in this project.
Update 2011-01-07: In the meantime I dropped my annotation package and use now FindBugs annotations in many places in JHotDraw 7. FindBugs annotations can be defined at various levels in the source code – for example on a method, a class or on a complete package. This keeps the source code tidy. The FindBugs analyzer can use these annotations to find potential problems in the code. I have fixed all problems that I considered potentially ‘harmful’.
In an earlier post, I discussed the design of the “Swatches” color chooser of JHotDraw.
For JHotDraw 7.5.1, I have implemented additional panels for the color chooser. We have got now also a color wheel, and a set of color sliders. read more…