Archive for November, 2008

Java CMYK HOWTO

Monday, November 24th, 2008

Lately, I’ve been playing with printers-trying to control the amount of cyan, magenta, yellow, and black printed by the printer. For various reasons I’ve been doing it in Java,and I’ve run up against some brick walls. There is no good HOWTO available, so I decided to write my own…

Java supports RGB by default and it works well, but it treats everything else like the proverbial red headed step child. To get it to work you have to find an ICC_Profile (CMYK.pf), load the file dynamically, and then do all your image manipulation. I never looked far enough to find out for sure, but I believe you have to do some special work to get it to save as CMYK also. It’s a pain.

The other option is to extend ColorSpace to support CMYK. You still have to save in a funny way, but this is what I decided to do.

First, the ColorSpace extension (available here, note that the code here is all GPL):

public class CMYKColorSpace extends ColorSpace implements Serializable {

	private static final long serialVersionUID = -5982040365555064012L;

	/**
	 * Create a new CMYKColorSpace Instance.
	 */
	public CMYKColorSpace() {
		super(ColorSpace.TYPE_CMYK, 4);
	}

	/**
	 * Converts to CMYK from CIEXYZ. We cheat here, using the RGB colorspace
	 * to do the math for us. The toCIEXYZ function has a description of how
	 * this is supposed to work, which may be implemented in the future.
	 *
	 * @see java.awt.color.ColorSpace#fromCIEXYZ(float[])
	 * @see org.scantegrity.lib.CMYKColorSpace#toCIEXYZ
	 */
	@Override
	public float[] fromCIEXYZ(float[] p_colorvalue) {
		ColorSpace l_cs = ColorSpace.getInstance(ColorSpace.TYPE_RGB);
		float[] l_rgb = l_cs.toCIEXYZ(p_colorvalue);
		return fromRGB(l_rgb);
	}

	/**
	 * Converts a given RGB to CMYK. RGB doesn't really use black, so K will
	 * always be 0. On printers, the black should actually look dark brown.
	 * RGB (an additive space) is simply the backwards from CMY (a subtractive
	 * space), so all we do is:
	 *
	 * 		C = 1-R
	 * 		M = 1-G
	 * 		Y = 1-B
	 *
	 * @param p_rgbvalue - The color to translate
	 * @return a float[4] of the CMYK values.
	 * @see java.awt.color.ColorSpace#fromRGB(float[])
	 */
	@Override
	public float[] fromRGB(float[] p_rgbvalue) {
		/* TODO: Maybe we should do a better job to determine when black should
		 * be used and pulled out? -- At this time, it's not necessary for our
		 * (Scantegrity's) uses.
		 */
		float[] l_res = {0,0,0,0};
		if (p_rgbvalue.length >= 3) {
			l_res[0] = (float)1.0 - p_rgbvalue[0];
			l_res[1] = (float)1.0 - p_rgbvalue[1];
			l_res[2] = (float)1.0 - p_rgbvalue[2];
		}
		return normalize(l_res);
	}

	/**
	 * Converts the CMYK color to CIEXYZ. Because CIEXYZ is 3-component, we
	 * cheat, converting to RGB and then using the RGB colorspace function
	 * to do the conversion. Details on this colorspace are available on
	 * wikipedia:
	 *
	 * http://en.wikipedia.org/wiki/CIE_XYZ_color_space
	 *
	 * There is also an "ideal relationship" to CMYK, which might be implemented
	 * in the future (don't recall the reference we got this from, probably
	 * color.org):
	 *
	 * C = (C' - K)/(1 - K)
	 * M = (M' - K)/(1 - K)
	 * Y = (Y' - K)/(1 - K)
	 * K = Min(C', M', Y')
	 *
	 * X   41.2453 35.7580 18.0423 | 1-C'
	 * Y = 21.2671 71.5160 07.2169 | 1-M'
	 * Z   01.9334 11.9193 95.0227 | 1-Y'
	 *
	 * @see java.awt.color.ColorSpace#toCIEXYZ(float[])
	 */
	@Override
	public float[] toCIEXYZ(float[] p_colorvalue) {
		float[] l_rgb = toRGB(p_colorvalue);
		ColorSpace l_cs = ColorSpace.getInstance(ColorSpace.TYPE_RGB);
		return l_cs.toCIEXYZ(l_rgb);
	}

