001 /* 002 * HistogramSerialization 003 * 004 * Copyright (c) 2000, 2001, 2002 Marco Schmidt <marcoschmidt@users.sourceforge.net> 005 * All rights reserved. 006 */ 007 008 package net.sourceforge.jiu.color.io; 009 010 import java.io.PrintStream; 011 import net.sourceforge.jiu.color.data.Histogram1D; 012 import net.sourceforge.jiu.color.data.Histogram3D; 013 014 /** 015 * This class has static methods for saving histograms. 016 * Text files (actually, any PrintStream, so you could write to standard output 017 * using {@link java.lang.System#out}) are used to store the histogram information. 018 * Hint: When creating a {@link java.io.PrintStream} object yourself, set the <code>autoFlush</code> 019 * argument of the constructor to <code>false</code>. 020 * You should also wrap your {@link java.io.OutputStream} object into a {@link java.io.BufferedOutputStream} object. 021 * That may speed things up. 022 * <p> 023 * A simple format is used for storing the histograms. 024 * The first line holds the number of components. 025 * This would be 3 for a three-dimensional histogram, e.g.for RGB color images, 026 * or 1 for a one-dimensional histogram as used for a grayscale image. 027 * <p> 028 * Next, as many lines as dimensions follow. 029 * Each line holds the maximum value allowed for that component. 030 * The minimum value is always zero. 031 * Typically, the maximum values are all the same, e.g. 255 for each 032 * component of a 24 bit RGB truecolor image. 033 * <p> 034 * Following these header lines is the actual histogram. 035 * Each line holds a non-zero counter value for one pixel. 036 * The counter is always the last integer value in the line. 037 * <p> 038 * Example: 039 * <pre> 040 * 34 0 55 4033 041 * </pre> 042 * For the histogram of an RGB24Image, this would mean that the pixel 043 * red=34, green=0, blue=55 occurs 4033 times. 044 * <pre> 045 * 0 2 046 * </pre> 047 * For the histogram of any one channel image, this means that the value 0 occurs twice. 048 */ 049 public class HistogramSerialization 050 { 051 private HistogramSerialization() 052 { 053 } 054 055 /** 056 * Saves a one-dimensional histogram to a text output stream. 057 * 058 * @param hist the histogram that will be written to a stream 059 * @param out the stream that will be written to 060 */ 061 public static void save(Histogram1D hist, PrintStream out) 062 { 063 if (hist == null || out == null) 064 { 065 return; 066 } 067 int max = hist.getMaxValue(); 068 out.println("1"); 069 out.println(max); 070 for (int i = 0; i <= max; i++) 071 { 072 int counter = hist.getEntry(i); 073 if (counter != 0) 074 { 075 out.print(i); 076 out.print(' '); 077 out.println(counter); 078 } 079 } 080 out.flush(); 081 } 082 083 /** 084 * Saves a three-dimensional histogram to a text output stream. 085 * 086 * @param hist the histogram to be saved 087 * @param out the output stream where the histogram will be saved to 088 */ 089 public static void save(Histogram3D hist, PrintStream out) 090 { 091 if (hist == null || out == null) 092 { 093 return; 094 } 095 int max1 = hist.getMaxValue(0); 096 int max2 = hist.getMaxValue(1); 097 int max3 = hist.getMaxValue(2); 098 out.println("3"); 099 out.println(max1); 100 out.println(max2); 101 out.println(max3); 102 for (int i = 0; i <= max1; i++) 103 { 104 for (int j = 0; j <= max2; j++) 105 { 106 for (int k = 0; k <= max3; k++) 107 { 108 int counter = hist.getEntry(i, j, k); 109 if (counter != 0) 110 { 111 out.print(i); 112 out.print(' '); 113 out.print(j); 114 out.print(' '); 115 out.print(k); 116 out.print(' '); 117 out.println(counter); 118 } 119 } 120 } 121 } 122 out.flush(); 123 } 124 }