The ImageToFaxEncoder.java encodes the fax document that is currently opened in the edit pane of the Fax Application window.
Listing 6-8 shows the ImageToFaxEncoder.java file:
Listing 6-8: The ImageToFaxEncoder.java File
import java.awt.Image; import java.awt.image.PixelGrabber; import java.io.PrintStream; public class ImageToFaxEncoder { public boolean debug; public int minBytesLine; public boolean alignEOL; public boolean completeLine; public int lineWidth; public int pageLength; public boolean completePage; public boolean scaleImage; public int scaleFactor; public boolean centerImage; public int whiteColor; public int blackColor; private int EOLimagearray[] = { 86, 101, 86, 102, 86, 103, 86, 104, 86, 105,86, 106, 86, 107, 86, 108, 86, 114, 86, 115, 86, 116, 86, 117, 86, 118, 86, 119, 86, 120,86, 121, 86, 122, 86, 123, 86, 124, 86, 130, 86, 131, 86, 132, 86, 138, 86, 139, 86, 150,86, 151, 87, 101, 87, 102, 87, 103, 87, 104, 87, 105, 87, 106, 87, 107, 87, 108, 87, 109,87, 110, 87, 114, 87, 115, 87, 116, 87, 117, 87, 118, 87, 119, 87, 120, 87, 121, 87, 122,87, 123, 87, 124, 87, 130, 87, 131, 87, 132, 87, 138, 87, 139, 87, 140, 87, 149, 87, 150,87, 151, 88, 101, 88, 102, 88, 109, 88, 110, 88, 114, 88, 115, 88, 129, 88, 130, 88, 132,88, 133, 88, 139, 88, 140, 88, 141, 88, 148, 88, 149, 88, 150, 89, 101, 89, 102, 89, 110,89, 111, 89, 114, 89, 115, 89, 129, 89, 130, 89, 132, 89, 133, 89, 140, 89, 141, 89, 142,89, 147, 89, 148, 89, 149, 90, 101, 90, 102, 90, 110, 90, 111, 90, 114, 90, 115, 90, 128,90, 129, 90, 133, 90, 134, 90, 141, 90, 142, 90, 143, 90, 146, 90, 147, 90, 148, 91, 101,91, 102, 91, 110, 91, 111, 91, 114, 91, 115, 91, 128, 91, 129, 91, 133, 91, 134, 91, 142,91, 143, 91, 144, 91, 145, 91, 146, 91, 147, 92, 101, 92, 102, 92, 109, 92, 110, 92, 114,92, 115, 92, 116, 92, 117, 92, 118, 92, 119, 92, 120, 92, 121, 92, 122, 92, 123, 92, 128,92, 129, 92, 133, 92, 134, 92, 143, 92, 144, 92, 145, 92, 146, 93, 101, 93, 102, 93, 103,93, 104, 93, 105, 93, 106, 93, 107, 93, 108, 93, 109, 93, 110, 93, 114, 93, 115, 93, 116,93, 117, 93, 118, 93, 119, 93, 120, 93, 121, 93, 122, 93, 123, 93, 127, 93, 128, 93, 134,93, 135, 93, 143, 93, 144, 93, 145, 93, 146, 94, 101, 94, 102, 94, 103, 94, 104, 94, 105,94, 106, 94, 107, 94, 108, 94, 109, 94, 110, 94, 114, 94, 115, 94, 127, 94, 128, 94, 129,94, 130, 94, 131, 94, 132, 94, 133, 94, 134, 94, 135, 94, 142, 94, 143, 94, 144, 94, 145,94, 146, 94, 147, 95, 101, 95, 102, 95, 110, 95, 111, 95, 114, 95, 115, 95, 126, 95, 127,95, 128, 95, 129, 95, 130, 95, 131, 95, 132, 95, 133, 95, 134, 95, 135, 95, 136, 95, 141,95, 142, 95, 143, 95, 146, 95, 147, 95, 148, 96, 101, 96, 102, 96, 110, 96, 111, 96, 114,96, 115, 96, 126, 96, 127, 96, 135, 96, 136, 96, 140, 96, 141, 96, 142, 96, 147, 96, 148,96, 149, 97, 101, 97, 102, 97, 110, 97, 111, 97, 114, 97, 115, 97, 126, 97, 127, 97, 135,97, 136, 97, 139, 97, 140, 97, 141, 97, 148, 97, 149, 97, 150, 98, 101, 98, 102, 98, 110,98, 111, 98, 114, 98, 115, 98, 125, 98, 126, 98, 136, 98, 137, 98, 138, 98, 139, 98, 140,98, 149, 98, 150, 98, 151, 99, 101, 99, 102, 99, 111, 99, 112, 99, 114, 99, 115, 99, 125,99, 126, 99, 136, 99, 137, 99, 138, 99, 139, 99, 150, 99, 151 }; public boolean createEOP; private int filterarrar[][] = { { 15, 10, 64 }, { 200, 12, 128 }, { 201, 12, 192 }, { 91, 12, 256 }, { 51, 12, 320 }, { 52, 12, 384 }, { 53, 12, 448 }, { 108, 13, 512 }, { 109, 13, 576 }, { 74, 13, 640 }, { 75, 13, 704 }, { 76, 13, 768 }, { 77, 13, 832 }, { 114, 13, 896 }, { 115, 13, 960 }, { 116, 13, 1024 }, { 117, 13, 1088 }, { 118, 13, 1152 }, { 119, 13, 1216 }, { 82, 13, 1280 }, { 83, 13, 1344 }, { 84, 13, 1408 }, { 85, 13, 1472 }, { 90, 13, 1536 }, { 91, 13, 1600 }, { 100, 13, 1664 }, { 101, 13, 1728 } }; private int matharray[][] = { { 27, 5, 64 }, { 18, 5, 128 }, { 23, 6, 192 }, { 55, 7, 256 }, { 54, 8, 320 }, { 55, 8, 384 }, { 100, 8, 448 }, { 101, 8, 512 }, { 104, 8, 576 }, { 103, 8, 640 }, { 204, 9, 704 }, { 205, 9, 768 }, { 210, 9, 832 }, { 211, 9, 896 }, { 212, 9, 960 }, { 213, 9, 1024 }, { 214, 9, 1088 }, { 215, 9, 1152 }, { 216, 9, 1216 }, { 217, 9, 1280 }, { 218, 9, 1344 }, { 219, 9, 1408 }, { 152, 9, 1472 }, { 153, 9, 1536 }, { 154, 9, 1600 }, { 24, 6, 1664 }, { 155, 9, 1728 } }; private int whitcodearray[][] = { { 55, 10, 0 }, { 2, 3, 1 }, { 3, 2, 2 }, { 2, 2, 3 }, { 3, 3, 4 }, { 3, 4, 5 }, { 2, 4, 6 }, { 3, 5, 7 }, { 5, 6, 8 }, { 4, 6, 9 }, { 4, 7, 10 }, { 5, 7, 11 }, { 7, 7, 12 }, { 4, 8, 13 }, { 7, 8, 14 }, { 24, 9, 15 }, { 23, 10, 16 }, { 24, 10, 17 }, { 8, 10, 18 }, { 103, 11, 19 }, { 104, 11, 20 }, { 108, 11, 21 }, { 55, 11, 22 }, { 40, 11, 23 }, { 23, 11, 24 }, { 24, 11, 25 }, { 202, 12, 26 }, { 203, 12, 27 }, { 204, 12, 28 }, { 205, 12, 29 }, { 104, 12, 30 }, { 105, 12, 31 }, { 106, 12, 32 }, { 107, 12, 33 }, { 210, 12, 34 }, { 211, 12, 35 }, { 212, 12, 36 }, { 213, 12, 37 }, { 214, 12, 38 }, { 215, 12, 39 }, { 108, 12, 40 }, { 109, 12, 41 }, { 218, 12, 42 }, { 219, 12, 43 }, { 84, 12, 44 }, { 85, 12, 45 }, { 86, 12, 46 }, { 87, 12, 47 }, { 100, 12, 48 }, { 101, 12, 49 }, { 82, 12, 50 }, { 83, 12, 51 }, { 36, 12, 52 }, { 55, 12, 53 }, { 56, 12, 54 }, { 39, 12, 55 }, { 40, 12, 56 }, { 88, 12, 57 }, { 89, 12, 58 }, { 43, 12, 59 }, { 44, 12, 60 }, { 90, 12, 61 }, { 102, 12, 62 }, { 103, 12, 63 } }; private int codearray[][] = { { 53, 8, 0 }, { 7, 6, 1 }, { 7, 4, 2 }, { 8, 4, 3 }, { 11, 4, 4 }, { 12, 4, 5 }, { 14, 4, 6 }, { 15, 4, 7 }, { 19, 5, 8 }, { 20, 5, 9 }, { 7, 5, 10 }, { 8, 5, 11 }, { 8, 6, 12 }, { 3, 6, 13 }, { 52, 6, 14 }, { 53, 6, 15 }, { 42, 6, 16 }, { 43, 6, 17 }, { 39, 7, 18 }, { 12, 7, 19 }, { 8, 7, 20 }, { 23, 7, 21 }, { 3, 7, 22 }, { 4, 7, 23 }, { 40, 7, 24 }, { 43, 7, 25 }, { 19, 7, 26 }, { 36, 7, 27 }, { 24, 7, 28 }, { 2, 8, 29 }, { 3, 8, 30 }, { 26, 8, 31 }, { 27, 8, 32 }, { 18, 8, 33 }, { 19, 8, 34 }, { 20, 8, 35 }, { 21, 8, 36 }, { 22, 8, 37 }, { 23, 8, 38 }, { 40, 8, 39 }, { 41, 8, 40 }, { 42, 8, 41 }, { 43, 8, 42 }, { 44, 8, 43 }, { 45, 8, 44 }, { 4, 8, 45 }, { 5, 8, 46 }, { 10, 8, 47 }, { 11, 8, 48 }, { 82, 8, 49 }, { 83, 8, 50 }, { 84, 8, 51 }, { 85, 8, 52 }, { 36, 8, 53 }, { 37, 8, 54 }, { 88, 8, 55 }, { 89, 8, 56 }, { 90, 8, 57 }, { 91, 8, 58 }, { 74, 8, 59 }, { 75, 8, 60 }, { 50, 8, 61 }, { 51, 8, 62 }, { 52, 8, 63 } }; private byte imagebit[]; private int leftshift; private byte masking; private int imagearrcount; private byte bytearray[]; public ImageToFaxEncoder() { debug = false; minBytesLine = 32; alignEOL = true; completeLine = true; lineWidth = 1728; pageLength = 2387; completePage = false; scaleImage = false; scaleFactor = 1; centerImage = false; whiteColor = -1; blackColor = 0; createEOP = false; imagebit = new byte[0x30d40]; leftshift = 7; masking = 0; imagearrcount = 0; bytearray = new byte[0]; } private void setBitMap(int b) { byte bitMask = 0; bitMask = (byte)(1 << leftshift); if(b == 1) masking = (byte)(masking | bitMask); if(debug) System.out.print("".concat(String.valueOf(String.valueOf(b)))); leftshift--; if(leftshift < 0) { leftshift = 7; imagebit[imagearrcount] = masking; imagearrcount++; masking = 0; } } private void setBitMask(int code, int bits) { int mask = 0; for(mask = 1 << bits - 1; mask != 0; mask >>= 1) if((code & mask) > 0) setBitMap(1); else setBitMap(0); if(debug) System.out.print(" "); } private void checkBitMap() { if(alignEOL) while(leftshift != 3) setBitMap(0); for(int h = 0; h < 11; h++) setBitMap(0); setBitMap(1); } private void _$41684() { for(int h = 0; h < 6; h++) checkBitMap(); } public void addFiller(int runl, boolean inWhite) { int codes[][]; int code[]; if(runl >= 64) { if(inWhite) codes = matharray; else codes = filterarrar; code = codes[(int)Math.floor(runl / 64 - 1)]; runl -= code[2]; setBitMask(code[0], code[1]); } if(inWhite) codes = codearray; else codes = whitcodearray; code = codes[runl]; setBitMask(code[0], code[1]); } public byte[] encodeImage(Image i) { int scale = scaleFactor; int currentPageRow = 0; if(scaleImage) { int w = i.getWidth(null); int h = i.getHeight(null); for(int j = 2; j < 10 && j * w <= lineWidth && j * h <= pageLength; j++) scale = j; } imagearrcount = 0; leftshift = 7; masking = 0; checkBitMap(); int iw = i.getWidth(null); int ih = i.getHeight(null); int pix[] = new int[iw * ih]; String s = ""; String c = ""; int runl = 0; int code[] = null; int lineBytesOffset = 0; PixelGrabber pg = new PixelGrabber(i, 0, 0, iw, ih, pix, 0, iw); try { pg.grabPixels(); } catch(Exception e) { System.err.println("".concat(String.valueOf(String.valueOf(e.getMessage())))); } int bp = 1; if(bp == whiteColor) bp = 0; for(int h = 0; h < EOLimagearray.length; h += 2) pix[EOLimagearray[h] * iw + EOLimagearray[h + 1]] = bp; for(int row = 0; row < ih; row++) { for(int sc = 1; sc <= scale; sc++) { currentPageRow++; int ocol = 0; int totalRow = 0; lineBytesOffset = imagearrcount; for(int col = 0; col < iw;) { runl = 0; if(col == 0 && completeLine && centerImage) { totalRow = (lineWidth - iw * scale) / 2; runl = totalRow; } while(col < lineWidth && col < iw && pix[row * iw + col] == whiteColor) { runl += scale; col++; totalRow += scale; } if(col >= iw && completeLine) { runl += lineWidth - totalRow; totalRow = lineWidth; } if(runl >= 64) { code = matharray[(int)Math.floor(runl / 64 - 1)]; runl -= code[2]; if(debug) System.out.println(String.valueOf(String.valueOf( (new StringBuffer("White length=")).append(code[2]).append (" code=").append(code[0]).append(" bits=").append(code[1])))); setBitMask(code[0], code[1]); } code = codearray[runl]; if(debug) System.out.println(String.valueOf(String.valueOf( (new StringBuffer("White length=")).append(runl).append (" code=").append(code[0]).append(" bits=").append(code[1])))); setBitMask(code[0], code[1]); if(col >= lineWidth || col >= iw) break; runl = 0; while(col < lineWidth && col < iw && pix[row * iw + col] != whiteColor) { runl += scale; col++; totalRow += scale; } if(runl >= 64) { code = filterarrar[(int)Math.floor(runl / 64 - 1)]; runl -= code[2]; if(debug) System.out.println(String.valueOf(String.valueOf( (new StringBuffer("Black length=")).append(code[2]).append (" code=").append(code[0]).append(" bits=").append(code[1])))); setBitMask(code[0], code[1]); } code = whitcodearray[runl]; if(debug) System.out.println(String.valueOf(String.valueOf( (new StringBuffer("Black length=")).append(runl).append (" code=").append(code[0]).append(" bits=").append(code[1])))); setBitMask(code[0], code[1]); } if(completeLine && totalRow < lineWidth) { runl = lineWidth - totalRow; if(runl >= 64) { code = matharray[(int)Math.floor(runl / 64 - 1)]; runl -= code[2]; setBitMask(code[0], code[1]); } code = codearray[runl]; setBitMask(code[0], code[1]); } while(minBytesLine > imagearrcount - lineBytesOffset) { int n = 0; while(n < 8) { setBitMap(0); n++; } } checkBitMap(); if(debug) System.out.println(" EOL"); } } if(completePage) for(; currentPageRow < pageLength; currentPageRow++) { runl = lineWidth; if(runl >= 64) { code = matharray[(int)Math.floor(runl / 64 - 1)]; runl -= code[2]; setBitMask(code[0], code[1]); } code = codearray[runl]; setBitMask(code[0], code[1]); checkBitMap(); } if(createEOP) _$41684(); if(leftshift != 7) { imagebit[imagearrcount] = masking; imagearrcount++; } byte result[] = new byte[imagearrcount]; for(int h = 0; h < imagearrcount; h++) result[h] = imagebit[h]; return result; } }
Download this listing.
In the above listing, the ImageToFaxEncoder class encodes the fax document. The various methods defined in the ImageToFaxEncoder.java file are:
setBitMap():Sets the bitmap for the image that the Fax application sends as a fax document.
setBitMask():Sets the mask bit for the image that the Fax application sends as a fax document.
checkBitMap():Checks the bitmap for the image sent as a fax.
_$41684():Invokes the checkBitMap() method.
addFiller():Adds the file filter for the image to open only image files.
encodeImage(): Encodes the image that the Fax application sends as a fax.