public static int PDJPG_EncodeComponentCtxI2( PDJPG_Context ctx, byte[] rgba, byte[] norm, byte[] spec, byte[] luma, byte[] lebe, BytePtr obuf, int xs, int ys, int qf, string tlname, PDJPG_LayerInfo linf) { byte[] tbuf; BytePtr ct; int i, j, k, n; if (rgba == null) return (-1); if (linf != null) { ctx.bcst_orgx = linf.orgx; ctx.bcst_orgy = linf.orgy; ctx.bcst_alpha = linf.alpha; ctx.bcst_blend = linf.blend; ctx.bcst_flags = (uint)linf.flags; ctx.bcst_minx = linf.minx; ctx.bcst_miny = linf.miny; ctx.bcst_maxx = linf.maxx; ctx.bcst_maxy = linf.maxy; } else { ctx.bcst_orgx = 0; ctx.bcst_orgy = 0; ctx.bcst_alpha = 255; ctx.bcst_blend = 0; ctx.bcst_flags = 0; ctx.bcst_minx = 0; ctx.bcst_miny = 0; ctx.bcst_maxx = 0; ctx.bcst_maxy = 0; } n = xs * ys; tbuf = new byte[xs * ys * 4]; ct = obuf; //ct.emit(0xFF); //ct.emit(JPG.JPG_SOI); //ct = PDJPG_EmitMarkerJFIF(ctx, ct); if (tlname != null) { ct = PDJPG_EmitTagLayer(ctx, ct, tlname); ct = PDJPG_EmitComponentLayer(ctx, ct, "RGB"); i = PDJPG_EncodeLDatCtx(ctx, rgba, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } else { ct = PDJPG_EmitComponentLayer(ctx, ct, "RGB"); // i=PDJPG_EncodeCtx(ctx, rgba, ct, xs, ys, qf, 0); i = PDJPG_EncodeBaseCtx(ctx, rgba, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } // if((ct[-2]==0xFF) && (ct[-2]==JPG_EOI)) // { ct-=2; } if (norm != null) { for (i = 0; i < n; i++) { tbuf[i * 4 + 0] = norm[i * 4 + 0]; tbuf[i * 4 + 1] = norm[i * 4 + 2]; tbuf[i * 4 + 2] = norm[i * 4 + 1]; tbuf[i * 4 + 3] = 255; } ct = PDJPG_EmitComponentLayer(ctx, ct, "XYZ"); i = PDJPG_EncodeLDatCtx(ctx, tbuf, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } if (spec != null) { ct = PDJPG_EmitComponentLayer(ctx, ct, "SpRGB"); i = PDJPG_EncodeLDatCtx(ctx, spec, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } if ((norm != null) || (spec != null)) { for (i = 0; i < n; i++) { j = rgba[i * 4 + 3]; tbuf[i * 4 + 0] = (byte)((norm != null) ? norm[i * 4 + 3] : j); tbuf[i * 4 + 1] = rgba[i * 4 + 3]; tbuf[i * 4 + 2] = (byte)((spec != null) ? spec[i * 4 + 3] : j); tbuf[i * 4 + 3] = 255; } ct = PDJPG_EmitComponentLayer(ctx, ct, "DASe"); i = PDJPG_EncodeLDatCtx(ctx, tbuf, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } else { k = 0; for (i = 0; i < n; i++) { j = rgba[i * 4 + 3]; if (j != 255) k = 1; tbuf[i * 4 + 0] = (byte)j; tbuf[i * 4 + 1] = (byte)j; tbuf[i * 4 + 2] = (byte)j; tbuf[i * 4 + 3] = 255; } if (k != 0) { ct = PDJPG_EmitComponentLayer(ctx, ct, "Alpha"); i = PDJPG_EncodeLDatCtx(ctx, tbuf, ct, xs, ys, qf, PDJPG_YYYA); if (i < 0) { return (i); } ct += i; } } if (luma != null) { ct = PDJPG_EmitComponentLayer(ctx, ct, "LuRGB"); i = PDJPG_EncodeLDatCtx(ctx, luma, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } if (lebe != null) { ct = PDJPG_EmitComponentLayer(ctx, ct, "LeBe"); i = PDJPG_EncodeLDatCtx(ctx, lebe, ct, xs, ys, qf, 0); if (i < 0) { return (i); } ct += i; } //ct.emit(0xFF); //ct.emit(JPG.JPG_EOI); //free(tbuf); return (ct - obuf); }
public static int PDJPG_DecodeComponent(BytePtr buf, int sz, byte[] rgba, byte[] norm, byte[] spec, byte[] luma, out int xs, out int ys, out PDJPG_LayerInfo info) { PDJPG_Context ctx; int i; ctx = PDJPG_AllocContext(); i = PDJPG_DecodeComponentCtx(ctx, buf, sz, rgba, norm, spec, luma, out xs, out ys); // ctx.jpg_imgbuf=null; info = new PDJPG_LayerInfo(); info.flags = (int)ctx.bcst_flags; info.alpha = ctx.bcst_alpha; info.blend = ctx.bcst_blend; info.orgx = ctx.bcst_orgx; info.orgy = ctx.bcst_orgy; PDJPG_FreeContext(ctx); return (i); }