16 cWindow = eWindow = sWindow = 0;
22 codes = malloc( 4*1024 );
36 - encodeQuietly:(
BOOL)which
54 return [paramFileId isScaleable];
59 return [paramFileId noAlpha];
69 return [paramFileId searchType];
74 return [paramFileId hasSound];
77 - (
int)previousFrameSize
84 return [paramFileId firstFrameSize];
89 return [paramFileId normalFrameSize];
92 - (
char *)currentFilename
97 -encodeStream: (
id)paramInputFile
100 char f0[MAXPATHLEN], f1[MAXPATHLEN], f2[MAXPATHLEN];
107 paramFileId = paramInputFile;
109 if ([paramInputFile noAlpha]==YES) printf(
"encodeStream: eluding alpha\n");
112 strcpy( f1, [paramInputFile getNextImageFilename]);
113 if (( [paramInputFile moreFrames] == YES )) strcpy( f2, [paramInputFile getNextImageFilename]);
117 [
self loadAndDisplayImage: f1];
119 if (onFrame==1 && ([
image hadAlpha]==NO || [paramInputFile noAlpha]==YES) && ![
self makingVideo] && ![
self scaleable]) {
120 [encoder sparseEncode: self];
124 [
self writeHangFrame];
126 [encoder sparseEncode: self];
133 if ([paramInputFile moreFrames] == YES) strcpy( f2, [paramInputFile getNextImageFilename]);
138 if ([
image hadAlpha] && [paramInputFile noAlpha]==NO) {
140 [encoder sparseEncode: self];
142 [
self writeLossless];
148 - write16Word:(
word *)aWord to:(FILE *)stream
161 - write32Word:(
unsigned int *)aWord to:(FILE *)stream
166 b = (*aWord >> 8) & 0xff;
167 c = (*aWord >> 16) & 0xff;
168 d = (*aWord >> 24) & 0xff;
177 -(
int)sizeFile:(FILE *)ftosize;
180 fat = ftell(ftosize);
182 fend = ftell(ftosize);
187 - convertPlanertoPacked
189 byte *iPlane[5], *newdata, *olddata;
190 int x,
y,
index, sample, pixelsWide, pixelsHigh;
191 TByteBitmapImageRep *newImage;
196 printf(
"convertPlanertoPacked: converting\n");
198 newImage = [[TByteBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
199 pixelsWide: pixelsWide
200 pixelsHigh: pixelsHigh
205 colorSpaceName: NSCalibratedRGBColorSpace
209 newdata = [newImage bitmapData];
212 if ([
image isPlanar]) {
214 for(y=0;y<pixelsHigh;y++) {
215 for(x=0;x<pixelsWide;x++) {
216 newdata[index++] = iPlane[0][y*pixelsWide+x];
217 newdata[index++] = iPlane[1][y*pixelsWide+x];
218 newdata[index++] = iPlane[2][y*pixelsWide+x];
219 if ([
image hasAlpha]) {
220 newdata[index++] = iPlane[3][y*pixelsWide+x];
222 newdata[index++] = 255;
229 for(y=0;y<pixelsHigh;y++) {
230 for(x=0;x<pixelsWide;x++) {
231 newdata[index++] = olddata[sample++];
232 newdata[index++] = olddata[sample++];
233 newdata[index++] = olddata[sample++];
234 if ([
image hasAlpha]) {
235 newdata[index++] = olddata[sample++];
237 newdata[index++] = 255;
254 char tempFile[MAXPATHLEN];
259 [
self convertPlanertoPacked];
261 [
self initRoQPatterns];
265 [
self write16Word: &direct to: RoQFile];
266 sprintf(tempFile,
"%s.jpg",[paramFileId roqTempFilename]);
270 ftemp = fopen(tempFile,
"rb");
271 if (!ftemp) { fprintf(stderr,
"Could not open temp file\n"); exit(1); }
272 j = [
self sizeFile: ftemp];
273 printf(
"writeLossless: writing %d bytes to RoQ_QUAD_JPEG\n", j);
274 [
self write32Word: &j to: RoQFile];
276 [
self write16Word: &direct to: RoQFile];
278 buffer = malloc( 16384 );
280 res =
fread( buffer, 1, 16384, ftemp);
282 if (res != mess) { fprintf(stderr,
"Could not write to output stream\n"); exit(1); }
283 }
while ( res == 16384 );
286 [encoder setPreviousImage: tempFile from: image parent: self];
293 - initRoQFile:(
const char *)RoQFilename
296 static int finit = 0;
300 printf(
"initRoQFile: %s\n", RoQFilename);
301 RoQFile = fopen( RoQFilename,
"w" );
304 fprintf(stderr,
"Unable to open output file %s.\n", RoQFilename);
309 [
self write16Word: &i to: RoQFile];
312 [
self write16Word: &i to: RoQFile];
313 [
self write16Word: &i to: RoQFile];
316 [
self write16Word: &i to: RoQFile];
330 [
self write16Word: &direct to: RoQFile];
334 [
self write32Word: &j to: RoQFile];
335 printf(
"initRoQPatterns: outputting %d bytes to RoQ_INFO\n", j);
337 if ([
self paramNoAlpha] == YES) direct = 0;
339 [
self write16Word: &direct to: RoQFile];
342 [
self write16Word: &direct to: RoQFile];
344 [
self write16Word: &direct to: RoQFile];
346 [
self write16Word: &direct to: RoQFile];
348 [
self write16Word: &direct to: RoQFile];
358 printf(
"closeRoQFile: closing RoQ file\n");
368 printf(
"*******************************************************************\n");
370 [
self write16Word: &direct to: RoQFile];
372 [
self write32Word: &j to: RoQFile];
374 [
self write16Word: &direct to: RoQFile];
378 - writeCodeBookToStream: (
byte *)codebook size: (
int)csize flags: (
word)cflags
384 printf(
"writeCodeBook: NO VQ DATA!!!!\n");
390 [
self write16Word: &direct to: RoQFile];
394 [
self write32Word: &j to: RoQFile];
395 printf(
"writeCodeBook: outputting %d bytes to RoQ_QUAD_CODEBOOK\n", j);
398 [
self write16Word: &direct to: RoQFile];
407 - writeCodeBook: (
byte *)codebook
409 memcpy(
codes, codebook, 4096 );
416 int onCCC, onAction,
i,
j, code;
419 int dx,dy,dxMean,dyMean,index2[256],index4[256], dimension;
422 use2 = malloc(256*
sizeof(
BOOL));
423 use4 = malloc(256*
sizeof(
BOOL));
434 dxMean = [encoder motMeanX];
435 dyMean = [encoder motMeanY];
437 if ([
image hadAlpha]) dimension = 10;
else dimension = 6;
440 if ( pquad[i].
size && pquad[i].
size < 16 ) {
441 switch( pquad[i].status ) {
443 use4[pquad[i].patten[0]] = YES;
444 use2[codes[dimension*256+(pquad[i].patten[0]*4)+0]] = YES;
445 use2[codes[dimension*256+(pquad[i].patten[0]*4)+1]] = YES;
446 use2[codes[dimension*256+(pquad[i].patten[0]*4)+2]] = YES;
447 use2[codes[dimension*256+(pquad[i].patten[0]*4)+3]] = YES;
450 use4[pquad[i].patten[0]] = YES;
451 use2[codes[dimension*256+(pquad[i].patten[0]*4)+0]] = YES;
452 use2[codes[dimension*256+(pquad[i].patten[0]*4)+1]] = YES;
453 use2[codes[dimension*256+(pquad[i].patten[0]*4)+2]] = YES;
454 use2[codes[dimension*256+(pquad[i].patten[0]*4)+3]] = YES;
457 use2[pquad[i].patten[1]] = YES;
458 use2[pquad[i].patten[2]] = YES;
459 use2[pquad[i].patten[3]] = YES;
460 use2[pquad[i].patten[4]] = YES;
467 [
self initRoQPatterns];
468 if ([
image hadAlpha]) i = 3584;
else i = 2560;
469 [
self writeCodeBookToStream: codes size: i flags: 0];
479 for(dx=0;dx<dimension;dx++) cccList[j*dimension+dx] =
codes[i*dimension+dx];
485 printf(
"writeFrame: really used %d 2x2 cels\n", j);
490 for(dx=0;dx<4;dx++) cccList[j*4+code+dx] = index2[
codes[i*4+(dimension*256)+dx]];
495 direct = (direct<<8) + j;
496 printf(
"writeFrame: really used %d 4x4 cels\n", j);
497 if ([
image hadAlpha]) i = 3584;
else i = 2560;
498 if ( code == i || j == 256) {
499 [
self writeCodeBookToStream: codes size: i flags: 0];
501 [
self writeCodeBookToStream: cccList size: code flags: direct];
509 if ( pquad[i].
size && pquad[i].
size < 16 ) {
511 switch( pquad[i].status ) {
517 cccList[onCCC++] = index4[pquad[i].patten[0]];
524 dx = ((pquad[i].
domain >> 8 )) - 128 - dxMean + 8;
525 dy = ((pquad[i].
domain & 0xff)) - 128 - dyMean + 8;
526 if (dx>15 || dx<0 || dy>15 || dy<0 ) {
527 printf(
"writeFrame: FCC error %d,%d mean %d,%d at %d,%d,%d rmse %f\n", dx,dy, dxMean, dyMean,pquad[i].xat,pquad[i].yat,pquad[i].
size, pquad[i].snr[
FCC] );
530 cccList[onCCC++] = (dx<<4)+dy;
534 cccList[onCCC++] = index4[pquad[i].patten[0]];
538 cccList[onCCC++] = index2[pquad[i].patten[1]];
539 cccList[onCCC++] = index2[pquad[i].patten[2]];
540 cccList[onCCC++] = index2[pquad[i].patten[3]];
541 cccList[onCCC++] = index2[pquad[i].patten[4]];
544 fprintf(stderr,
"dead cels in picture\n");
548 fprintf(stderr,
"writeFrame: an error occurred writing the frame\n");
552 action = (action<<2)|code;
556 cccList[onAction+0] = (action & 0xff);
557 cccList[onAction+1] = ((action >> 8) & 0xff);
565 action <<= ((8-j)*2);
566 cccList[onAction+0] = (action & 0xff);
567 cccList[onAction+1] = ((action >> 8) & 0xff);
572 [
self write16Word: &direct to: RoQFile];
575 [
self write32Word: &j to: RoQFile];
579 direct += (dxMean<<8);
581 [
self write16Word: &direct to: RoQFile];
583 printf(
"writeFrame: outputting %d bytes to RoQ_QUAD_VQ\n", j);
598 - writePuzzleFrame:(
quadcel *)pquad
603 - (TByteBitmapImageRep *)scaleImage: (TByteBitmapImageRep *)toscale
605 int newx, newy,
x,
y,
s,linesize;
606 TByteBitmapImageRep *newImage;
607 unsigned char *i0, *
i1;
612 if ([toscale pixelsHigh] == 160) {
615 newy = [toscale pixelsHigh];
617 newImage = [[TByteBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
624 colorSpaceName: NSCalibratedRGBColorSpace
628 i0 = [toscale bitmapData];
629 i1 = [newImage bitmapData];
630 linesize = [toscale pixelsWide]*4;
632 for(y=0; y<newy; y++) {
633 for(x=0; x<320; x++) {
634 if (x>=16 && x<304) {
636 if ([toscale pixelsHigh] == 160) {
637 newv = i0[(x*2+0)*4+(y>>1)*linesize+s];
638 newv += i0[(x*2+1)*4+(y>>1)*linesize+s];
639 newv += i0[(x*2+0)*4+((y>>1)+1)*linesize+s];
640 newv += i0[(x*2+1)*4+((y>>1)+1)*linesize+s];
643 newv = i0[(x*2+0)*4+y*linesize+s];
644 newv += i0[(x*2+1)*4+y*linesize+s];
647 i1[(x-16)*4+y*(288*4)+s] = newv;
658 - loadAndDisplayImage: (
const char *) filename
661 char *secondFilename,firstFilename[MAXPATHLEN+1];
669 printf(
"loadAndDisplayImage: %s\n", filename);
673 image = [TByteBitmapImageRep alloc];
674 if (!(secondFilename= strchr(filename,
'\n'))) {
678 strncpy(firstFilename,filename,secondFilename-filename);
679 firstFilename[secondFilename-filename]=
'\0';
681 image1 = [[TByteBitmapImageRep alloc] initFromFile:firstFilename];
684 [
self composite:image1 to:image];
690 newSize.height = 256;
694 if ([paramFileId output3DO] == YES) {
695 image1 = [
self scaleImage: image];
701 numQuadCels += numQuadCels/4 + numQuadCels/16;
709 if (!
quietMode) printf(
"loadAndDisplayImage: %dx%d\n", [
image pixelsWide], [
image pixelsHigh]);
713 cRect.origin.x = 8.0 * 48.0;
717 cWindow = [[NSWindow alloc] initWithContentRect:cRect styleMask:NSTitledWindowMask
718 backing:NSBackingStoreBuffered defer:NO];
719 cRect.origin.x = cRect.origin.y = 0.0;
721 [cWindow setTitle: @"current frame"];
722 [cWindow makeKeyAndOrderFront: [cWindow contentView]];
727 cRect = [[cWindow contentView] bounds];
728 [[cWindow contentView] lockFocus];
730 [[cWindow contentView] unlockFocus];
731 [cWindow flushWindow];
734 cRect.origin.x = 8.0 * 48.0 - ([
image pixelsWide]>>1) - 4;
738 eWindow = [[NSWindow alloc] initWithContentRect:cRect styleMask:NSTitledWindowMask
739 backing:NSBackingStoreBuffered defer:NO];
740 cRect.origin.x = cRect.origin.y = 0.0;
742 [eWindow setTitle: @"cel error"];
743 [eWindow makeKeyAndOrderFront: [eWindow contentView]];
748 cRect.origin.x = 8.0 * 48.0 - ([
image pixelsWide]>>1) - 4;
749 cRect.origin.y = ([image
pixelsHigh]+80) + (([
image pixelsHigh]+48)>>1);
752 sWindow = [[NSWindow alloc] initWithContentRect: cRect styleMask:NSTitledWindowMask
753 backing:NSBackingStoreBuffered defer:NO];
754 cRect.origin.x = cRect.origin.y = 0.0;
756 [sWindow setTitle: @"quadtree map"];
757 [sWindow makeKeyAndOrderFront: [sWindow contentView]];
761 cRect = [[sWindow contentView] bounds];
762 [[sWindow contentView] lockFocus];
764 [[sWindow contentView] unlockFocus];
765 [sWindow flushWindow];
767 cRect = [[eWindow contentView] bounds];
768 [[eWindow contentView] lockFocus];
770 [[eWindow contentView] unlockFocus];
771 [eWindow flushWindow];
781 colorSpaceName: NSCalibratedRGBColorSpace
793 - markQuadx: (
int)xat quady: (
int)yat quads: (
int)size error: (
float)cerror type: (
int)choice
797 static int ywasat = -1;
799 cRect.origin.x = (xat)>>1;
800 cRect.origin.y = ([image
pixelsHigh] - yat - size)>>1;
801 cRect.size.width = cRect.size.height = (
size)>>1;
804 [[sWindow contentView] lockFocus];
805 if (size == 8 && choice == 1) {
807 }
else if (size == 8 && choice == 3) {
808 PSsetgray(NSLightGray);
809 }
else if (size == 4) {
810 PSsetgray(NSDarkGray);
811 }
else if (size == 2) {
814 NSFrameRectWithWidth(cRect,0.0);
815 [[sWindow contentView] unlockFocus];
816 if (!(ywasat & 31)) {
817 [sWindow flushWindow];
821 err = [errImage bitmapData];
822 err[0] = err[1] = err[2] = 0;
823 if ( cerror > 31 ) cerror = 31;
824 if (choice & 1) err[0] = (
int)cerror*8;
825 if (choice & 2) err[1] = (
int)cerror*8;
826 if (choice & 4) err[2] = (
int)cerror*8;
828 [[eWindow contentView] lockFocus];
829 [errImage drawInRect: cRect];
830 [[eWindow contentView] unlockFocus];
831 if (!(ywasat & 31)) {
832 [eWindow flushWindow];
860 unsigned short value;
861 int x,
y,bpp,inc,pixelsWide,pixelsHigh,yoff,pixel;
862 byte *iPlane[5], *dPlane[5];
868 if ([source isPlanar]) {
869 [source getBitmapDataPlanes: iPlane];
870 [destination getBitmapDataPlanes: dPlane];
871 for(y=0;y<pixelsHigh;y++) {
873 for(x=0;x<pixelsWide;x++) {
874 if ([destination hasAlpha]) {
875 value = dPlane[3][yoff+x];
880 dPlane[0][yoff+x] = iPlane[0][yoff+x];
881 dPlane[1][yoff+x] = iPlane[1][yoff+x];
882 dPlane[2][yoff+x] = iPlane[2][yoff+x];
883 dPlane[3][yoff+x] = iPlane[3][yoff+x];
884 }
else if (value != 255) {
885 pixel = ((iPlane[0][yoff+x]*(255-value))/255) + ((dPlane[0][yoff+x]*value)/255);
886 dPlane[0][yoff+x] = pixel;
887 pixel = ((iPlane[1][yoff+x]*(255-value))/255) + ((dPlane[1][yoff+x]*value)/255);
888 dPlane[1][yoff+x] = pixel;
889 pixel = ((iPlane[2][yoff+x]*(255-value))/255) + ((dPlane[2][yoff+x]*value)/255);
890 dPlane[2][yoff+x] = pixel;
891 if ([destination hasAlpha]) {
892 if (iPlane[3][yoff+x]>dPlane[3][yoff+x]) dPlane[3][yoff+x] = iPlane[3][yoff+x];
900 for(y=0;y<pixelsHigh;y++) {
901 yoff = y*pixelsWide*bpp;
902 for(x=0;x<pixelsWide;x++) {
904 if ([destination hasAlpha]) {
905 value = dPlane[0][yoff+inc+3];
910 dPlane[0][yoff+inc+0] = iPlane[0][yoff+inc+0];
911 dPlane[0][yoff+inc+1] = iPlane[0][yoff+inc+1];
912 dPlane[0][yoff+inc+2] = iPlane[0][yoff+inc+2];
913 dPlane[0][yoff+inc+3] = iPlane[0][yoff+inc+3];
914 }
else if (value != 255) {
915 pixel = ((iPlane[0][yoff+inc+0]*(255-value))/255) + ((dPlane[0][yoff+inc+0]*value)/255);
916 dPlane[0][yoff+inc+0] = pixel;
917 pixel = ((iPlane[0][yoff+inc+1]*(255-value))/255) + ((dPlane[0][yoff+inc+1]*value)/255);
918 dPlane[0][yoff+inc+1] = pixel;
919 pixel = ((iPlane[0][yoff+inc+2]*(255-value))/255) + ((dPlane[0][yoff+inc+2]*value)/255);
920 dPlane[0][yoff+inc+2] = pixel;
921 if ([destination hasAlpha]) {
922 if (iPlane[0][yoff+inc+3]>dPlane[0][yoff+inc+3]) dPlane[0][yoff+inc+3] = iPlane[0][yoff+inc+3];
GLsizei const GLfloat * value
GLenum GLsizei GLenum GLenum const GLvoid * image
#define RoQ_QUAD_CODEBOOK
GLubyte GLubyte GLubyte a
static WindowRef ValidModeCallbackProc inCallback OSStatus err
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
int samplesPerPixel(void)
int sprintf(idStr &string, const char *fmt,...)