	/**
	 * Converts CMYK colors to RGB. Note that converting back will be lossy. The
	 * formula for this is:
	 *
	 * K = 1 - K (go to additive)
	 * R = K * (1 - C)
	 * G = K * (1 - M)
	 * B = K * (1 - Y)
	 *
	 * @param p_colorvalue The color in CMYK.
	 * @see java.awt.color.ColorSpace#toRGB(float[])
	 */
	@Override
	public float[] toRGB(float[] p_colorvalue) {
		float[] l_res = {0,0,0};
		if (p_colorvalue.length >= 4)
		{
			float l_black = (float)1.0 - p_colorvalue[3];
			l_res[0] = l_black * ((float)1.0 - p_colorvalue[0]);
			l_res[1] = l_black * ((float)1.0 - p_colorvalue[1]);
			l_res[2] = l_black * ((float)1.0 - p_colorvalue[2]);
		}
		return normalize(l_res);
	}

	/**
	 * Normalize ensures all color values returned are between 0 and 1.
	 *
	 * @param p_colors
	 * @return p_colors, with any values greater than 1 set to 1, and less than
	 * 0 set to 0.
	 */
	private float[] normalize(float[] p_colors) {
		for (int l_i = 0; l_i < p_colors.length; l_i++) {
			if (p_colors[l_i] > (float)1.0) p_colors[l_i] = (float)1.0;
			else if (p_colors[l_i] < (float)0.0) p_colors[l_i] = (float)0.0;
		}
		return p_colors;
	}
}

The color space is used to convert CMYK to RGB or CIEXYZ on demand, which is done whenever Java displays images on the screen, saves them, or copies them to any context in which the color space is different. It also allows you to create CMYK colors and to store the data in a custom color model/sample color model.

Creating CMYK Colors

This is self explanatory:

float[] l_colorComponents = {1, 0, 0, 0};
CMYKColorSpace l_cs = new CMYKColorSpace();
Color l_cyan = new Color(l_cs, l_colorComponents, 1);

Creating a BufferedImage with CMYK Colors

This piece is the most complicated becase you need to understand how Java models the colors in an Image Object. You may want something in-depth, but here’s a short explanation: Each color in java is represented as a “band,” and it is stored in a way defined by Model and stored in a generic “DataBuffer” object.  To get it working you create a (usually component) ColorModel, and then create a sample model with the specific layout (Interleaved, banded or one color per, etc). The sample model creates a Raster, which then creates the Image object.

CMYKColorSpace l_cs = new CMYKColorSpace();
ComponentColorModel l_ccm = new ComponentColorModel(l_cs, false, false,
						1, DataBuffer.TYPE_FLOAT);
int[] l_bandoff = {0, 1, 2, 3}; //Index for each color (C, is index 0, etc)
PixelInterleavedSampleModel l_sm = new PixelInterleavedSampleModel(
						   DataBuffer.TYPE_FLOAT,
						   (int)l_width, (int)l_height,
					     	   4,(int)l_width*4, l_bandoff);
WritableRaster l_raster = WritableRaster.createWritableRaster(l_sm,
							new Point(0,0));
BufferedImage l_ret = new BufferedImage(l_ccm, l_raster, false, null);

Graphics2D l_g2d = l_ret.createGraphics();

Saving CMYK Images

For this, I chose to use the iText library to save in PDF format. All you have to do is pull the DataBuffer out of the raster and save the bytes. You might also want to save in JPEG or another kind of format, which should be reasonably easy (just send the function the right bytes in the right order) once you’ve seen below:

