// images must have same size static FloatBWImage preciseAverageOfBinaryImages(L images) { int n = l(images); if (n == 0) null; var it = iterator(images); var img = it.next(); int w = img.getWidth(), h = img.getHeight(); int[] pixels = new[w*h]; int nBytes = (w*h+7)/8; var pingSource = pingSource(); while (true) { ping(pingSource); Image2B realizedImage = img.toImage2B(); byte[] imgPixels = realizedImage.pixels; int j = 0; for i to nBytes: { int b = imgPixels[i]; for bit to 8: { ifdef preciseAverageOfBinaryImages_useIf // This version is half as fast (don't set this flag) if ((b & 1) != 0) pixels[i*8+bit]++; endifdef ifndef preciseAverageOfBinaryImages_useIf pixels[j++] += b & 1; endifndef b >>= 1; } } if (!it.hasNext()) break; img = it.next(); } float[] floats = new[w*h]; double factor = 1.0/n; for i over pixels: floats[i] = (float) (pixels[i]*factor); ret new FloatBWImage(w, h, floats); }