├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── dimens.xml
│ │ │ ├── colors.xml
│ │ │ ├── styles.xml
│ │ │ └── strings.xml
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── inbox.png
│ │ │ ├── camera.png
│ │ │ ├── logoohne.png
│ │ │ ├── outbox.png
│ │ │ └── ic_launcher.png
│ │ ├── layout
│ │ │ ├── pref_main.xml
│ │ │ ├── activity_main.xml
│ │ │ └── content_main.xml
│ │ ├── menu
│ │ │ └── menu_main.xml
│ │ ├── xml
│ │ │ └── preferences.xml
│ │ └── values-de
│ │ │ └── strings.xml
│ │ ├── java
│ │ └── click
│ │ │ └── dummer
│ │ │ └── imagesms
│ │ │ ├── MyPreferenceFragment.java
│ │ │ ├── PreferencesActivity.java
│ │ │ ├── PermissionUtils.java
│ │ │ ├── MainActivity.java
│ │ │ └── GifEncoder.java
│ │ └── AndroidManifest.xml
├── release
│ ├── click.dummer.imagesms.apk
│ └── output.json
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── LICENSE
├── gradlew.bat
├── Readme.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
finish() flushes all frames. If
117 | * setSize was not invoked, the size of the first image is used
118 | * for all subsequent frames.
119 | *
120 | * @param im
121 | * BufferedImage containing frame to write.
122 | * @return true if successful.
123 | */
124 | public boolean addFrame(Bitmap im) {
125 | if ((im == null) || !started) {
126 | return false;
127 | }
128 | boolean ok = true;
129 | try {
130 | if (!sizeSet) {
131 | // use first frame's size
132 | setSize(im.getWidth(), im.getHeight());
133 | }
134 | if(image != null) {
135 | // recycle the old Bitmap instance to free its memory
136 | image.recycle();
137 | }
138 | image = im;
139 | getImagePixels(); // convert to correct format if necessary
140 | analyzePixels(); // build color table & map pixels
141 | if (firstFrame) {
142 | writeLSD(); // logical screen descriptior
143 | writePalette(); // global color table
144 | if (repeat >= 0) {
145 | // use NS app extension to indicate reps
146 | writeNetscapeExt();
147 | }
148 | }
149 | writeGraphicCtrlExt(); // write graphic control extension
150 | writeImageDesc(); // image descriptor
151 | if (!firstFrame) {
152 | writePalette(); // local color table
153 | }
154 | writePixels(); // encode and write pixel data
155 | firstFrame = false;
156 | } catch (IOException e) {
157 | ok = false;
158 | }
159 |
160 | return ok;
161 | }
162 |
163 | /**
164 | * Flushes any pending data and closes output file. If writing to an
165 | * OutputStream, the stream is not closed.
166 | */
167 | public boolean finish() {
168 | if (!started)
169 | return false;
170 | boolean ok = true;
171 | started = false;
172 | try {
173 | out.write(0x3b); // gif trailer
174 | out.flush();
175 | if (closeStream) {
176 | out.close();
177 | }
178 | } catch (IOException e) {
179 | ok = false;
180 | }
181 |
182 | // reset for subsequent use
183 | transIndex = 0;
184 | out = null;
185 | image = null;
186 | pixels = null;
187 | indexedPixels = null;
188 | colorTab = null;
189 | closeStream = false;
190 | firstFrame = true;
191 |
192 | return ok;
193 | }
194 |
195 | /**
196 | * Sets frame rate in frames per second. Equivalent to
197 | * setDelay(1000/fps).
198 | *
199 | * @param fps
200 | * float frame rate (frames per second)
201 | */
202 | public void setFrameRate(float fps) {
203 | if (fps != 0f) {
204 | delay = (int)(100 / fps);
205 | }
206 | }
207 |
208 | /**
209 | * Sets quality of color quantization (conversion of images to the maximum 256
210 | * colors allowed by the GIF specification). Lower values (minimum = 1)
211 | * produce better colors, but slow processing significantly. 10 is the
212 | * default, and produces good color mapping at reasonable speeds. Values
213 | * greater than 20 do not yield significant improvements in speed.
214 | *
215 | * @param quality
216 | * int greater than 0.
217 | * @return
218 | */
219 | public void setQuality(int quality) {
220 | if (quality < 1)
221 | quality = 1;
222 | sample = quality;
223 | }
224 |
225 | /**
226 | * Sets the GIF frame size. The default size is the size of the first frame
227 | * added if this method is not invoked.
228 | *
229 | * @param w
230 | * int frame width.
231 | * @param h
232 | * int frame width.
233 | */
234 | public void setSize(int w, int h) {
235 | width = w;
236 | height = h;
237 | if (width < 1)
238 | width = 320;
239 | if (height < 1)
240 | height = 240;
241 | sizeSet = true;
242 | }
243 |
244 | /**
245 | * Sets the GIF frame position. The position is 0,0 by default.
246 | * Useful for only updating a section of the image
247 | *
248 | * @param x
249 | * int frame x-position.
250 | * @param y
251 | * int frame y position.
252 | */
253 | public void setPosition(int x, int y) {
254 | this.x = x;
255 | this.y = y;
256 | }
257 |
258 | /**
259 | * Initiates GIF file creation on the given stream. The stream is not closed
260 | * automatically.
261 | *
262 | * @param os
263 | * OutputStream on which GIF images are written.
264 | * @return false if initial write failed.
265 | */
266 | public boolean start(OutputStream os) {
267 | if (os == null)
268 | return false;
269 | boolean ok = true;
270 | closeStream = false;
271 | out = os;
272 | try {
273 | writeString("GIF89a"); // header
274 | } catch (IOException e) {
275 | ok = false;
276 | }
277 | return started = ok;
278 | }
279 |
280 | /**
281 | * Analyzes image colors and creates color map.
282 | */
283 | protected void analyzePixels() {
284 | int len = pixels.length;
285 | int nPix = len / 3;
286 | indexedPixels = new byte[nPix];
287 | NeuQuant nq = new NeuQuant(pixels, len, sample);
288 | // initialize quantizer
289 | colorTab = nq.process(); // create reduced palette
290 | // convert map from BGR to RGB
291 | for (int i = 0; i < colorTab.length; i += 3) {
292 | byte temp = colorTab[i];
293 | colorTab[i] = colorTab[i + 2];
294 | colorTab[i + 2] = temp;
295 | usedEntry[i / 3] = false;
296 | }
297 | // map image pixels to new palette
298 | int k = 0;
299 | for (int i = 0; i < nPix; i++) {
300 | int index = nq.map(pixels[k++] & 0xff, pixels[k++] & 0xff, pixels[k++] & 0xff);
301 | usedEntry[index] = true;
302 | indexedPixels[i] = (byte) index;
303 | }
304 | pixels = null;
305 | colorDepth = 8;
306 | palSize = 7;
307 | // get closest match to transparent color if specified
308 | if (transparent != -1) {
309 | transIndex = findClosest(transparent);
310 | }
311 | }
312 |
313 | /**
314 | * Returns index of palette color closest to c
315 | *
316 | */
317 | protected int findClosest(int c) {
318 | if (colorTab == null)
319 | return -1;
320 | int r = (c >> 16) & 0xff;
321 | int g = (c >> 8) & 0xff;
322 | int b = (c >> 0) & 0xff;
323 | int minpos = 0;
324 | int dmin = 256 * 256 * 256;
325 | int len = colorTab.length;
326 | for (int i = 0; i < len;) {
327 | int dr = r - (colorTab[i++] & 0xff);
328 | int dg = g - (colorTab[i++] & 0xff);
329 | int db = b - (colorTab[i] & 0xff);
330 | int d = dr * dr + dg * dg + db * db;
331 | int index = i / 3;
332 | if (usedEntry[index] && (d < dmin)) {
333 | dmin = d;
334 | minpos = index;
335 | }
336 | i++;
337 | }
338 | return minpos;
339 | }
340 |
341 | /**
342 | * Extracts image pixels into byte array "pixels"
343 | */
344 | protected void getImagePixels() {
345 | int w = image.getWidth();
346 | int h = image.getHeight();
347 | if ((w != width) || (h != height)) {
348 | // create new image with right size/format
349 | Bitmap temp = Bitmap.createBitmap(width, height, Config.RGB_565);
350 | Canvas g = new Canvas(temp);
351 | g.drawBitmap(image, 0, 0, new Paint());
352 | image = temp;
353 | }
354 | int[] data = getImageData(image);
355 | pixels = new byte[data.length * 3];
356 | for (int i = 0; i < data.length; i++) {
357 | int td = data[i];
358 | int tind = i * 3;
359 | pixels[tind++] = (byte) ((td >> 0) & 0xFF);
360 | pixels[tind++] = (byte) ((td >> 8) & 0xFF);
361 | pixels[tind] = (byte) ((td >> 16) & 0xFF);
362 | }
363 | }
364 | protected int[] getImageData(Bitmap img) {
365 | int w = img.getWidth();
366 | int h = img.getHeight();
367 |
368 | int[] data = new int[w * h];
369 | img.getPixels(data, 0, w, 0, 0, w, h);
370 | return data;
371 | }
372 |
373 | /**
374 | * Writes Graphic Control Extension
375 | */
376 | protected void writeGraphicCtrlExt() throws IOException {
377 | out.write(0x21); // extension introducer
378 | out.write(0xf9); // GCE label
379 | out.write(4); // data block size
380 | int transp, disp;
381 | if (transparent == -1) {
382 | transp = 0;
383 | disp = 0; // dispose = no action
384 | } else {
385 | transp = 1;
386 | disp = 2; // force clear if using transparent color
387 | }
388 | if (dispose >= 0) {
389 | disp = dispose & 7; // user override
390 | }
391 | disp <<= 2;
392 |
393 | // packed fields
394 | out.write(0 | // 1:3 reserved
395 | disp | // 4:6 disposal
396 | 0 | // 7 user input - 0 = none
397 | transp); // 8 transparency flag
398 |
399 | writeShort(delay); // delay x 1/100 sec
400 | out.write(transIndex); // transparent color index
401 | out.write(0); // block terminator
402 | }
403 |
404 | /**
405 | * Writes Image Descriptor
406 | */
407 | protected void writeImageDesc() throws IOException {
408 | out.write(0x2c); // image separator
409 | writeShort(x); // image position x,y = 0,0
410 | writeShort(y);
411 | writeShort(width); // image size
412 | writeShort(height);
413 | // packed fields
414 | if (firstFrame) {
415 | // no LCT - GCT is used for first (or only) frame
416 | out.write(0);
417 | } else {
418 | // specify normal LCT
419 | out.write(0x80 | // 1 local color table 1=yes
420 | 0 | // 2 interlace - 0=no
421 | 0 | // 3 sorted - 0=no
422 | 0 | // 4-5 reserved
423 | palSize); // 6-8 size of color table
424 | }
425 | }
426 |
427 | /**
428 | * Writes Logical Screen Descriptor
429 | */
430 | protected void writeLSD() throws IOException {
431 | // logical screen size
432 | writeShort(width);
433 | writeShort(height);
434 | // packed fields
435 | out.write((0x80 | // 1 : global color table flag = 1 (gct used)
436 | 0x70 | // 2-4 : color resolution = 7
437 | 0x00 | // 5 : gct sort flag = 0
438 | palSize)); // 6-8 : gct size
439 |
440 | out.write(0); // background color index
441 | out.write(0); // pixel aspect ratio - assume 1:1
442 | }
443 |
444 | /**
445 | * Writes Netscape application extension to define repeat count.
446 | */
447 | protected void writeNetscapeExt() throws IOException {
448 | out.write(0x21); // extension introducer
449 | out.write(0xff); // app extension label
450 | out.write(11); // block size
451 | writeString("NETSCAPE" + "2.0"); // app id + auth code
452 | out.write(3); // sub-block size
453 | out.write(1); // loop sub-block id
454 | writeShort(repeat); // loop count (extra iterations, 0=repeat forever)
455 | out.write(0); // block terminator
456 | }
457 |
458 | /**
459 | * Writes color table
460 | */
461 | protected void writePalette() throws IOException {
462 | out.write(colorTab, 0, colorTab.length);
463 | int n = (3 * 256) - colorTab.length;
464 | for (int i = 0; i < n; i++) {
465 | out.write(0);
466 | }
467 | }
468 |
469 | /**
470 | * Encodes and writes pixel data
471 | */
472 | protected void writePixels() throws IOException {
473 | LZWEncoder encoder = new LZWEncoder(width, height, indexedPixels, colorDepth);
474 | encoder.encode(out);
475 | }
476 |
477 | /**
478 | * Write 16-bit value to output stream, LSB first
479 | */
480 | protected void writeShort(int value) throws IOException {
481 | out.write(value & 0xff);
482 | out.write((value >> 8) & 0xff);
483 | }
484 |
485 | /**
486 | * Writes string to output stream
487 | */
488 | protected void writeString(String s) throws IOException {
489 | for (int i = 0; i < s.length(); i++) {
490 | out.write((byte) s.charAt(i));
491 | }
492 | }
493 | }
494 |
495 | /*
496 | * NeuQuant Neural-Net Quantization Algorithm
497 | * ------------------------------------------
498 | *
499 | * Copyright (c) 1994 Anthony Dekker
500 | *
501 | * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. See
502 | * "Kohonen neural networks for optimal colour quantization" in "Network:
503 | * Computation in Neural Systems" Vol. 5 (1994) pp 351-367. for a discussion of
504 | * the algorithm.
505 | *
506 | * Any party obtaining a copy of these files from the author, directly or
507 | * indirectly, is granted, free of charge, a full and unrestricted irrevocable,
508 | * world-wide, paid up, royalty-free, nonexclusive right and license to deal in
509 | * this software and documentation files (the "Software"), including without
510 | * limitation the rights to use, copy, modify, merge, publish, distribute,
511 | * sublicense, and/or sell copies of the Software, and to permit persons who
512 | * receive copies from any such party to do so, with the only requirement being
513 | * that this copyright notice remain intact.
514 | */
515 |
516 | // Ported to Java 12/00 K Weiner
517 | class NeuQuant {
518 |
519 | protected static final int netsize = 256; /* number of colours used */
520 |
521 | /* four primes near 500 - assume no image has a length so large */
522 | /* that it is divisible by all four primes */
523 | protected static final int prime1 = 499;
524 |
525 | protected static final int prime2 = 491;
526 |
527 | protected static final int prime3 = 487;
528 |
529 | protected static final int prime4 = 503;
530 |
531 | protected static final int minpicturebytes = (3 * prime4);
532 |
533 | /* minimum size for input image */
534 |
535 | /*
536 | * Program Skeleton ---------------- [select samplefac in range 1..30] [read
537 | * image from input file] pic = (unsigned char*) malloc(3*width*height);
538 | * initnet(pic,3*width*height,samplefac); learn(); unbiasnet(); [write output
539 | * image header, using writecolourmap(f)] inxbuild(); write output image using
540 | * inxsearch(b,g,r)
541 | */
542 |
543 | /*
544 | * Network Definitions -------------------
545 | */
546 |
547 | protected static final int maxnetpos = (netsize - 1);
548 |
549 | protected static final int netbiasshift = 4; /* bias for colour values */
550 |
551 | protected static final int ncycles = 100; /* no. of learning cycles */
552 |
553 | /* defs for freq and bias */
554 | protected static final int intbiasshift = 16; /* bias for fractions */
555 |
556 | protected static final int intbias = (((int) 1) << intbiasshift);
557 |
558 | protected static final int gammashift = 10; /* gamma = 1024 */
559 |
560 | protected static final int gamma = (((int) 1) << gammashift);
561 |
562 | protected static final int betashift = 10;
563 |
564 | protected static final int beta = (intbias >> betashift); /* beta = 1/1024 */
565 |
566 | protected static final int betagamma = (intbias << (gammashift - betashift));
567 |
568 | /* defs for decreasing radius factor */
569 | protected static final int initrad = (netsize >> 3); /*
570 | * for 256 cols, radius
571 | * starts
572 | */
573 |
574 | protected static final int radiusbiasshift = 6; /* at 32.0 biased by 6 bits */
575 |
576 | protected static final int radiusbias = (((int) 1) << radiusbiasshift);
577 |
578 | protected static final int initradius = (initrad * radiusbias); /*
579 | * and
580 | * decreases
581 | * by a
582 | */
583 |
584 | protected static final int radiusdec = 30; /* factor of 1/30 each cycle */
585 |
586 | /* defs for decreasing alpha factor */
587 | protected static final int alphabiasshift = 10; /* alpha starts at 1.0 */
588 |
589 | protected static final int initalpha = (((int) 1) << alphabiasshift);
590 |
591 | protected int alphadec; /* biased by 10 bits */
592 |
593 | /* radbias and alpharadbias used for radpower calculation */
594 | protected static final int radbiasshift = 8;
595 |
596 | protected static final int radbias = (((int) 1) << radbiasshift);
597 |
598 | protected static final int alpharadbshift = (alphabiasshift + radbiasshift);
599 |
600 | protected static final int alpharadbias = (((int) 1) << alpharadbshift);
601 |
602 | /*
603 | * Types and Global Variables --------------------------
604 | */
605 |
606 | protected byte[] thepicture; /* the input image itself */
607 |
608 | protected int lengthcount; /* lengthcount = H*W*3 */
609 |
610 | protected int samplefac; /* sampling factor 1..30 */
611 |
612 | // typedef int pixel[4]; /* BGRc */
613 | protected int[][] network; /* the network itself - [netsize][4] */
614 |
615 | protected int[] netindex = new int[256];
616 |
617 | /* for network lookup - really 256 */
618 |
619 | protected int[] bias = new int[netsize];
620 |
621 | /* bias and freq arrays for learning */
622 | protected int[] freq = new int[netsize];
623 |
624 | protected int[] radpower = new int[initrad];
625 |
626 | /* radpower for precomputation */
627 |
628 | /*
629 | * Initialise network in range (0,0,0) to (255,255,255) and set parameters
630 | * -----------------------------------------------------------------------
631 | */
632 | public NeuQuant(byte[] thepic, int len, int sample) {
633 |
634 | int i;
635 | int[] p;
636 |
637 | thepicture = thepic;
638 | lengthcount = len;
639 | samplefac = sample;
640 |
641 | network = new int[netsize][];
642 | for (i = 0; i < netsize; i++) {
643 | network[i] = new int[4];
644 | p = network[i];
645 | p[0] = p[1] = p[2] = (i << (netbiasshift + 8)) / netsize;
646 | freq[i] = intbias / netsize; /* 1/netsize */
647 | bias[i] = 0;
648 | }
649 | }
650 |
651 | public byte[] colorMap() {
652 | byte[] map = new byte[3 * netsize];
653 | int[] index = new int[netsize];
654 | for (int i = 0; i < netsize; i++)
655 | index[network[i][3]] = i;
656 | int k = 0;
657 | for (int i = 0; i < netsize; i++) {
658 | int j = index[i];
659 | map[k++] = (byte) (network[j][0]);
660 | map[k++] = (byte) (network[j][1]);
661 | map[k++] = (byte) (network[j][2]);
662 | }
663 | return map;
664 | }
665 |
666 | /*
667 | * Insertion sort of network and building of netindex[0..255] (to do after
668 | * unbias)
669 | * -------------------------------------------------------------------------------
670 | */
671 | public void inxbuild() {
672 |
673 | int i, j, smallpos, smallval;
674 | int[] p;
675 | int[] q;
676 | int previouscol, startpos;
677 |
678 | previouscol = 0;
679 | startpos = 0;
680 | for (i = 0; i < netsize; i++) {
681 | p = network[i];
682 | smallpos = i;
683 | smallval = p[1]; /* index on g */
684 | /* find smallest in i..netsize-1 */
685 | for (j = i + 1; j < netsize; j++) {
686 | q = network[j];
687 | if (q[1] < smallval) { /* index on g */
688 | smallpos = j;
689 | smallval = q[1]; /* index on g */
690 | }
691 | }
692 | q = network[smallpos];
693 | /* swap p (i) and q (smallpos) entries */
694 | if (i != smallpos) {
695 | j = q[0];
696 | q[0] = p[0];
697 | p[0] = j;
698 | j = q[1];
699 | q[1] = p[1];
700 | p[1] = j;
701 | j = q[2];
702 | q[2] = p[2];
703 | p[2] = j;
704 | j = q[3];
705 | q[3] = p[3];
706 | p[3] = j;
707 | }
708 | /* smallval entry is now in position i */
709 | if (smallval != previouscol) {
710 | netindex[previouscol] = (startpos + i) >> 1;
711 | for (j = previouscol + 1; j < smallval; j++)
712 | netindex[j] = i;
713 | previouscol = smallval;
714 | startpos = i;
715 | }
716 | }
717 | netindex[previouscol] = (startpos + maxnetpos) >> 1;
718 | for (j = previouscol + 1; j < 256; j++)
719 | netindex[j] = maxnetpos; /* really 256 */
720 | }
721 |
722 | /*
723 | * Main Learning Loop ------------------
724 | */
725 | public void learn() {
726 |
727 | int i, j, b, g, r;
728 | int radius, rad, alpha, step, delta, samplepixels;
729 | byte[] p;
730 | int pix, lim;
731 |
732 | if (lengthcount < minpicturebytes)
733 | samplefac = 1;
734 | alphadec = 30 + ((samplefac - 1) / 3);
735 | p = thepicture;
736 | pix = 0;
737 | lim = lengthcount;
738 | samplepixels = lengthcount / (3 * samplefac);
739 | delta = samplepixels / ncycles;
740 | alpha = initalpha;
741 | radius = initradius;
742 |
743 | rad = radius >> radiusbiasshift;
744 | if (rad <= 1)
745 | rad = 0;
746 | for (i = 0; i < rad; i++)
747 | radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad));
748 |
749 | // fprintf(stderr,"beginning 1D learning: initial radius=%d\n", rad);
750 |
751 | if (lengthcount < minpicturebytes)
752 | step = 3;
753 | else if ((lengthcount % prime1) != 0)
754 | step = 3 * prime1;
755 | else {
756 | if ((lengthcount % prime2) != 0)
757 | step = 3 * prime2;
758 | else {
759 | if ((lengthcount % prime3) != 0)
760 | step = 3 * prime3;
761 | else
762 | step = 3 * prime4;
763 | }
764 | }
765 |
766 | i = 0;
767 | while (i < samplepixels) {
768 | b = (p[pix + 0] & 0xff) << netbiasshift;
769 | g = (p[pix + 1] & 0xff) << netbiasshift;
770 | r = (p[pix + 2] & 0xff) << netbiasshift;
771 | j = contest(b, g, r);
772 |
773 | altersingle(alpha, j, b, g, r);
774 | if (rad != 0)
775 | alterneigh(rad, j, b, g, r); /* alter neighbours */
776 |
777 | pix += step;
778 | if (pix >= lim)
779 | pix -= lengthcount;
780 |
781 | i++;
782 | if (delta == 0)
783 | delta = 1;
784 | if (i % delta == 0) {
785 | alpha -= alpha / alphadec;
786 | radius -= radius / radiusdec;
787 | rad = radius >> radiusbiasshift;
788 | if (rad <= 1)
789 | rad = 0;
790 | for (j = 0; j < rad; j++)
791 | radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad));
792 | }
793 | }
794 | // fprintf(stderr,"finished 1D learning: final alpha=%f
795 | // !\n",((float)alpha)/initalpha);
796 | }
797 |
798 | /*
799 | * Search for BGR values 0..255 (after net is unbiased) and return colour
800 | * index
801 | * ----------------------------------------------------------------------------
802 | */
803 | public int map(int b, int g, int r) {
804 |
805 | int i, j, dist, a, bestd;
806 | int[] p;
807 | int best;
808 |
809 | bestd = 1000; /* biggest possible dist is 256*3 */
810 | best = -1;
811 | i = netindex[g]; /* index on g */
812 | j = i - 1; /* start at netindex[g] and work outwards */
813 |
814 | while ((i < netsize) || (j >= 0)) {
815 | if (i < netsize) {
816 | p = network[i];
817 | dist = p[1] - g; /* inx key */
818 | if (dist >= bestd)
819 | i = netsize; /* stop iter */
820 | else {
821 | i++;
822 | if (dist < 0)
823 | dist = -dist;
824 | a = p[0] - b;
825 | if (a < 0)
826 | a = -a;
827 | dist += a;
828 | if (dist < bestd) {
829 | a = p[2] - r;
830 | if (a < 0)
831 | a = -a;
832 | dist += a;
833 | if (dist < bestd) {
834 | bestd = dist;
835 | best = p[3];
836 | }
837 | }
838 | }
839 | }
840 | if (j >= 0) {
841 | p = network[j];
842 | dist = g - p[1]; /* inx key - reverse dif */
843 | if (dist >= bestd)
844 | j = -1; /* stop iter */
845 | else {
846 | j--;
847 | if (dist < 0)
848 | dist = -dist;
849 | a = p[0] - b;
850 | if (a < 0)
851 | a = -a;
852 | dist += a;
853 | if (dist < bestd) {
854 | a = p[2] - r;
855 | if (a < 0)
856 | a = -a;
857 | dist += a;
858 | if (dist < bestd) {
859 | bestd = dist;
860 | best = p[3];
861 | }
862 | }
863 | }
864 | }
865 | }
866 | return (best);
867 | }
868 |
869 | public byte[] process() {
870 | learn();
871 | unbiasnet();
872 | inxbuild();
873 | return colorMap();
874 | }
875 |
876 | /*
877 | * Unbias network to give byte values 0..255 and record position i to prepare
878 | * for sort
879 | * -----------------------------------------------------------------------------------
880 | */
881 | public void unbiasnet() {
882 |
883 | int i;
884 |
885 | for (i = 0; i < netsize; i++) {
886 | network[i][0] >>= netbiasshift;
887 | network[i][1] >>= netbiasshift;
888 | network[i][2] >>= netbiasshift;
889 | network[i][3] = i; /* record colour no */
890 | }
891 | }
892 |
893 | /*
894 | * Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in
895 | * radpower[|i-j|]
896 | * ---------------------------------------------------------------------------------
897 | */
898 | protected void alterneigh(int rad, int i, int b, int g, int r) {
899 |
900 | int j, k, lo, hi, a, m;
901 | int[] p;
902 |
903 | lo = i - rad;
904 | if (lo < -1)
905 | lo = -1;
906 | hi = i + rad;
907 | if (hi > netsize)
908 | hi = netsize;
909 |
910 | j = i + 1;
911 | k = i - 1;
912 | m = 1;
913 | while ((j < hi) || (k > lo)) {
914 | a = radpower[m++];
915 | if (j < hi) {
916 | p = network[j++];
917 | try {
918 | p[0] -= (a * (p[0] - b)) / alpharadbias;
919 | p[1] -= (a * (p[1] - g)) / alpharadbias;
920 | p[2] -= (a * (p[2] - r)) / alpharadbias;
921 | } catch (Exception e) {
922 | } // prevents 1.3 miscompilation
923 | }
924 | if (k > lo) {
925 | p = network[k--];
926 | try {
927 | p[0] -= (a * (p[0] - b)) / alpharadbias;
928 | p[1] -= (a * (p[1] - g)) / alpharadbias;
929 | p[2] -= (a * (p[2] - r)) / alpharadbias;
930 | } catch (Exception e) {
931 | }
932 | }
933 | }
934 | }
935 |
936 | /*
937 | * Move neuron i towards biased (b,g,r) by factor alpha
938 | * ----------------------------------------------------
939 | */
940 | protected void altersingle(int alpha, int i, int b, int g, int r) {
941 |
942 | /* alter hit neuron */
943 | int[] n = network[i];
944 | n[0] -= (alpha * (n[0] - b)) / initalpha;
945 | n[1] -= (alpha * (n[1] - g)) / initalpha;
946 | n[2] -= (alpha * (n[2] - r)) / initalpha;
947 | }
948 |
949 | /*
950 | * Search for biased BGR values ----------------------------
951 | */
952 | protected int contest(int b, int g, int r) {
953 |
954 | /* finds closest neuron (min dist) and updates freq */
955 | /* finds best neuron (min dist-bias) and returns position */
956 | /* for frequently chosen neurons, freq[i] is high and bias[i] is negative */
957 | /* bias[i] = gamma*((1/netsize)-freq[i]) */
958 |
959 | int i, dist, a, biasdist, betafreq;
960 | int bestpos, bestbiaspos, bestd, bestbiasd;
961 | int[] n;
962 |
963 | bestd = ~(((int) 1) << 31);
964 | bestbiasd = bestd;
965 | bestpos = -1;
966 | bestbiaspos = bestpos;
967 |
968 | for (i = 0; i < netsize; i++) {
969 | n = network[i];
970 | dist = n[0] - b;
971 | if (dist < 0)
972 | dist = -dist;
973 | a = n[1] - g;
974 | if (a < 0)
975 | a = -a;
976 | dist += a;
977 | a = n[2] - r;
978 | if (a < 0)
979 | a = -a;
980 | dist += a;
981 | if (dist < bestd) {
982 | bestd = dist;
983 | bestpos = i;
984 | }
985 | biasdist = dist - ((bias[i]) >> (intbiasshift - netbiasshift));
986 | if (biasdist < bestbiasd) {
987 | bestbiasd = biasdist;
988 | bestbiaspos = i;
989 | }
990 | betafreq = (freq[i] >> betashift);
991 | freq[i] -= betafreq;
992 | bias[i] += (betafreq << gammashift);
993 | }
994 | freq[bestpos] += beta;
995 | bias[bestpos] -= betagamma;
996 | return (bestbiaspos);
997 | }
998 | }
999 |
1000 | // ==============================================================================
1001 | // Adapted from Jef Poskanzer's Java port by way of J. M. G. Elliott.
1002 | // K Weiner 12/00
1003 |
1004 | class LZWEncoder {
1005 |
1006 | private static final int EOF = -1;
1007 |
1008 | private int imgW, imgH;
1009 |
1010 | private byte[] pixAry;
1011 |
1012 | private int initCodeSize;
1013 |
1014 | private int remaining;
1015 |
1016 | private int curPixel;
1017 |
1018 | // GIFCOMPR.C - GIF Image compression routines
1019 | //
1020 | // Lempel-Ziv compression based on 'compress'. GIF modifications by
1021 | // David Rowley (mgardi@watdcsu.waterloo.edu)
1022 |
1023 | // General DEFINEs
1024 |
1025 | static final int BITS = 12;
1026 |
1027 | static final int HSIZE = 5003; // 80% occupancy
1028 |
1029 | // GIF Image compression - modified 'compress'
1030 | //
1031 | // Based on: compress.c - File compression ala IEEE Computer, June 1984.
1032 | //
1033 | // By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
1034 | // Jim McKie (decvax!mcvax!jim)
1035 | // Steve Davies (decvax!vax135!petsd!peora!srd)
1036 | // Ken Turkowski (decvax!decwrl!turtlevax!ken)
1037 | // James A. Woods (decvax!ihnp4!ames!jaw)
1038 | // Joe Orost (decvax!vax135!petsd!joe)
1039 |
1040 | int n_bits; // number of bits/code
1041 |
1042 | int maxbits = BITS; // user settable max # bits/code
1043 |
1044 | int maxcode; // maximum code, given n_bits
1045 |
1046 | int maxmaxcode = 1 << BITS; // should NEVER generate this code
1047 |
1048 | int[] htab = new int[HSIZE];
1049 |
1050 | int[] codetab = new int[HSIZE];
1051 |
1052 | int hsize = HSIZE; // for dynamic table sizing
1053 |
1054 | int free_ent = 0; // first unused entry
1055 |
1056 | // block compression parameters -- after all codes are used up,
1057 | // and compression rate changes, start over.
1058 | boolean clear_flg = false;
1059 |
1060 | // Algorithm: use open addressing double hashing (no chaining) on the
1061 | // prefix code / next character combination. We do a variant of Knuth's
1062 | // algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
1063 | // secondary probe. Here, the modular division first probe is gives way
1064 | // to a faster exclusive-or manipulation. Also do block compression with
1065 | // an adaptive reset, whereby the code table is cleared when the compression
1066 | // ratio decreases, but after the table fills. The variable-length output
1067 | // codes are re-sized at this point, and a special CLEAR code is generated
1068 | // for the decompressor. Late addition: construct the table according to
1069 | // file size for noticeable speed improvement on small files. Please direct
1070 | // questions about this implementation to ames!jaw.
1071 |
1072 | int g_init_bits;
1073 |
1074 | int ClearCode;
1075 |
1076 | int EOFCode;
1077 |
1078 | // output
1079 | //
1080 | // Output the given code.
1081 | // Inputs:
1082 | // code: A n_bits-bit integer. If == -1, then EOF. This assumes
1083 | // that n_bits =< wordsize - 1.
1084 | // Outputs:
1085 | // Outputs code to the file.
1086 | // Assumptions:
1087 | // Chars are 8 bits long.
1088 | // Algorithm:
1089 | // Maintain a BITS character long buffer (so that 8 codes will
1090 | // fit in it exactly). Use the VAX insv instruction to insert each
1091 | // code in turn. When the buffer fills up empty it and start over.
1092 |
1093 | int cur_accum = 0;
1094 |
1095 | int cur_bits = 0;
1096 |
1097 | int masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF,
1098 | 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1099 |
1100 | // Number of characters so far in this 'packet'
1101 | int a_count;
1102 |
1103 | // Define the storage for the packet accumulator
1104 | byte[] accum = new byte[256];
1105 |
1106 | // ----------------------------------------------------------------------------
1107 | LZWEncoder(int width, int height, byte[] pixels, int color_depth) {
1108 | imgW = width;
1109 | imgH = height;
1110 | pixAry = pixels;
1111 | initCodeSize = Math.max(2, color_depth);
1112 | }
1113 |
1114 | // Add a character to the end of the current packet, and if it is 254
1115 | // characters, flush the packet to disk.
1116 | void char_out(byte c, OutputStream outs) throws IOException {
1117 | accum[a_count++] = c;
1118 | if (a_count >= 254)
1119 | flush_char(outs);
1120 | }
1121 |
1122 | // Clear out the hash table
1123 |
1124 | // table clear for block compress
1125 | void cl_block(OutputStream outs) throws IOException {
1126 | cl_hash(hsize);
1127 | free_ent = ClearCode + 2;
1128 | clear_flg = true;
1129 |
1130 | output(ClearCode, outs);
1131 | }
1132 |
1133 | // reset code table
1134 | void cl_hash(int hsize) {
1135 | for (int i = 0; i < hsize; ++i)
1136 | htab[i] = -1;
1137 | }
1138 |
1139 | void compress(int init_bits, OutputStream outs) throws IOException {
1140 | int fcode;
1141 | int i /* = 0 */;
1142 | int c;
1143 | int ent;
1144 | int disp;
1145 | int hsize_reg;
1146 | int hshift;
1147 |
1148 | // Set up the globals: g_init_bits - initial number of bits
1149 | g_init_bits = init_bits;
1150 |
1151 | // Set up the necessary values
1152 | clear_flg = false;
1153 | n_bits = g_init_bits;
1154 | maxcode = MAXCODE(n_bits);
1155 |
1156 | ClearCode = 1 << (init_bits - 1);
1157 | EOFCode = ClearCode + 1;
1158 | free_ent = ClearCode + 2;
1159 |
1160 | a_count = 0; // clear packet
1161 |
1162 | ent = nextPixel();
1163 |
1164 | hshift = 0;
1165 | for (fcode = hsize; fcode < 65536; fcode *= 2)
1166 | ++hshift;
1167 | hshift = 8 - hshift; // set hash code range bound
1168 |
1169 | hsize_reg = hsize;
1170 | cl_hash(hsize_reg); // clear hash table
1171 |
1172 | output(ClearCode, outs);
1173 |
1174 | outer_loop: while ((c = nextPixel()) != EOF) {
1175 | fcode = (c << maxbits) + ent;
1176 | i = (c << hshift) ^ ent; // xor hashing
1177 |
1178 | if (htab[i] == fcode) {
1179 | ent = codetab[i];
1180 | continue;
1181 | } else if (htab[i] >= 0) // non-empty slot
1182 | {
1183 | disp = hsize_reg - i; // secondary hash (after G. Knott)
1184 | if (i == 0)
1185 | disp = 1;
1186 | do {
1187 | if ((i -= disp) < 0)
1188 | i += hsize_reg;
1189 |
1190 | if (htab[i] == fcode) {
1191 | ent = codetab[i];
1192 | continue outer_loop;
1193 | }
1194 | } while (htab[i] >= 0);
1195 | }
1196 | output(ent, outs);
1197 | ent = c;
1198 | if (free_ent < maxmaxcode) {
1199 | codetab[i] = free_ent++; // code -> hashtable
1200 | htab[i] = fcode;
1201 | } else
1202 | cl_block(outs);
1203 | }
1204 | // Put out the final code.
1205 | output(ent, outs);
1206 | output(EOFCode, outs);
1207 | }
1208 |
1209 | // ----------------------------------------------------------------------------
1210 | void encode(OutputStream os) throws IOException {
1211 | os.write(initCodeSize); // write "initial code size" byte
1212 |
1213 | remaining = imgW * imgH; // reset navigation variables
1214 | curPixel = 0;
1215 |
1216 | compress(initCodeSize + 1, os); // compress and write the pixel data
1217 |
1218 | os.write(0); // write block terminator
1219 | }
1220 |
1221 | // Flush the packet to disk, and reset the accumulator
1222 | void flush_char(OutputStream outs) throws IOException {
1223 | if (a_count > 0) {
1224 | outs.write(a_count);
1225 | outs.write(accum, 0, a_count);
1226 | a_count = 0;
1227 | }
1228 | }
1229 |
1230 | final int MAXCODE(int n_bits) {
1231 | return (1 << n_bits) - 1;
1232 | }
1233 |
1234 | // ----------------------------------------------------------------------------
1235 | // Return the next pixel from the image
1236 | // ----------------------------------------------------------------------------
1237 | private int nextPixel() {
1238 | if (remaining == 0)
1239 | return EOF;
1240 |
1241 | --remaining;
1242 |
1243 | byte pix = pixAry[curPixel++];
1244 |
1245 | return pix & 0xff;
1246 | }
1247 |
1248 | void output(int code, OutputStream outs) throws IOException {
1249 | cur_accum &= masks[cur_bits];
1250 |
1251 | if (cur_bits > 0)
1252 | cur_accum |= (code << cur_bits);
1253 | else
1254 | cur_accum = code;
1255 |
1256 | cur_bits += n_bits;
1257 |
1258 | while (cur_bits >= 8) {
1259 | char_out((byte) (cur_accum & 0xff), outs);
1260 | cur_accum >>= 8;
1261 | cur_bits -= 8;
1262 | }
1263 |
1264 | // If the next entry is going to be too big for the code size,
1265 | // then increase it, if possible.
1266 | if (free_ent > maxcode || clear_flg) {
1267 | if (clear_flg) {
1268 | maxcode = MAXCODE(n_bits = g_init_bits);
1269 | clear_flg = false;
1270 | } else {
1271 | ++n_bits;
1272 | if (n_bits == maxbits)
1273 | maxcode = maxmaxcode;
1274 | else
1275 | maxcode = MAXCODE(n_bits);
1276 | }
1277 | }
1278 |
1279 | if (code == EOFCode) {
1280 | // At EOF, write the rest of the buffer.
1281 | while (cur_bits > 0) {
1282 | char_out((byte) (cur_accum & 0xff), outs);
1283 | cur_accum >>= 8;
1284 | cur_bits -= 8;
1285 | }
1286 |
1287 | flush_char(outs);
1288 | }
1289 | }
1290 | }
1291 |
--------------------------------------------------------------------------------