Raster l_tmpRaster = c_img.getRaster();
DataBuffer l_db = l_tmpRaster.getDataBuffer();
byte[] l_bytes = new byte[l_db.getSize()];
for (int l_i = 0; l_i < l_bytes.length; l_i++) {
	l_bytes[l_i] = (byte)Math.round(l_db.getElemFloat(l_i)*(float)255);
}

com.lowagie.text.Image l_img = com.lowagie.text.Image.getInstance(
								l_tmpRaster.getWidth(),
								l_tmpRaster.getHeight(),
								4, 8, l_bytes);
l_img.setDpi(300, 300);
Document l_doc = new Document(new Rectangle(0,0,l_img.getWidth(), l_img.getHeight()));
l_doc.setMargins(0,0,0,0);
PdfWriter.getInstance(l_doc,
			new FileOutputStream("inkeratorout" + c_c + ".pdf"));
l_doc.open();
l_doc.add(l_img);
l_doc.close();

Everything else should work as normal (just remember that it is calling the colorspace functions to do it’s job).

I wrote this in a bit of a rush (i’m pretty busy today). I haven’t had any problems with it yet, but if you do please feel free to leave a note below.d

My Day as an Election Judge in the 2008 Election

Thursday, November 6th, 2008

Cross posted on the Scantegrity Blog.

I had meant to post this yesterday, but I woke up not feeling well and spent the day in bed. I see that Ben Adida and Avi Rubin have already posted their experiences. Aleks Essex also posted his experiences a few weeks ago when he was a worker for the most recent Canadian election.

The chief judges invited all the site judges to meet up at the fire house the night before at 6:30 to make room for everything. We also plugged in the DREs, which you can conveniently do without opening the units. I didn’t ask, but the average age was likely in the 40s. We had 1 high school student who was 17, and my guess is that there was an even split between the 20-40 crowd and the 40+ crowd. There were 3 republicans, and the rest were democrats except for the unaffiliated high school student.

After we finished, the Chief Judges explained what would happen in the morning, and asked if there were any questions. The biggest concerns were potential turnout and if we’d get the extra machines we had up in time with the same number of judges as before. They also expressed concern about a technician showing up and staying for the whole day.

My Election Day Materials for 2008

My Election Day Materials for 2008

Getting Started

I woke up @ 4:30 and arrived a little after 5:30. Myself and another judge set up each DRE while 1-3 judges watched and recorded information. I recall it being a fairly simple process and working pretty well with the exception that the chief judge sometimes had trouble opening the printer compartment. I was a little concerned that the DREs, despite being plugged in all night, all registered only a 60-80% charge.

The electronic poll books were equally easy to set up although some instructions were confusing, telling the user not to plug the poll books into the UPS unit. I think what they meant is to plug a power strip into the UPS unit and then to plug the poll books into that strip, which is what the other judges decided to do.

Voting

Our longest line happened when we first opened the polls, which went all the way out the door and around the corner. People at the end of that line were still cheery, as they only ended up waiting approximately 15-20 minutes. We had a steady stream of people until about 10:30, which picked right up again at 11:00, and stayed mostly steady for the rest of the day with occasional 5-10 minute breaks in flow. Check-in time only took about a minute if you were listed in the poll book.  I’m not totally sure, but I believe voting varied from 1-10 minutes, although some voters probably took longer.

I served as a voting unit judge for the whole day, which is all standing (my legs were killing me that night)! This involved taking a voter authority paper, initialing it, writing the unit number you gave the voter, getting the voter started on the machine, explaining how it worked (if necessary), and dropping the the paper into an envelope on the side. The papers are tallied every hour, and the chief judges make rounds to verify the counts match the machine totals so far.

A majority of our 8 machines were in use for almost the whole day, and at the beginning and towards the end, we had periods of about an hour and a half where all of them were constantly in use. No one used the headset or keypad, although we did have a few voters who requested assistance or needed a chair. Many voters used the large text and apparently the button was not big enough as this was our most asked question.

Glitches

