@Before public void initTest() throws IOException { { URLConnection testTextureUrlConn = IOUtil.getResource(this.getClass(), "test-u32.tga"); Assert.assertNotNull(testTextureUrlConn); testTextureStream01U32 = testTextureUrlConn.getInputStream(); Assert.assertNotNull(testTextureStream01U32); } { URLConnection testTextureUrlConn = IOUtil.getResource(this.getClass(), "bug744-rle32.tga"); Assert.assertNotNull(testTextureUrlConn); testTextureStream02RLE32 = testTextureUrlConn.getInputStream(); Assert.assertNotNull(testTextureStream02RLE32); } }
private Font abspathImpl(final String fname, final int family, final int style) throws IOException { final InputStream stream; if (useTempJARCache) { final Exception[] privErr = {null}; stream = AccessController.doPrivileged( new PrivilegedAction<InputStream>() { @Override public InputStream run() { try { final Uri uri = TempJarCache.getResourceUri(fname); return null != uri ? uri.toURL().openConnection().getInputStream() : null; } catch (final Exception e) { privErr[0] = e; return null; } } }); if (null != privErr[0]) { throw new IOException(privErr[0]); } } else { final URLConnection urlConn = IOUtil.getResource(UbuntuFontLoader.class, fname); stream = null != urlConn ? urlConn.getInputStream() : null; } if (null != stream) { final Font f = FontFactory.get(stream, true); if (null != f) { fontMap.put((family << 8) | style, f); return f; } } return null; }
static void testTempDirImpl(boolean isSecure) { isSecure |= !usesSecurityManager; Exception se0 = null; try { final File tmp = IOUtil.getTempDir(true); System.err.println("Temp: " + tmp); } catch (final AccessControlException e) { se0 = e; if (!isSecure) { System.err.println("Expected exception for insecure temp dir"); System.err.println("Message: " + se0.getMessage()); } else { System.err.println("Unexpected exception for secure temp dir"); se0.printStackTrace(); } } catch (final SecurityException e) { se0 = e; if (!isSecure) { System.err.println("Expected exception for insecure temp dir (2)"); System.err.println("Message: " + se0.getMessage()); } else { System.err.println("Unexpected exception for secure temp dir (2)"); se0.printStackTrace(); } } catch (final IOException e) { throw new RuntimeException(e); // oops } if (isSecure) { Assert.assertNull("AccessControlException thrown on secure temp dir", se0); } else { Assert.assertNotNull("AccessControlException not thrown on insecure temp dir", se0); } }
static URLConnection getResource(String path[], int off) { URLConnection uc = null; for (int i = off; null == uc && i < path.length; i++) { if (null != path[i] && path[i].length() > 0) { uc = IOUtil.getResource(path[i], null); Log.d(TAG, "Stream: <" + path[i] + ">: " + (null != uc)); } } return uc; }
@Override public void init(final GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); glut = new GLUT(); gl.glClearColor(0.5f, 0.5f, 0.5f, 0.0f); try { final URLConnection urlConn = IOUtil.getResource(PNGTstFiles.class, "test-ntscP_3-01-160x90.png"); tex = TextureIO.newTexture( gl, TextureIO.newTextureData( gl.getGLProfile(), urlConn.getInputStream(), false, TextureIO.PNG)); } catch (final Exception e) { e.printStackTrace(); } // tex.bind(gl); // uncomment this and comment the above to see a working texture // makeStripeImage(); // gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); // gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, // GL2.GL_MODULATE); // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_S, // GL.GL_REPEAT); // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, // GL.GL_LINEAR); // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER, // GL.GL_LINEAR); // gl.glTexImage1D(GL2.GL_TEXTURE_1D, 0, 3, stripeImageWidth, 0, // GL.GL_RGB, GL.GL_UNSIGNED_BYTE, stripeImageBuf); // gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT); // gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR); // gl.glTexGenfv(GL2.GL_S, GL2.GL_OBJECT_PLANE, sgenparams, 0); }
@Override protected final void initStreamImpl(final int vid, final int aid) throws IOException { if (0 == moviePtr) { throw new GLException("FFMPEG native instance null"); } if (DEBUG) { System.err.println("initStream: p1 " + this); } final String streamLocS = IOUtil.decodeURIIfFilePath(getURI()); destroyAudioSink(); if (GLMediaPlayer.STREAM_ID_NONE == aid) { audioSink = AudioSinkFactory.createNull(); } else { audioSink = AudioSinkFactory.createDefault(); } final AudioFormat preferredAudioFormat = audioSink.getPreferredFormat(); if (DEBUG) { System.err.println("initStream: p2 preferred " + preferredAudioFormat + ", " + this); } final boolean isCameraInput = null != cameraPath; final String resStreamLocS; // int rw=640, rh=480, rr=15; int rw = -1, rh = -1, rr = -1; String sizes = null; if (isCameraInput) { switch (PlatformPropsImpl.OS_TYPE) { case ANDROID: // ?? case FREEBSD: case HPUX: case LINUX: case SUNOS: resStreamLocS = dev_video_linux + cameraPath; break; case WINDOWS: resStreamLocS = cameraPath; break; case MACOS: case OPENKODE: default: resStreamLocS = streamLocS; // FIXME: ?? break; } if (null != cameraProps) { sizes = cameraProps.get(CameraPropSizeS); int v = getPropIntVal(cameraProps, CameraPropWidth); if (v > 0) { rw = v; } v = getPropIntVal(cameraProps, CameraPropHeight); if (v > 0) { rh = v; } v = getPropIntVal(cameraProps, CameraPropRate); if (v > 0) { rr = v; } } } else { resStreamLocS = streamLocS; } final int aMaxChannelCount = audioSink.getMaxSupportedChannels(); final int aPrefSampleRate = preferredAudioFormat.sampleRate; // setStream(..) issues updateAttributes*(..), and defines avChosenAudioFormat, vid, aid, .. etc if (DEBUG) { System.err.println( "initStream: p3 cameraPath " + cameraPath + ", isCameraInput " + isCameraInput); System.err.println( "initStream: p3 stream " + getURI() + " -> " + streamLocS + " -> " + resStreamLocS); System.err.println( "initStream: p3 vid " + vid + ", sizes " + sizes + ", reqVideo " + rw + "x" + rh + "@" + rr + ", aid " + aid + ", aMaxChannelCount " + aMaxChannelCount + ", aPrefSampleRate " + aPrefSampleRate); } natives.setStream0( moviePtr, resStreamLocS, isCameraInput, vid, sizes, rw, rh, rr, aid, aMaxChannelCount, aPrefSampleRate); }
protected void runOneSet( GLAutoDrawable drawable, String textBaseName, int numObjs, int numTextures, int loops) { GL2ES2 gl = drawable.getGL().getGL2ES2(); if (numTextures > MAX_TEXTURE_ENGINES) { throw new GLException("numTextures must be within 1.." + MAX_TEXTURE_ENGINES); } String textName = null; textDatas = new TextureData[numObjs]; textures = new Texture[numTextures]; try { for (int i = 0; i < numObjs; i++) { textName = "data/" + textBaseName + "." + (i + 1) + ".tga"; URL urlText = IOUtil.getResource(Perftst.class, textName); if (urlText == null) { throw new RuntimeException("couldn't fetch " + textName); } textDatas[i] = TextureIO.newTextureData(gl.getGLProfile(), urlText.openStream(), false, TextureIO.TGA); System.out.println(textBaseName + ": " + textDatas[i]); } for (int i = 0; i < numTextures; i++) { gl.glActiveTexture(i); textures[i] = new Texture(GL.GL_TEXTURE_2D); } } catch (IOException ioe) { System.err.println("couldn't fetch " + textName); throw new RuntimeException(ioe); } // // Vertices Data setup // st.useProgram(gl, true); GLArrayDataServer vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); { FloatBuffer vb = (FloatBuffer) vertices.getBuffer(); vb.put(0f); vb.put(0f); vb.put(1f); vb.put(0f); vb.put(0f); vb.put(1f); vb.put(1f); vb.put(1f); } vertices.seal(gl, true); GLArrayDataServer texCoords = GLArrayDataServer.createGLSL( "mgl_MultiTexCoord0", 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); { FloatBuffer cb = (FloatBuffer) texCoords.getBuffer(); cb.put(0f); cb.put(0f); cb.put(1f); cb.put(0f); cb.put(0f); cb.put(1f); cb.put(1f); cb.put(1f); } texCoords.seal(gl, true); // // texture setup // long[] tU = new long[numObjs + 1]; tU[0] = System.currentTimeMillis(); for (int j = 0; j < numTextures; j++) { gl.glActiveTexture(j); textures[j].updateImage(gl, textDatas[0]); tU[j + 1] = System.currentTimeMillis(); } GLUniformData activeTexture = new GLUniformData("mgl_ActiveTexture", 0); st.uniform(gl, activeTexture); // // run loops // long dtC, dt, dt2, dt3, dtF, dtS, dtT; long[][] tC = new long[loops][numObjs]; long[][] t0 = new long[loops][numObjs]; long[][][] t1 = new long[loops][numObjs][numTextures]; long[][][] t2 = new long[loops][numObjs][numTextures]; long[][][] t3 = new long[loops][numObjs][numTextures]; long[][] tF = new long[loops][numObjs]; long[][] tS = new long[loops][numObjs]; for (int i = 0; i < loops; i++) { for (int j = 0; j < numObjs; j++) { tC[i][j] = System.currentTimeMillis(); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); t0[i][j] = System.currentTimeMillis(); for (int k = 0; k < numTextures; k++) { gl.glActiveTexture(GL.GL_TEXTURE0 + k); textures[k].enable(gl); textures[k].bind(gl); activeTexture.setData(k); st.uniform(gl, activeTexture); t1[i][j][k] = System.currentTimeMillis(); textures[k].updateSubImage(gl, textDatas[j], 0, 0, 0); t2[i][j][k] = System.currentTimeMillis(); gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, vertices.getElementCount()); t3[i][j][k] = System.currentTimeMillis(); } gl.glFinish(); tF[i][j] = System.currentTimeMillis(); drawable.swapBuffers(); tS[i][j] = System.currentTimeMillis(); /*try { Thread.sleep(100); } catch (Exception e) {} */ } } int textBytes = 0; for (int j = 0; j < numObjs; j++) { textBytes += textDatas[j].getEstimatedMemorySize(); } textBytes *= numTextures; dt = 0; for (int i = 1; i < loops; i++) { for (int j = 0; j < numObjs; j++) { dt += tS[i][j] - tC[i][j]; } } System.out.println(""); System.out.println( "Texture " + textBaseName + ", loops " + loops + ", textures " + numTextures + ", objects " + numObjs + ", total bytes " + textBytes + ", total time: " + dt + "ms, fps(-1): " + (((loops - 1) * numObjs * 1000) / dt) + ",\n text kB/s: " + (((double) (loops * textBytes) / 1024.0) / ((double) dt / 1000.0))); for (int i = 0; i < loops; i++) { dtC = 0; dtF = 0; dtS = 0; dtT = 0; for (int j = 0; j < numObjs; j++) { dtC += t0[i][j] - tC[i][j]; dtF += tF[i][j] - t3[i][j][numTextures - 1]; dtS += tS[i][j] - tF[i][j]; dtT += tS[i][j] - tC[i][j]; } if (dtT <= 0) dtT = 1; System.out.println( "\tloop " + i + ": clear " + dtC + "ms, finish " + dtF + ", swap " + dtS + "ms, total: " + dtT + "ms, fps " + (numObjs * 1000) / dtT); /* for(int j=0; j<dummyUni.length; j++) { dt = t1[i][j] - t0[i]; dt2= t2[i][j] - t1[i][j]; dt3= t3[i][j] - t2[i][j]; dtT= dt+dt2+dt3; System.out.println("\t\tobj "+j+": setup "+dt +"ms, update "+dt2 +"ms, draw "+dt3+"ms, total: "+ dtT); } */ } System.out.println("*****************************************************************"); st.useProgram(gl, false); for (int i = 0; i < numTextures; i++) { textures[i].disable(gl); textures[i].destroy(gl); textures[i] = null; } for (int i = 0; i < numObjs; i++) { textDatas[i] = null; } textures = null; textDatas = null; System.gc(); try { Thread.sleep(100); } catch (Exception e) { } System.gc(); }
public void write(File out, boolean allowOverwrite) throws IOException { final ImageInfo imi = new ImageInfo( pixelWidth, pixelHeight, 8, (4 == bytesPerPixel) ? true : false); // 8 bits per channel, no alpha // open image for writing to a output stream final OutputStream outs = new BufferedOutputStream(IOUtil.getFileOutputStream(out, allowOverwrite)); try { final PngWriter png = new PngWriter(outs, imi); // add some optional metadata (chunks) png.getMetadata().setDpi(dpi[0], dpi[1]); png.getMetadata().setTimeNow(0); // 0 seconds fron now = now png.getMetadata().setText(PngChunkTextVar.KEY_Title, "JogAmp PNGImage"); // png.getMetadata().setText("my key", "my text"); final boolean hasAlpha = 4 == bytesPerPixel; final ImageLine l1 = new ImageLine(imi); if (isGLOriented) { // start at last pixel at end-of-buffer, reverse read (OpenGL bottom-left -> PNG top-left // origin) int dataOff = (pixelWidth * bytesPerPixel * (pixelHeight - 1)) + // full lines - 1 line ((pixelWidth - 1) * bytesPerPixel); // one line - 1 pixel for (int row = 0; row < pixelHeight; row++) { int lineOff = (pixelWidth - 1) * bytesPerPixel; // start w/ last pixel in line, reverse store (OpenGL bottom-left // -> PNG top-left origin) if (1 == bytesPerPixel) { for (int j = pixelWidth - 1; j >= 0; j--) { l1.scanline[lineOff--] = data.get(dataOff--); // // Luminance, 1 bytesPerPixel } } else { for (int j = pixelWidth - 1; j >= 0; j--) { dataOff = setPixelRGBA8(l1, lineOff, data, dataOff, hasAlpha); lineOff -= bytesPerPixel; } } png.writeRow(l1, row); } } else { int dataOff = 0; // start at first pixel at start-of-buffer, normal read (same origin: top-left) for (int row = 0; row < pixelHeight; row++) { int lineOff = 0; // start w/ first pixel in line, normal store (same origin: top-left) if (1 == bytesPerPixel) { for (int j = pixelWidth - 1; j >= 0; j--) { l1.scanline[lineOff++] = data.get(dataOff++); // // Luminance, 1 bytesPerPixel } } else { for (int j = pixelWidth - 1; j >= 0; j--) { dataOff = setPixelRGBA8(l1, lineOff, data, dataOff, hasAlpha); lineOff += bytesPerPixel; } } png.writeRow(l1, row); } } png.end(); } finally { IOUtil.close(outs, false); } }
private void writeImage( final File file, byte[] data, final int xsize, final int ysize, final int zsize, final boolean yflip) throws IOException { // Input data is in RGBRGBRGB or RGBARGBARGBA format; first unswizzle it final byte[] tmpData = new byte[xsize * ysize * zsize]; int dest = 0; for (int i = 0; i < zsize; i++) { for (int j = i; j < (xsize * ysize * zsize); j += zsize) { tmpData[dest++] = data[j]; } } data = tmpData; // requires: DATA must be an array of size XSIZE * YSIZE * ZSIZE, // indexed in the following manner: // data[0] ...data[xsize-1] == first row of first channel // data[xsize]...data[2*xsize-1] == second row of first channel // ... data[(ysize - 1) * xsize]...data[(ysize * xsize) - 1] == // last row of first channel // Later channels follow the same format. // *** NOTE that "first row" is defined by the BOTTOM ROW of // the image. That is, the origin is in the lower left corner. // effects: writes out an SGI image to FILE, RLE-compressed, INCLUDING // header, of dimensions (xsize, ysize, zsize), and containing // the data in DATA. If YFLIP is set, outputs the data in DATA // in reverse order vertically (equivalent to a flip about the // x axis). // Build the offset tables final int[] starttab = new int[ysize * zsize]; final int[] lengthtab = new int[ysize * zsize]; // Temporary buffer for holding RLE data. // Note that this makes the assumption that RLE-compressed data will // never exceed twice the size of the input data. // There are surely formal proofs about how big the RLE buffer should // be, as well as what the optimal look-ahead size is (i.e. don't switch // copy/repeat modes for less than N repeats). However, I'm going from // empirical evidence here; the break-even point seems to be a look- // ahead of 3. (That is, if the three values following this one are all // the same as the current value, switch to repeat mode.) final int lookahead = 3; final byte[] rlebuf = new byte[2 * xsize * ysize * zsize]; int cur_loc = 0; // current offset location. int ptr = 0; int total_size = 0; int ystart = 0; int yincr = 1; int yend = ysize; if (yflip) { ystart = ysize - 1; yend = -1; yincr = -1; } final boolean DEBUG = false; for (int z = 0; z < zsize; z++) { for (int y = ystart; y != yend; y += yincr) { // RLE-compress each row. int x = 0; byte count = 0; boolean repeat_mode = false; boolean should_switch = false; final int start_ptr = ptr; int num_ptr = ptr++; byte repeat_val = 0; while (x < xsize) { // see if we should switch modes should_switch = false; if (repeat_mode) { if (imgref(data, x, y, z, xsize, ysize, zsize) != repeat_val) { should_switch = true; } } else { // look ahead to see if we should switch to repeat mode. // stay within the scanline for the lookahead if ((x + lookahead) < xsize) { should_switch = true; for (int i = 1; i <= lookahead; i++) { if (DEBUG) System.err.println( "left side was " + ((int) imgref(data, x, y, z, xsize, ysize, zsize)) + ", right side was " + (int) imgref(data, x + i, y, z, xsize, ysize, zsize)); if (imgref(data, x, y, z, xsize, ysize, zsize) != imgref(data, x + i, y, z, xsize, ysize, zsize)) should_switch = false; } } } if (should_switch || (count == 127)) { // update the number of elements we repeated/copied if (x > 0) { if (repeat_mode) rlebuf[num_ptr] = count; else rlebuf[num_ptr] = (byte) (count | 0x80); } // perform mode switch if necessary; output repeat_val if // switching FROM repeat mode, and set it if switching // TO repeat mode. if (repeat_mode) { if (should_switch) repeat_mode = false; rlebuf[ptr++] = repeat_val; } else { if (should_switch) repeat_mode = true; repeat_val = imgref(data, x, y, z, xsize, ysize, zsize); } if (x > 0) { // reset the number pointer num_ptr = ptr++; // reset number of bytes copied count = 0; } } // if not in repeat mode, copy element to ptr if (!repeat_mode) { rlebuf[ptr++] = imgref(data, x, y, z, xsize, ysize, zsize); } count++; if (x == xsize - 1) { // Need to store the number of pixels we copied/repeated. if (repeat_mode) { rlebuf[num_ptr] = count; // If we ended the row in repeat mode, store the // repeated value rlebuf[ptr++] = repeat_val; } else rlebuf[num_ptr] = (byte) (count | 0x80); // output zero counter for the last value in the row rlebuf[ptr++] = 0; } x++; } // output this row's length into the length table final int rowlen = ptr - start_ptr; if (yflip) lengthtab[ysize * z + (ysize - y - 1)] = rowlen; else lengthtab[ysize * z + y] = rowlen; // add to the start table, and update the current offset if (yflip) starttab[ysize * z + (ysize - y - 1)] = cur_loc; else starttab[ysize * z + y] = cur_loc; cur_loc += rowlen; } } // Now we have the offset tables computed, as well as the RLE data. // Output this information to the file. total_size = ptr; if (DEBUG) System.err.println("total_size was " + total_size); final DataOutputStream stream = new DataOutputStream(new BufferedOutputStream(IOUtil.getFileOutputStream(file, true))); writeHeader(stream, xsize, ysize, zsize, true); final int SIZEOF_INT = 4; for (int i = 0; i < (ysize * zsize); i++) stream.writeInt(starttab[i] + 512 + (2 * ysize * zsize * SIZEOF_INT)); for (int i = 0; i < (ysize * zsize); i++) stream.writeInt(lengthtab[i]); for (int i = 0; i < total_size; i++) stream.write(rlebuf[i]); stream.close(); }