doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
roq.m
Go to the documentation of this file.
1 
2 #import "roq.h"
3 #import "codec.h"
4 
5 #ifdef __MACOS__
6 
7 blah
8 
9 #endif
10 
11 @implementation roq
12 
13 
14 - init
15 {
16  cWindow = eWindow = sWindow = 0;
17  image = 0;
18  quietMode = NO;
19  encoder = 0;
20  previousSize = 0;
21  lastFrame = NO;
22  codes = malloc( 4*1024 );
23  dataStuff=NO;
24  return self;
25 }
26 
27 - (void)dealloc
28 {
29  free( codes );
30 
31  if (image) [image dealloc];
32  if (encoder) [encoder dealloc];
33  return;
34 }
35 
36 - encodeQuietly:(BOOL)which
37 {
38  quietMode = which;
39  return self;
40 }
41 
42 - (BOOL)isQuiet
43 {
44  return quietMode;
45 }
46 
47 - (BOOL)isLastFrame
48 {
49  return lastFrame;
50 }
51 
52 - (BOOL)scaleable
53 {
54  return [paramFileId isScaleable];
55 }
56 
57 - (BOOL)paramNoAlpha
58 {
59  return [paramFileId noAlpha];
60 }
61 
62 - (BOOL)makingVideo
63 {
64  return YES; //[paramFileId timecode];
65 }
66 
67 - (BOOL)searchType
68 {
69  return [paramFileId searchType];
70 }
71 
72 - (BOOL)hasSound
73 {
74  return [paramFileId hasSound];
75 }
76 
77 - (int)previousFrameSize
78 {
79  return previousSize;
80 }
81 
82 -(int)firstFrameSize
83 {
84  return [paramFileId firstFrameSize];
85 }
86 
87 -(int)normalFrameSize
88 {
89  return [paramFileId normalFrameSize];
90 }
91 
92 - (char *)currentFilename
93 {
94  return currentFile;
95 }
96 
97 -encodeStream: (id)paramInputFile
98 {
99 int onFrame;
100 char f0[MAXPATHLEN], f1[MAXPATHLEN], f2[MAXPATHLEN];
101 int morestuff;
102 
103  onFrame = 1;
104 
105  encoder = [[codec alloc] init: self];
106  numberOfFrames = [paramInputFile numberOfFrames];
107  paramFileId = paramInputFile;
108 
109  if ([paramInputFile noAlpha]==YES) printf("encodeStream: eluding alpha\n");
110 
111  f0[0] = 0;
112  strcpy( f1, [paramInputFile getNextImageFilename]);
113  if (( [paramInputFile moreFrames] == YES )) strcpy( f2, [paramInputFile getNextImageFilename]);
114  morestuff = numberOfFrames;
115 
116  while( morestuff ) {
117  [self loadAndDisplayImage: f1];
118 
119  if (onFrame==1 && ([image hadAlpha]==NO || [paramInputFile noAlpha]==YES) && ![self makingVideo] && ![self scaleable]) {
120  [encoder sparseEncode: self];
121 // [self writeLossless];
122  } else {
123  if (!strcmp( f0, f1 ) && strcmp( f1, f2) ) {
124  [self writeHangFrame];
125  } else {
126  [encoder sparseEncode: self];
127  }
128  }
129 
130  onFrame++;
131  strcpy( f0, f1 );
132  strcpy( f1, f2 );
133  if ([paramInputFile moreFrames] == YES) strcpy( f2, [paramInputFile getNextImageFilename]);
134  morestuff--;
135  }
136 
137  if (numberOfFrames != 1) {
138  if ([image hadAlpha] && [paramInputFile noAlpha]==NO) {
139  lastFrame = YES;
140  [encoder sparseEncode: self];
141  } else {
142  [self writeLossless];
143  }
144  }
145  return self;
146 }
147 
148 - write16Word:(word *)aWord to:(FILE *)stream
149 {
150  byte a, b;
151 
152  a = *aWord & 0xff;
153  b = *aWord >> 8;
154 
155  fputc( a, stream );
156  fputc( b, stream );
157 
158  return self;
159 }
160 
161 - write32Word:( unsigned int *)aWord to:(FILE *)stream
162 {
163  byte a, b, c, d;
164 
165  a = *aWord & 0xff;
166  b = (*aWord >> 8) & 0xff;
167  c = (*aWord >> 16) & 0xff;
168  d = (*aWord >> 24) & 0xff;
169 
170  fputc( a, stream );
171  fputc( b, stream );
172  fputc( c, stream );
173  fputc( d, stream );
174  return self;
175 }
176 
177 -(int)sizeFile:(FILE *)ftosize;
178 {
179  long int fat, fend;
180  fat = ftell(ftosize);
181  fseek( ftosize, 0, SEEK_END );
182  fend = ftell(ftosize);
183  fseek( ftosize, fat, SEEK_SET);
184  return (fend);
185 }
186 
187 - convertPlanertoPacked
188 {
189 byte *iPlane[5], *newdata, *olddata;
190 int x,y,index, sample, pixelsWide, pixelsHigh;
191 TByteBitmapImageRep *newImage;
192 
193  pixelsWide = [image pixelsWide];
194  pixelsHigh = [image pixelsHigh];
195 
196  printf("convertPlanertoPacked: converting\n");
197 
198  newImage = [[TByteBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
199  pixelsWide: pixelsWide
200  pixelsHigh: pixelsHigh
201  bitsPerSample: 8
202  samplesPerPixel: 4
203  hasAlpha: YES
204  isPlanar: NO
205  colorSpaceName: NSCalibratedRGBColorSpace
206  bytesPerRow: 0
207  bitsPerPixel: 0];
208 
209  newdata = [newImage bitmapData];
210  index = 0;
211 
212  if ([image isPlanar]) {
213  [image getBitmapDataPlanes: iPlane];
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];
221  } else {
222  newdata[index++] = 255;
223  }
224  }
225  }
226  } else {
227  sample = 0;
228  olddata = [image bitmapData];
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++];
236  } else {
237  newdata[index++] = 255;
238  }
239  }
240  }
241  }
242 
243  [image dealloc];
244  image = newImage;
245 
246  return self;
247 }
248 
249 
250 - writeLossless
251 {
252 word direct;
253 unsigned int j;
254 char tempFile[MAXPATHLEN];
255 FILE *ftemp;
256 byte *buffer;
257 int res, mess;
258 
259  [self convertPlanertoPacked];
260  if (!dataStuff) {
261  [self initRoQPatterns];
262  dataStuff=YES;
263  }
264  direct = RoQ_QUAD_JPEG;
265  [self write16Word: &direct to: RoQFile];
266  sprintf(tempFile, "%s.jpg",[paramFileId roqTempFilename]);
267 
268  [image writeJFIF:tempFile quality: [paramFileId jpegQuality]];
269 
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];
275  direct = 0; // flags
276  [self write16Word: &direct to: RoQFile];
277 
278  buffer = malloc( 16384 );
279  do {
280  res = fread( buffer, 1, 16384, ftemp);
281  mess = fwrite( buffer, 1, res, RoQFile );
282  if (res != mess) { fprintf(stderr, "Could not write to output stream\n"); exit(1); }
283  } while ( res == 16384 );
284  free( buffer );
285  fclose(ftemp);
286  [encoder setPreviousImage: tempFile from: image parent: self];
287  remove( tempFile );
288  fflush( RoQFile );
289 
290  return self;
291 }
292 
293 - initRoQFile:(const char *)RoQFilename
294 {
295 word i;
296 static int finit = 0;
297 
298  if (!finit) {
299  finit++;
300  printf("initRoQFile: %s\n", RoQFilename);
301  RoQFile = fopen( RoQFilename, "w" );
302 // chmod(RoQFilename, S_IREAD|S_IWRITE|S_ISUID|S_ISGID|0070|0007 );
303  if (!RoQFile) {
304  fprintf(stderr,"Unable to open output file %s.\n", RoQFilename);
305  exit(1);
306  }
307 
308  i = RoQ_ID;
309  [self write16Word: &i to: RoQFile];
310 
311  i = 0xffff;
312  [self write16Word: &i to: RoQFile];
313  [self write16Word: &i to: RoQFile];
314 
315  i = 24; // framerate
316  [self write16Word: &i to: RoQFile];
317  fflush( RoQFile );
318  }
319  strcpy( roqOutfile, RoQFilename );
320 
321  return self;
322 }
323 
324 - initRoQPatterns
325 {
326 int j;
327 word direct;
328 
329  direct = RoQ_QUAD_INFO;
330  [self write16Word: &direct to: RoQFile];
331 
332  j = 8;
333 
334  [self write32Word: &j to: RoQFile];
335  printf("initRoQPatterns: outputting %d bytes to RoQ_INFO\n", j);
336  direct = [image hadAlpha];
337  if ([self paramNoAlpha] == YES) direct = 0;
338 
339  [self write16Word: &direct to: RoQFile];
340 
341  direct = [image pixelsWide];
342  [self write16Word: &direct to: RoQFile];
343  direct = [image pixelsHigh];
344  [self write16Word: &direct to: RoQFile];
345  direct = 8;
346  [self write16Word: &direct to: RoQFile];
347  direct = 4;
348  [self write16Word: &direct to: RoQFile];
349 
350  fflush( RoQFile );
351 
352  return self;
353 }
354 
355 - closeRoQFile
356 {
357  fflush( RoQFile );
358  printf("closeRoQFile: closing RoQ file\n");
359  fclose( RoQFile );
360 
361  return self;
362 }
363 
364 - writeHangFrame
365 {
366 int j;
367 word direct;
368  printf("*******************************************************************\n");
369  direct = RoQ_QUAD_HANG;
370  [self write16Word: &direct to: RoQFile];
371  j = 0;
372  [self write32Word: &j to: RoQFile];
373  direct = 0;
374  [self write16Word: &direct to: RoQFile];
375  return self;
376 }
377 
378 - writeCodeBookToStream: (byte *)codebook size: (int)csize flags: (word)cflags
379 {
380 int j;
381 word direct;
382 
383  if (!csize) {
384  printf("writeCodeBook: NO VQ DATA!!!!\n");
385  return self;
386  }
387 
388  direct = RoQ_QUAD_CODEBOOK;
389 
390  [self write16Word: &direct to: RoQFile];
391 
392  j = csize;
393 
394  [self write32Word: &j to: RoQFile];
395  printf("writeCodeBook: outputting %d bytes to RoQ_QUAD_CODEBOOK\n", j);
396 
397  direct = cflags;
398  [self write16Word: &direct to: RoQFile];
399 
400  fwrite( codebook, j, 1, RoQFile);
401 
402  fflush( RoQFile );
403 
404  return self;
405 }
406 
407 - writeCodeBook: (byte *)codebook
408 {
409  memcpy( codes, codebook, 4096 );
410  return self;
411 }
412 
413 - writeFrame:(quadcel *)pquad
414 {
415 word action, direct;
416 int onCCC, onAction, i, j, code;
417 byte *cccList;
418 BOOL *use2, *use4;
419 int dx,dy,dxMean,dyMean,index2[256],index4[256], dimension;
420 
421  cccList = malloc( numQuadCels * 8); // maximum length
422  use2 = malloc(256*sizeof(BOOL));
423  use4 = malloc(256*sizeof(BOOL));
424 
425  for(i=0;i<256;i++) {
426  use2[i] = NO;
427  use4[i] = NO;
428  }
429 
430  action = 0;
431  j = onAction = 0;
432  onCCC = 2; // onAction going to go at zero
433 
434  dxMean = [encoder motMeanX];
435  dyMean = [encoder motMeanY];
436 
437  if ([image hadAlpha]) dimension = 10; else dimension = 6;
438 
439  for (i=0; i<numQuadCels; i++) {
440  if ( pquad[i].size && pquad[i].size < 16 ) {
441  switch( pquad[i].status ) {
442  case SLD:
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;
448  break;
449  case PAT:
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;
455  break;
456  case CCC:
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;
461  }
462  }
463  }
464 
465  if (!dataStuff) {
466  dataStuff=YES;
467  [self initRoQPatterns];
468  if ([image hadAlpha]) i = 3584; else i = 2560;
469  [self writeCodeBookToStream: codes size: i flags: 0];
470  for(i=0;i<256;i++) {
471  index2[i] = i;
472  index4[i] = i;
473  }
474  } else {
475  j = 0;
476  for(i=0;i<256;i++) {
477  if (use2[i]) {
478  index2[i] = j;
479  for(dx=0;dx<dimension;dx++) cccList[j*dimension+dx] = codes[i*dimension+dx];
480  j++;
481  }
482  }
483  code = j*dimension;
484  direct = j;
485  printf("writeFrame: really used %d 2x2 cels\n", j);
486  j = 0;
487  for(i=0;i<256;i++) {
488  if (use4[i]) {
489  index4[i] = j;
490  for(dx=0;dx<4;dx++) cccList[j*4+code+dx] = index2[codes[i*4+(dimension*256)+dx]];
491  j++;
492  }
493  }
494  code += j*4;
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];
500  } else {
501  [self writeCodeBookToStream: cccList size: code flags: direct];
502  }
503  }
504 
505  action = 0;
506  j = onAction = 0;
507 
508  for (i=0; i<numQuadCels; i++) {
509  if ( pquad[i].size && pquad[i].size < 16 ) {
510  code = -1;
511  switch( pquad[i].status ) {
512  case DEP:
513  code = 3;
514  break;
515  case SLD:
516  code = 2;
517  cccList[onCCC++] = index4[pquad[i].patten[0]];
518  break;
519  case MOT:
520  code = 0;
521  break;
522  case FCC:
523  code = 1;
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] );
528  exit(1);
529  }
530  cccList[onCCC++] = (dx<<4)+dy;
531  break;
532  case PAT:
533  code = 2;
534  cccList[onCCC++] = index4[pquad[i].patten[0]];
535  break;
536  case CCC:
537  code = 3;
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]];
542  break;
543  case DEAD:
544  fprintf(stderr,"dead cels in picture\n");
545  break;
546  }
547  if (code == -1) {
548  fprintf(stderr, "writeFrame: an error occurred writing the frame\n");
549  exit(2);
550  }
551 
552  action = (action<<2)|code;
553  j++;
554  if (j == 8) {
555  j = 0;
556  cccList[onAction+0] = (action & 0xff);
557  cccList[onAction+1] = ((action >> 8) & 0xff);
558  onAction = onCCC;
559  onCCC += 2;
560  }
561  }
562  }
563 
564  if (j) {
565  action <<= ((8-j)*2);
566  cccList[onAction+0] = (action & 0xff);
567  cccList[onAction+1] = ((action >> 8) & 0xff);
568  }
569 
570  direct = RoQ_QUAD_VQ;
571 
572  [self write16Word: &direct to: RoQFile];
573 
574  j = onCCC;
575  [self write32Word: &j to: RoQFile];
576 
577  direct = dyMean;
578  direct &= 0xff;
579  direct += (dxMean<<8); // flags
580 
581  [self write16Word: &direct to: RoQFile];
582 
583  printf("writeFrame: outputting %d bytes to RoQ_QUAD_VQ\n", j);
584 
585  previousSize = j;
586 
587  fwrite( cccList, onCCC, 1, RoQFile );
588 
589  fflush( RoQFile );
590 
591  free( cccList );
592  free( use2 );
593  free( use4 );
594 
595  return self;
596 }
597 
598 - writePuzzleFrame:(quadcel *)pquad
599 {
600  return self;
601 }
602 
603 - (TByteBitmapImageRep *)scaleImage: (TByteBitmapImageRep *)toscale
604 {
605 int newx, newy,x,y,s,linesize;
606 TByteBitmapImageRep *newImage;
607 unsigned char *i0, *i1;
608 int newv;
609 
610  newx = 288;
611 
612  if ([toscale pixelsHigh] == 160) {
613  newy = 320;
614  } else {
615  newy = [toscale pixelsHigh];
616  }
617  newImage = [[TByteBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
618  pixelsWide: newx
619  pixelsHigh: newy
620  bitsPerSample: 8
621  samplesPerPixel: 4
622  hasAlpha: YES
623  isPlanar: NO
624  colorSpaceName: NSCalibratedRGBColorSpace
625  bytesPerRow: 0
626  bitsPerPixel: 0];
627 
628  i0 = [toscale bitmapData];
629  i1 = [newImage bitmapData];
630  linesize = [toscale pixelsWide]*4;
631 
632  for(y=0; y<newy; y++) {
633  for(x=0; x<320; x++) {
634  if (x>=16 && x<304) {
635  for(s=0;s<4;s++) {
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];
641  newv = newv/4;
642  } else {
643  newv = i0[(x*2+0)*4+y*linesize+s];
644  newv += i0[(x*2+1)*4+y*linesize+s];
645  newv = newv/2;
646  }
647  i1[(x-16)*4+y*(288*4)+s] = newv;
648  }
649  }
650  }
651  }
652  return (newImage);
653 }
654 
655 //
656 // load a frame, create a window (if neccesary) and display the frame
657 //
658 - loadAndDisplayImage: (const char *) filename
659 {
660 NSRect cRect;
661 char *secondFilename,firstFilename[MAXPATHLEN+1];
662 id image1;
663 NSSize newSize;
664 // unsigned char *imageData;
665 // static BOOL cleared = NO;
666 
667  if (image) [image dealloc];
668 
669  printf("loadAndDisplayImage: %s\n", filename);
670 
671  strcpy( currentFile, filename );
672 
673  image = [TByteBitmapImageRep alloc];
674  if (!(secondFilename= strchr(filename,'\n'))) { //one filename, no compositing
675  [image initFromFile: filename ];
676  }
677  else {
678  strncpy(firstFilename,filename,secondFilename-filename);
679  firstFilename[secondFilename-filename]='\0';
680  secondFilename++;//point past the \n
681  image1 = [[TByteBitmapImageRep alloc] initFromFile:firstFilename];
682  [image initFromFile: secondFilename ];//result will be in here
683  //image is the composite of those two...
684  [self composite:image1 to:image];
685  }
686 //
687 // wolf stuff
688 //
689  newSize.width = 256;
690  newSize.height = 256;
691 
692  [image setSize: newSize];
693 
694  if ([paramFileId output3DO] == YES) {
695  image1 = [self scaleImage: image];
696  [image dealloc];
697  image = image1;
698  }
699 
700  numQuadCels = (([image pixelsWide] & 0xfff0)*([image pixelsHigh] & 0xfff0))/(MINSIZE*MINSIZE);
701  numQuadCels += numQuadCels/4 + numQuadCels/16;
702 
703 // if ([paramFileId deltaFrames] == YES && cleared == NO && [image isPlanar] == NO) {
704 // cleared = YES;
705 // imageData = [image data];
706 // memset( imageData, 0, [image pixelsWide]*[image pixelsHigh]*[image samplesPerPixel]);
707 // }
708 
709  if (!quietMode) printf("loadAndDisplayImage: %dx%d\n", [image pixelsWide], [image pixelsHigh]);
710 
711  if (!quietMode) {
712  if (!cWindow) {
713  cRect.origin.x = 8.0 * 48.0;
714  cRect.origin.y = ([image pixelsHigh]+80);
715  cRect.size.width = [image pixelsWide];
716  cRect.size.height = [image pixelsHigh];
717  cWindow = [[NSWindow alloc] initWithContentRect:cRect styleMask:NSTitledWindowMask
718  backing:NSBackingStoreBuffered defer:NO];
719  cRect.origin.x = cRect.origin.y = 0.0;
720 // [[cWindow contentView] setClipping:NO];
721  [cWindow setTitle: @"current frame"];
722  [cWindow makeKeyAndOrderFront: [cWindow contentView]];
723  [cWindow display];
724  }
725 
726 
727  cRect = [[cWindow contentView] bounds];
728  [[cWindow contentView] lockFocus];
729  [image drawInRect: cRect];
730  [[cWindow contentView] unlockFocus];
731  [cWindow flushWindow];
732 
733  if (!eWindow) {
734  cRect.origin.x = 8.0 * 48.0 - ([image pixelsWide]>>1) - 4;
735  cRect.origin.y = ([image pixelsHigh]+80);
736  cRect.size.width = [image pixelsWide] >> 1;
737  cRect.size.height = [image pixelsHigh] >> 1;
738  eWindow = [[NSWindow alloc] initWithContentRect:cRect styleMask:NSTitledWindowMask
739  backing:NSBackingStoreBuffered defer:NO];
740  cRect.origin.x = cRect.origin.y = 0.0;
741 // [[eWindow contentView] setClipping:NO];
742  [eWindow setTitle: @"cel error"];
743  [eWindow makeKeyAndOrderFront: [eWindow contentView]];
744  [eWindow display];
745  }
746 
747  if (!sWindow) {
748  cRect.origin.x = 8.0 * 48.0 - ([image pixelsWide]>>1) - 4;
749  cRect.origin.y = ([image pixelsHigh]+80) + (([image pixelsHigh]+48)>>1);
750  cRect.size.width = [image pixelsWide] >> 1;
751  cRect.size.height = [image pixelsHigh] >> 1;
752  sWindow = [[NSWindow alloc] initWithContentRect: cRect styleMask:NSTitledWindowMask
753  backing:NSBackingStoreBuffered defer:NO];
754  cRect.origin.x = cRect.origin.y = 0.0;
755 // [[eWindow contentView] setClipping:NO];
756  [sWindow setTitle: @"quadtree map"];
757  [sWindow makeKeyAndOrderFront: [sWindow contentView]];
758  [sWindow display];
759  }
760 
761  cRect = [[sWindow contentView] bounds];
762  [[sWindow contentView] lockFocus];
763  [image drawInRect: cRect];
764  [[sWindow contentView] unlockFocus];
765  [sWindow flushWindow];
766 
767  cRect = [[eWindow contentView] bounds];
768  [[eWindow contentView] lockFocus];
769  [image drawInRect: cRect];
770  [[eWindow contentView] unlockFocus];
771  [eWindow flushWindow];
772 
773  if (!errImage) {
774  errImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
775  pixelsWide: 1
776  pixelsHigh: 1
777  bitsPerSample: 8
778  samplesPerPixel: 3
779  hasAlpha: NO
780  isPlanar: NO
781  colorSpaceName: NSCalibratedRGBColorSpace
782  bytesPerRow: 0
783  bitsPerPixel: 0];
784  }
785 
786 // NSPing();
787  }
788 
789  return self;
790 }
791 
792 
793 - markQuadx: (int)xat quady: (int)yat quads: (int)size error: (float)cerror type: (int)choice
794 {
795 NSRect cRect;
796 byte *err;
797 static int ywasat = -1;
798  if (!quietMode) {
799  cRect.origin.x = (xat)>>1;
800  cRect.origin.y = ([image pixelsHigh] - yat - size)>>1;
801  cRect.size.width = cRect.size.height = (size)>>1;
802 
803  if (size < 1) {
804  [[sWindow contentView] lockFocus];
805  if (size == 8 && choice == 1) {
806  PSsetgray(NSWhite);
807  } else if (size == 8 && choice == 3) {
808  PSsetgray(NSLightGray);
809  } else if (size == 4) {
810  PSsetgray(NSDarkGray);
811  } else if (size == 2) {
812  PSsetgray(NSBlack);
813  }
814  NSFrameRectWithWidth(cRect,0.0);
815  [[sWindow contentView] unlockFocus];
816  if (!(ywasat & 31)) {
817  [sWindow flushWindow];
818  }
819  }
820 
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;
827 
828  [[eWindow contentView] lockFocus];
829  [errImage drawInRect: cRect];
830  [[eWindow contentView] unlockFocus];
831  if (!(ywasat & 31)) {
832  [eWindow flushWindow];
833  }
834  ywasat++;
835  }
836 
837  return self;
838 }
839 
840 - (NSBitmapImageRep*)currentImage
841 {
842  return image;
843 }
844 
845 - (id)errorWindow
846 {
847  return eWindow;
848 }
849 
850 - (id)scaleWindow
851 {
852  return sWindow;
853 }
854 - (int)numberOfFrames {
855  return numberOfFrames;
856 }
857 
858 - composite:(NSBitmapImageRep *)source to: (NSBitmapImageRep *)destination
859 {
860 unsigned short value;
861 int x,y,bpp,inc,pixelsWide,pixelsHigh,yoff,pixel;
862 byte *iPlane[5], *dPlane[5];
863 
864  bpp = [source samplesPerPixel];
865  pixelsWide = [source pixelsWide];
866  pixelsHigh = [source pixelsHigh];
867 
868  if ([source isPlanar]) {
869  [source getBitmapDataPlanes: iPlane];
870  [destination getBitmapDataPlanes: dPlane];
871  for(y=0;y<pixelsHigh;y++) {
872  yoff = y*pixelsWide;
873  for(x=0;x<pixelsWide;x++) {
874  if ([destination hasAlpha]) {
875  value = dPlane[3][yoff+x];
876  } else {
877  value = 255;
878  }
879  if (value == 0) {
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];
893  }
894  }
895  }
896  }
897  } else {
898  iPlane[0] = [source bitmapData];
899  dPlane[0] = [destination bitmapData];
900  for(y=0;y<pixelsHigh;y++) {
901  yoff = y*pixelsWide*bpp;
902  for(x=0;x<pixelsWide;x++) {
903  inc = x*bpp;
904  if ([destination hasAlpha]) {
905  value = dPlane[0][yoff+inc+3];
906  } else {
907  value = 255;
908  }
909  if (value == 0) {
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];
923  }
924  }
925  }
926  }
927  }
928 
929  return self;
930 
931 
932 }
933 @end
#define strcmp
Definition: Str.h:41
GLsizei const GLfloat * value
Definition: glext.h:3614
#define RoQ_QUAD_JPEG
Definition: quaddefs.h:68
#define PAT
Definition: quaddefs.h:37
int previousSize
Definition: roq.h:127
#define const
Definition: getdate.c:251
GLenum GLint GLint y
Definition: glext.h:2849
#define MOT
Definition: quaddefs.h:38
#define RoQ_QUAD_VQ
Definition: quaddefs.h:67
case const int
Definition: Callbacks.cpp:52
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:2855
#define DEAD
Definition: quaddefs.h:39
GLdouble s
Definition: glext.h:2935
#define DEP
Definition: quaddefs.h:33
GLenum GLint x
Definition: glext.h:2849
int i
Definition: process.py:33
#define BOOL
Definition: mprintf.c:71
int numberOfFrames
Definition: roq.h:126
#define FCC
Definition: quaddefs.h:34
#define SEEK_END
Definition: Unzip.cpp:131
GLuint index
Definition: glext.h:3476
const GLubyte * c
Definition: glext.h:4677
#define SLD
Definition: quaddefs.h:36
GLuint buffer
Definition: glext.h:3108
codec * encoder
Definition: roq.h:116
int fputc(int, FILE *)
Definition: codec.h:45
#define RoQ_ID
Definition: quaddefs.h:61
#define CCC
Definition: quaddefs.h:35
#define RoQ_QUAD_CODEBOOK
Definition: quaddefs.h:69
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
word domain
Definition: quaddefs.h:99
#define RoQ_QUAD_INFO
Definition: quaddefs.h:66
GLubyte GLubyte b
Definition: glext.h:4662
static WindowRef ValidModeCallbackProc inCallback OSStatus err
bool dataStuff
Definition: roq.h:129
size_t fread(void *, size_t, size_t, FILE *)
bool quietMode
Definition: roq.h:122
#define SEEK_SET
Definition: Unzip.cpp:129
Definition: roq.h:69
GLuint id
Definition: glext.h:3103
unsigned short word
Definition: Lib.h:76
unsigned char byte
Definition: Lib.h:75
GLsizeiptr size
Definition: glext.h:3112
const int MINSIZE
Definition: Cinematic.cpp:120
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLint i1
Definition: qgl.h:261
idFile * RoQFile
Definition: roq.h:119
idStr currentFile
Definition: roq.h:125
GLuint res
Definition: glext.h:5385
idStr roqOutfile
Definition: roq.h:124
#define RoQ_QUAD_HANG
Definition: quaddefs.h:64
GLint j
Definition: qgl.h:264
bool lastFrame
Definition: roq.h:123
int numQuadCels
Definition: roq.h:121
byte codes[4096]
Definition: roq.h:128
NSBitmapImageRep * image
Definition: roq.h:120
size_t fwrite(const void *, size_t, size_t, FILE *)
byte * bitmapData(void)
int sprintf(idStr &string, const char *fmt,...)
Definition: Str.cpp:1528