00001 #include "FCam/FCam.h"
00002
00003 #include "../src/Debug.h"
00004 #include <stdio.h>
00005
00006 using namespace FCam;
00007
00008 int main(int argc, const char **argv) {
00009 printf("Testing the image class\n");
00010
00011 printf("Constructing some images\n");
00012 Image small(640, 480, UYVY);
00013 FCAM_IMAGE_DEBUG(small);
00014
00015 Image big(1024, 1024, UYVY);
00016 FCAM_IMAGE_DEBUG(big);
00017
00018 unsigned char data[100*100*2];
00019 Image weak(100, 100, UYVY, &data[0]);
00020 FCAM_IMAGE_DEBUG(weak);
00021
00022 {
00023 printf("\nTesting copy constructor\n");
00024 Image newsmall(small);
00025 FCAM_IMAGE_DEBUG(small);
00026 FCAM_IMAGE_DEBUG(newsmall);
00027
00028 printf("\n");
00029 Image newweak(weak);
00030 FCAM_IMAGE_DEBUG(weak);
00031 FCAM_IMAGE_DEBUG(newweak);
00032 printf("Destroying copies...\n");
00033 }
00034 FCAM_IMAGE_DEBUG(small);
00035 FCAM_IMAGE_DEBUG(weak);
00036
00037 {
00038 printf("\nTesting assignment operator\n");
00039
00040 Image newsmall = small;
00041 FCAM_IMAGE_DEBUG(small);
00042 FCAM_IMAGE_DEBUG(newsmall);
00043
00044 Image newweak = weak;
00045 FCAM_IMAGE_DEBUG(weak);
00046 FCAM_IMAGE_DEBUG(newweak);
00047
00048 printf("Destroying assigned copies...\n");
00049 }
00050 FCAM_IMAGE_DEBUG(small);
00051 FCAM_IMAGE_DEBUG(weak);
00052
00053 {
00054 printf("\nTesting locking a copy\n");
00055 Image newsmall(small);
00056 Image newweak(weak);
00057
00058 newsmall.lock();
00059 if (small.lock(0)) {
00060 printf("ERROR: should not have been able to acquire lock on original when copy is locked\n");
00061 FCAM_IMAGE_DEBUG(small);
00062 FCAM_IMAGE_DEBUG(newsmall);
00063 return -1;
00064 }
00065 newsmall.unlock();
00066
00067 if (!small.lock(0)) {
00068 printf("ERROR: should be able to acquire lock on original when copy is unlocked\n");
00069 FCAM_IMAGE_DEBUG(small);
00070 FCAM_IMAGE_DEBUG(newsmall);
00071 return -1;
00072 }
00073
00074 if (newsmall.lock(0)) {
00075 printf("ERROR: should be not able to acquire lock on copy where original is locked\n");
00076 FCAM_IMAGE_DEBUG(small);
00077 FCAM_IMAGE_DEBUG(newsmall);
00078 return -1;
00079 }
00080
00081 printf("\nTesting destroying an unlocked copy while original is locked\n");
00082 weak.lock();
00083 }
00084
00085 weak.unlock();
00086 small.unlock();
00087 FCAM_IMAGE_DEBUG(small);
00088 FCAM_IMAGE_DEBUG(weak);
00089
00090 {
00091 printf("Testing making a copy, then setting the copies to special values\n");
00092
00093 Image newsmall(small);
00094 newsmall = Image(newsmall.size(), newsmall.type(), Image::AutoAllocate);
00095 FCAM_IMAGE_DEBUG(small);
00096 FCAM_IMAGE_DEBUG(newsmall);
00097
00098 Image newweak(weak);
00099 newweak = Image(newweak.size(), newweak.type(), Image::Discard);
00100 FCAM_IMAGE_DEBUG(weak);
00101 FCAM_IMAGE_DEBUG(newweak);
00102
00103 printf("\nTesting deleting images with special values\n");
00104 }
00105 FCAM_IMAGE_DEBUG(small);
00106 FCAM_IMAGE_DEBUG(weak);
00107
00108 {
00109 printf("\nTesting assigning myself to a copy\n");
00110 Image foo(small);
00111 small = foo;
00112 FCAM_IMAGE_DEBUG(small);
00113 }
00114 FCAM_IMAGE_DEBUG(small);
00115
00116 {
00117 printf("\nTesting decref of assigned copies\n");
00118 for (int i = 0; i < 10; i++) {
00119 Image newsmall(small);
00120 newsmall = weak;
00121 }
00122 }
00123 FCAM_IMAGE_DEBUG(small);
00124 FCAM_IMAGE_DEBUG(weak);
00125
00126
00127 {
00128 printf("\nTesting assigning myself to myself\n");
00129 small = small;
00130 }
00131 FCAM_IMAGE_DEBUG(small);
00132
00133 {
00134 printf("\nTesting making a backup, nuking myself, and restoring from backup\n");
00135 Image newsmall(small);
00136 small = Image();
00137 small = newsmall;
00138
00139 Image newweak(weak);
00140 weak = Image();
00141 weak = newweak;
00142 }
00143 FCAM_IMAGE_DEBUG(small);
00144 FCAM_IMAGE_DEBUG(weak);
00145
00146 printf("\nTiming creation and deletion of lots of images\n");
00147 Time start = Time::now();
00148 for (int i = 0; i < 1000; i++) {
00149 Image *a = new Image(640, 480, UYVY);
00150 Image *b = new Image(640, 480, UYVY);
00151 Image *c = new Image(640, 480, UYVY);
00152 Image *d = new Image(640, 480, UYVY);
00153 Image *e = new Image(640, 480, UYVY);
00154 delete e;
00155 delete d;
00156 delete c;
00157 delete b;
00158 delete a;
00159 }
00160 Time end = Time::now();
00161 printf("Took %d us per allocation and deletion\n", (end-start)/5000);
00162
00163 printf("\nTesting image locking\n");
00164 {
00165 Image newsmall(small);
00166 small.lock();
00167 printf("This should not deadlock...\n");
00168 start = Time::now();
00169 if (newsmall.lock(10000)) {
00170 printf("ERROR: two different references to an image both successfully locked it\n");
00171 return 1;
00172 }
00173 end = Time::now();
00174 printf("Timeout on locking an image took %d us when it should take %d us\n", end-start, 10000);
00175 printf("Unlocking image\n");
00176 small.unlock();
00177 printf("Locking different reference and letting it fall out of scope\n");
00178 newsmall.lock();
00179 }
00180 printf("Relocking image\n");
00181 small.lock();
00182 small.unlock();
00183
00184
00185 printf("\nTesting subimages\n");
00186
00187 FCAM_IMAGE_DEBUG(big);
00188 printf(" subImage1 = big.subImage(100,100,Size(100,100));\n");
00189 Image subImage1 = big.subImage(100,100,Size(100,100));
00190 FCAM_IMAGE_DEBUG(subImage1);
00191 printf(" subImage2 = big.subImage(500,100,Size(big.size.width,100));\n");
00192 Image subImage2 = big.subImage(500,100,Size(big.width(),100));
00193 FCAM_IMAGE_DEBUG(subImage2);
00194
00195 printf("\nTesting image copy\n");
00196 FCAM_IMAGE_DEBUG(small);
00197 printf(" small = weak.copy()\n");
00198 small = weak.copy();
00199 FCAM_IMAGE_DEBUG(small);
00200
00201 printf("\nTesting image copyFrom\n");
00202 *small(0,0)=100;
00203 *weak(0,0)=150;
00204 FCAM_IMAGE_DEBUG(small);
00205 printf(" small(0,0)=%d, weak(0,0)=%d\n", *small(0,0), *weak(0,0));
00206 printf(" small.copyFrom(weak)\n");
00207 small.copyFrom(weak);
00208 FCAM_IMAGE_DEBUG(small);
00209 printf(" small(0,0)=%d, weak(0,0)=%d\n", *small(0,0), *weak(0,0));
00210
00211 printf("\nTesting subImage assignment\n");
00212 FCAM_IMAGE_DEBUG(subImage1);
00213 FCAM_IMAGE_DEBUG(subImage2);
00214 printf(" subImage1 = subImage2\n");
00215 subImage1 = subImage2;
00216 FCAM_IMAGE_DEBUG(subImage1);
00217
00218 printf(" subImage1 = small\n");
00219 subImage1 = small;
00220 FCAM_IMAGE_DEBUG(subImage1);
00221 FCAM_IMAGE_DEBUG(subImage2);
00222 FCAM_IMAGE_DEBUG(small);
00223
00224 printf("Success!\n");
00225 return 0;
00226 }