We had no major equipment failures during the voting day. The biggest equipment problem was that the smart cards were sometimes difficult for voters to get into the machine, which we solved by starting it in the machine for the voter, and letting him or her push it in until it snapped.

The other problems all involved the electronic poll books, which sported a confusing user interface to navigate. The judges managed and came up with troubleshooting steps. I was later told that a couple voters would not come up on the local polling site search, but when you went to the statewide search, they did and were registered at our local polling site.

I don’t know a specific number, but at least 15 people or so came in and had trouble during the check-in process. About half voted provisionally, and the rest either went to the correct polling place or admitted they knew they were not registered. I believe there might have been one person who walked out on us, which is really unfortunate. There needs to be a better way to deal with registration problems.

Although it mostly worked well for the vast majority of voters, a few voters were tripped up by the interface. At least one person asked me a question after having pressed cast ballot. Another couple asked why the judges question, a vote for 2 race, was red (because they had not voted for two candidates).

Ballot question wording was also an issue, and understandably so due to the legal language. A number of people called us over to ask me the meaning of various passages, and a few even to ask which answer was “yes”, and which was “no.” It would be best if the language were simple and clear.

Early in the day 1 voter complained about the privacy of the machines. We flipped down the screen for him so it faced the ceiling and told him we could angle the machine however he wanted. It turned out to be a more general problem because there was no curtain. That explains what happened later, when we got a call that someone complained that people were being watched as they voted, and someone from the central office came by to check in on us.

Lots of voters complained about the security of the machines. Some were curious why others did not think they were secure. I just tried to stay away from that discussion as much as possible.

Cleaning Up

At the end of the day, we had 1045 DRE voters, and 8 provisional ballot voters. The chief judges told us that the last major election had 300 out of about 1500-1600 registered voters for our precinct overall (about 200 voters per machine), which is a significant improvement in turnout over previous years.

We finished around 9:30. Tearing down was much worse than getting started, as you had to navigate through a bunch of menus that really didn’t need to be there. An “end election” button that printed the necessary information would have sped things up significantly. The accumulator machine did not recognize the modem, so results were phoned in and then the memory cards were driven back by both the chief judges.

Results

Poll-tape Results for our poll site (the English Consul Volunteer Fire Department, 13-8), were recorded by me as follows:

Candidate Totals
President
Obama/Biden 331
McCain/Palin 684
McKinney/Clemente 5
Barr/Root 2
Nader/Gonzalez 8
Baldwin/Castle 4
Write-In 8
Congressional District 03
Sarbanes 524
Harris 349
Write-In 5
Judicial Circuit 03
Bollinger 662
Stringer 500
Write-In 6
Judge, Court of Appeals, Appellate Circuit 02
Murphy Yes: 662, No: 152
Judge, Court of Special Appeals At Large
Eyler Yes: 675, No: 146
Zarnoch Yes: 627, No: 167
Statewide Ballot Questions
Q1 (Early Voting) Yes: 497, No: 445
Q2 (Slots) Yes: 696, No: 321
Local Ballot Questions
QA Yes: 363, No: 512
QB Excluded from ballot
QC Yes: 614, No: 317
QD Yes: 599, No: 320
QE Yes: 620, No: 291
QF Yes: 500, No: 396
QG Yes: 592, No: 307
QH Yes: 570, No: 319
QI Yes: 493, No: 402
QJ Yes: 535, No: 360
QK Yes: 597, No: 304

I hope I didn’t make a mistake. You may want to look at the ballot.

My 2008 Ballot

Monday, November 3rd, 2008
This is a cross-post from the Scantegrity Blog.
I vote in Maryland tomorrow. Here’s my ballot:
2008 Baltimore County Specimen Ballot

2008 Baltimore County Specimen Ballot

You can also download it in PDF. Only 2^44 unique marking patterns this time. Anyone want to determine how many legal patterns there are using my handy guide?

I’m also serving as an election judge. I’m looking forward to it.

Happy voting!