Changeset 513 for abuse/trunk/src/imlib/image.cpp
- Timestamp:
- Apr 20, 2011, 8:32:26 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
abuse/trunk/src/imlib/image.cpp
r512 r513 80 80 81 81 82 image_descriptor::image_descriptor( int16_t length, int16_t height,82 image_descriptor::image_descriptor(vec2i size, 83 83 int keep_dirties, int static_memory) 84 84 85 85 { clipx1=0; clipy1=0; 86 l= length; h=height;86 l=size.x; h=size.y; 87 87 clipx2=l-1; clipy2=h-1; 88 88 keep_dirt=keep_dirties; … … 90 90 } 91 91 92 void image:: change_size(int16_t new_width, int16_t new_height, uint8_t *page)93 { 94 delete_page();95 size = vec2i(new_width, new_height);96 make_page(new_width, new_height, page);92 void image::SetSize(vec2i new_size, uint8_t *page) 93 { 94 delete_page(); 95 m_size = new_size; 96 make_page(new_size, page); 97 97 } 98 98 99 99 image::~image() 100 100 { 101 if( _locked)101 if(m_locked) 102 102 { 103 103 fprintf(stderr, "Error: image is locked upon deletion\n"); … … 121 121 uint8_t image::pixel(int16_t x, int16_t y) 122 122 { 123 CONDITION(x>=0 && x< size.x && y>=0 && y<size.y,123 CONDITION(x>=0 && x<m_size.x && y>=0 && y<m_size.y, 124 124 "image::pixel Bad pixel xy"); 125 125 return (*(scan_line(y)+x)); … … 128 128 void image::putpixel(int16_t x, int16_t y, char color) 129 129 { 130 CONDITION(x>=0 && x< size.x && y>=0 && y<size.y,130 CONDITION(x>=0 && x<m_size.x && y>=0 && y<m_size.y, 131 131 "image::putpixel Bad pixel xy"); 132 132 if (special) … … 138 138 139 139 140 image::image(int16_t width, int16_t height, uint8_t *page_buffer, int16_t create_descriptor) 141 { 142 size = vec2i(width, height); 143 if (create_descriptor || page_buffer) 144 { 145 if (create_descriptor==2) 146 special=new image_descriptor(width, height, 1, (page_buffer!=NULL)); 147 else special=new image_descriptor(width, height, 0, (page_buffer!=NULL)); 148 } else special=NULL; 149 make_page(width, height, page_buffer); 150 image_list.add_end((linked_node *) this); 151 _locked = false; 140 image::image(vec2i size, uint8_t *page_buffer, int create_descriptor) 141 { 142 m_size = size; 143 if (create_descriptor || page_buffer) 144 { 145 if (create_descriptor==2) 146 special=new image_descriptor(size, 1, (page_buffer!=NULL)); 147 else 148 special=new image_descriptor(size, 0, (page_buffer!=NULL)); 149 } 150 else 151 special = NULL; 152 make_page(size, page_buffer); 153 image_list.add_end((linked_node *)this); 154 m_locked = false; 152 155 } 153 156 154 157 image::image(spec_entry *e, bFILE *fp) 155 158 { 156 int16_t i;157 159 fp->seek(e->offset, 0); 158 size.x = fp->read_uint16();159 size.y = fp->read_uint16();160 m_size.x = fp->read_uint16(); 161 m_size.y = fp->read_uint16(); 160 162 special = NULL; 161 make_page( size.x, size.y, NULL);162 for (i = 0; i <size.y; i++)163 fp->read(scan_line(i), size.x);163 make_page(m_size, NULL); 164 for (int i = 0; i < m_size.y; i++) 165 fp->read(scan_line(i), m_size.x); 164 166 image_list.add_end((linked_node *) this); 165 _locked = false;167 m_locked = false; 166 168 } 167 169 168 170 image::image(bFILE *fp) 169 171 { 170 int16_t i; 171 size.x = fp->read_uint16(); 172 size.y = fp->read_uint16(); 172 m_size.x = fp->read_uint16(); 173 m_size.y = fp->read_uint16(); 173 174 special = NULL; 174 make_page( size.x, size.y, NULL);175 for (i = 0; i <size.y; i++)176 fp->read(scan_line(i), size.x);175 make_page(m_size, NULL); 176 for (int i = 0; i < m_size.y; i++) 177 fp->read(scan_line(i), m_size.x); 177 178 image_list.add_end((linked_node *) this); 178 _locked = false;179 m_locked = false; 179 180 } 180 181 … … 183 184 /* This is currently a no-op, because it's unneeded with SDL */ 184 185 185 if( _locked)186 if(m_locked) 186 187 fprintf(stderr, "Trying to lock a locked picture!\n"); 187 _locked = true;188 m_locked = true; 188 189 } 189 190 … … 192 193 /* This is currently a no-op, because it's unneeded with SDL */ 193 194 194 if(! _locked)195 if(!m_locked) 195 196 fprintf(stderr, "Trying to unlock an unlocked picture!\n"); 196 _locked = false;197 m_locked = false; 197 198 } 198 199 … … 237 238 238 239 lock(); 239 for(co = 0, i = size.y - 1; i >= 0; i--)240 for(co = 0, i = m_size.y - 1; i >= 0; i--) 240 241 { 241 242 c = scan_line(i); 242 for(j = size.x - 1; j >= 0; j--, c++)243 for(j = m_size.x - 1; j >= 0; j--, c++) 243 244 if(*c != background) co++; 244 245 } … … 262 263 } 263 264 else 264 for(i = size.y - 1; i >= 0; i--)265 memset(scan_line(i), color, size.x);266 add_dirty(0, 0, size.x - 1,size.y - 1);265 for(i = m_size.y - 1; i >= 0; i--) 266 memset(scan_line(i), color, m_size.x); 267 add_dirty(0, 0, m_size.x - 1, m_size.y - 1); 267 268 unlock(); 268 269 } … … 270 271 image *image::copy() 271 272 { 272 image *im;273 uint8_t *c, *dat;274 int i;275 276 273 lock(); 277 dat = (uint8_t *)malloc(size.x); 278 im = new image(size.x, size.y); 274 image *im = new image(m_size); 279 275 im->lock(); 280 for(i = size.y - 1; i >= 0; i--) 281 { 282 c = scan_line(i); 283 memcpy(dat, c, size.x); 284 c = im->scan_line(i); 285 memcpy(c, dat, size.x); 286 } 276 for(int i = m_size.y - 1; i >= 0; i--) 277 memcpy(im->scan_line(i), scan_line(i), m_size.x); 287 278 im->unlock(); 288 279 unlock(); 289 free((char *)dat);290 280 return im; 291 281 } … … 443 433 if(screen->special) 444 434 { 445 put_part(screen, x, y, 0, 0, size.x-1,size.y-1, transparent);435 put_part(screen, x, y, 0, 0, m_size.x-1, m_size.y-1, transparent); 446 436 return; 447 437 } … … 449 439 if(x < screen->Size().x && y < screen->Size().y) 450 440 { 451 xl = size.x;441 xl = m_size.x; 452 442 if(x + xl > screen->Size().x) // clip to the border of the screen 453 443 xl = screen->Size().x - x; 454 yl = size.y;444 yl = m_size.y; 455 445 if(y + yl > screen->Size().y) 456 446 yl = screen->Size().y - y; … … 517 507 return ; 518 508 screen->add_dirty(x1, y1, x2, y2); 519 w= size.x;509 w=m_size.x; 520 510 if (align) 521 511 { 522 512 start=x1%w; 523 starty=y1% size.y;513 starty=y1%m_size.y; 524 514 } 525 515 else … … 533 523 pg1=screen->scan_line(j); 534 524 pg2=scan_line(starty++); 535 if (starty>= size.y) starty=0;525 if (starty>=m_size.y) starty=0; 536 526 i=x1; 537 527 xx=start; … … 565 555 if (x1<0) { x+=-x1; x1=0; } 566 556 if (y1<0) { y+=-y1; y1=0; } 567 if (x2>= size.x) x2=size.x-1;568 if (y2>= size.y) y2=size.y-1;557 if (x2>=m_size.x) x2=m_size.x-1; 558 if (y2>=m_size.y) y2=m_size.y-1; 569 559 if (x1>x2 || y1>y2) return ; // return if it was adjusted so that nothing will be put 570 560 … … 628 618 CHECK(x1<=x2 && y1<=y2); 629 619 630 i=x1; x1= size.x-x2-1; // reverse the x locations631 x2= size.x-i-1;620 i=x1; x1=m_size.x-x2-1; // reverse the x locations 621 x2=m_size.x-i-1; 632 622 633 623 if (x1<0) … … 652 642 return ; 653 643 654 if (x<screen->Size().x && y<screen->Size().y && x1< size.x && y1<size.y &&644 if (x<screen->Size().x && y<screen->Size().y && x1<m_size.x && y1<m_size.y && 655 645 x1<=x2 && y1<=y2) 656 646 { 657 if (x2>= size.x)658 x2= size.x-1;659 if (y2>= size.y)660 y2= size.y-1;647 if (x2>=m_size.x) 648 x2=m_size.x-1; 649 if (y2>=m_size.y) 650 y2=m_size.y-1; 661 651 xl=x2-x1+1; 662 652 if (x+xl>screen->Size().x) … … 714 704 ml=mask->Size().x; 715 705 mh=mask->Size().y; 716 if (x<screen->Size().x && y<screen->Size().y && x1< size.x && y1<size.y &&706 if (x<screen->Size().x && y<screen->Size().y && x1<m_size.x && y1<m_size.y && 717 707 maskx<ml && masky<mh && x1<=x2 && y1<=y2) 718 708 { 719 709 720 if (x2>= size.x)721 x2= size.x-1;722 if (y2>= size.y)723 y2= size.y-1;710 if (x2>=m_size.x) 711 x2=m_size.x-1; 712 if (y2>=m_size.y) 713 y2=m_size.y-1; 724 714 xl=x2-x1+1; 725 715 if (x+xl>screen->Size().x) … … 761 751 brv=0; bri=0; 762 752 lock(); 763 for (j=0; j< size.y; j++)753 for (j=0; j<m_size.y; j++) 764 754 { 765 755 p=scan_line(j); 766 for (i=0; i< size.x; i++)756 for (i=0; i<m_size.x; i++) 767 757 { pal->get(p[i], r, g, b); 768 758 if ((int32_t)r*(int32_t)g*(int32_t)b>brv) … … 782 772 brv=(int32_t)258*(int32_t)258*(int32_t)258; bri=0; 783 773 lock(); 784 for (j=0; j< size.y; j++)774 for (j=0; j<m_size.y; j++) 785 775 { 786 776 p=scan_line(j); 787 for (i=0; i< size.x; i++)777 for (i=0; i<m_size.x; i++) 788 778 { pal->get(p[i], r, g, b); 789 779 x=(int32_t)r*(int32_t)g*(int32_t)b; … … 811 801 // with no dirty rectangle keeping. 812 802 if(!special) 813 special = new image_descriptor( size.x,size.y, 0);803 special = new image_descriptor(m_size.x, m_size.y, 0); 814 804 815 805 // set the image descriptor what the clip … … 823 813 special->get_clip(x1, y1, x2, y2); 824 814 else 825 { x1=0; y1=0; x2= size.x-1; y2=size.y-1; }815 { x1=0; y1=0; x2=m_size.x-1; y2=m_size.y-1; } 826 816 } 827 817 … … 1056 1046 { if (x1<0) x1=0; 1057 1047 if (y1<0) y1=0; 1058 if (x2>= size.x) x2=size.x-1;1059 if (y2>= size.y) y2=size.y-1;1060 } 1061 if (x2<0 || y2<0 || x1>= size.x || y1>=size.y || x2<x1 || y2<y1)1048 if (x2>=m_size.x) x2=m_size.x-1; 1049 if (y2>=m_size.y) y2=m_size.y-1; 1050 } 1051 if (x2<0 || y2<0 || x1>=m_size.x || y1>=m_size.y || x2<x1 || y2<y1) 1062 1052 return ; 1063 1053 lock(); … … 1081 1071 { if (x1<0) x1=0; 1082 1072 if (y1<0) y1=0; 1083 if (x2>= size.x) x2=size.x-1;1084 if (y2>= size.y) y2=size.y-1;1085 } 1086 if (x2<0 || y2<0 || x1>= size.x || y1>=size.y || x2<x1 || y2<y1)1073 if (x2>=m_size.x) x2=m_size.x-1; 1074 if (y2>=m_size.y) y2=m_size.y-1; 1075 } 1076 if (x2<0 || y2<0 || x1>=m_size.x || y1>=m_size.y || x2<x1 || y2<y1) 1087 1077 return ; 1088 1078 … … 1094 1084 for (x=x1; x<=x2; x++, s++) 1095 1085 *s=(*s)^color; 1096 sl+= size.x;1086 sl+=m_size.x; 1097 1087 } 1098 1088 unlock(); … … 1106 1096 int16_t x; 1107 1097 uint8_t *sl, *ex, mask, bt, sh; 1108 ex=(uint8_t *)malloc( size.x);1098 ex=(uint8_t *)malloc(m_size.x); 1109 1099 1110 1100 lock(); 1111 1101 sl=scan_line(line); 1112 memcpy(ex, sl, size.x);1102 memcpy(ex, sl, m_size.x); 1113 1103 unlock(); 1114 1104 … … 1117 1107 else { mask=128+64+32+16; bt=2; } 1118 1108 1119 for (x=0; x< size.x; x++)1109 for (x=0; x<m_size.x; x++) 1120 1110 { sh=((x%bt)<<(bitsperpixel-1)); 1121 1111 sl[x]=(ex[x/bt]&(mask>>sh))>>(bt-sh-1); … … 1135 1125 uint8_t *sl; 1136 1126 lock(); 1137 for (y= size.y-1; y>=0; y--)1127 for (y=m_size.y-1; y>=0; y--) 1138 1128 { 1139 1129 sl=scan_line(y); 1140 for (i=0, j=y%4, x= size.x-1; x>=0; x--)1130 for (i=0, j=y%4, x=m_size.x-1; x>=0; x--) 1141 1131 { 1142 1132 if (pal->red(sl[x])>dt_matrix[j*4+i]) … … 1160 1150 } 1161 1151 1162 void image::resize(int16_t new_width, int16_t new_height) 1163 { 1164 int old_width=size.x, old_height=size.y; 1165 uint8_t *im=(uint8_t *)malloc(size.x*size.y); 1166 lock(); 1167 memcpy(im, scan_line(0), size.x*size.y); 1168 1169 delete_page(); 1170 make_page(new_width, new_height, NULL); 1171 size = vec2i(new_width, new_height); // set the new height and width 1172 1173 uint8_t *sl1, *sl2; 1174 int16_t y, y2, x2; 1175 double yc, xc, yd, xd; 1176 1177 1178 1179 yc=(double)old_height/(double)new_height; 1180 xc=(double)old_width/(double)new_width; 1181 for (y2=0, yd=0; y2<new_height; yd+=yc, y2++) 1182 { 1183 y=(int)yd; 1184 sl1=im+y*old_width; 1185 sl2=scan_line(y2); 1186 for (xd=0, x2=0; x2<new_width; xd+=xc, x2++) 1187 { sl2[x2]=sl1[(int)xd]; } 1188 } 1189 free(im); 1190 if (special) special->resize(new_width, new_height); 1191 unlock(); 1152 void image::Scale(vec2i new_size) 1153 { 1154 vec2i old_size = m_size; 1155 uint8_t *im = (uint8_t *)malloc(old_size.x * old_size.y); 1156 lock(); 1157 memcpy(im, scan_line(0), old_size.x * old_size.y); 1158 1159 delete_page(); 1160 make_page(new_size, NULL); 1161 m_size = new_size; // set the new height and width 1162 1163 uint8_t *sl1, *sl2; 1164 int y, y2, x2; 1165 double yc, xc, yd, xd; 1166 1167 yc = (double)old_size.y / (double)new_size.y; 1168 xc = (double)old_size.x / (double)new_size.x; 1169 for (y2 = 0, yd = 0; y2 < new_size.y; yd += yc, y2++) 1170 { 1171 y = (int)yd; 1172 sl1 = im + y * old_size.x; 1173 sl2 = scan_line(y2); 1174 for (xd = 0, x2 = 0; x2 < new_size.x; xd += xc, x2++) 1175 sl2[x2] = sl1[(int)xd]; 1176 } 1177 free(im); 1178 if (special) 1179 special->resize(new_size.x, new_size.y); 1180 unlock(); 1192 1181 } 1193 1182 … … 1195 1184 { 1196 1185 int16_t cx1, cy1, cx2, cy2; 1197 CHECK(x1>=0 && y1>=0 && x1<x2 && y1<y2 && x2< size.x && y2<size.y);1186 CHECK(x1>=0 && y1>=0 && x1<x2 && y1<y2 && x2<m_size.x && y2<m_size.y); 1198 1187 if (special) 1199 1188 { … … 1227 1216 d=smoothness*2+1; 1228 1217 d=d*d; 1229 im=new image( size.x, size.y);1230 for (i=0; i< size.x; i++)1231 for (j=0; j< size.y; j++)1218 im=new image(m_size); 1219 for (i=0; i<m_size.x; i++) 1220 for (j=0; j<m_size.y; j++) 1232 1221 { 1233 1222 for (t=0, k=-smoothness; k<=smoothness; k++) 1234 1223 for (l=-smoothness; l<=smoothness; l++) 1235 if (i+k>smoothness && i+k< size.x-smoothness && j+l<size.y-smoothness && j+l>smoothness)1224 if (i+k>smoothness && i+k<m_size.x-smoothness && j+l<m_size.y-smoothness && j+l>smoothness) 1236 1225 t+=pixel(i+k, j+l); 1237 1226 else t+=pixel(i, j); … … 1290 1279 } 1291 1280 } 1292 if (y< size.y-1)1281 if (y<m_size.y-1) 1293 1282 { 1294 1283 above=scan_line(y+1); … … 1311 1300 } 1312 1301 } 1313 if (y< size.y-1)1302 if (y<m_size.y-1) 1314 1303 { below=scan_line(y+1); 1315 1304 if (x>0 && below[x-1]!=fcolor && below[x]==fcolor) … … 1319 1308 } 1320 1309 x++; 1321 } while (sl[x]==fcolor && x< size.x);1310 } while (sl[x]==fcolor && x<m_size.x); 1322 1311 x--; 1323 1312 if (y>0) … … 1329 1318 } 1330 1319 } 1331 if (y< size.y-1)1320 if (y<m_size.y-1) 1332 1321 { 1333 1322 above=scan_line(y+1); … … 1389 1378 CHECK(x2>=x1 && y2>=y1); 1390 1379 if (x2<x1 || y2<y1) return NULL; 1391 ret=new image( (x2-x1+8)/8, (y2-y1+1));1380 ret=new image(vec2i((x2-x1+8)/8, (y2-y1+1))); 1392 1381 if (!last_loaded()) 1393 1382 ret->clear(); … … 1424 1413 void image::flip_x() 1425 1414 { 1426 uint8_t *rev=(uint8_t *)malloc( size.x), *sl;1415 uint8_t *rev=(uint8_t *)malloc(m_size.x), *sl; 1427 1416 CONDITION(rev, "memory allocation"); 1428 1417 int y, x, i; … … 1430 1419 /* FIXME: Abuse Win32 uses RestoreSurface() here instead of locking */ 1431 1420 lock(); 1432 for (y=0; y< size.y; y++)1421 for (y=0; y<m_size.y; y++) 1433 1422 { sl=scan_line(y); 1434 for (i=0, x= size.x-1; x>=0; x--, i++)1423 for (i=0, x=m_size.x-1; x>=0; x--, i++) 1435 1424 rev[i]=sl[x]; 1436 memcpy(sl, rev, size.x);1425 memcpy(sl, rev, m_size.x); 1437 1426 } 1438 1427 unlock(); … … 1442 1431 void image::flip_y() 1443 1432 { 1444 uint8_t *rev=(uint8_t *)malloc( size.x), *sl;1433 uint8_t *rev=(uint8_t *)malloc(m_size.x), *sl; 1445 1434 CONDITION(rev, "memory allocation"); 1446 1435 int y; … … 1448 1437 /* FIXME: Abuse Win32 uses RestoreSurface() here instead of locking */ 1449 1438 lock(); 1450 for (y=0; y< size.y/2; y++)1439 for (y=0; y<m_size.y/2; y++) 1451 1440 { sl=scan_line(y); 1452 memcpy(rev, sl, size.x);1453 memcpy(sl, scan_line( size.y-y-1),size.x);1454 memcpy(scan_line( size.y-y-1), rev,size.x);1441 memcpy(rev, sl, m_size.x); 1442 memcpy(sl, scan_line(m_size.y-y-1), m_size.x); 1443 memcpy(scan_line(m_size.y-y-1), rev, m_size.x); 1455 1444 } 1456 1445 unlock(); … … 1463 1452 int y, x; 1464 1453 lock(); 1465 for (y=0; y< size.y; y++)1454 for (y=0; y<m_size.y; y++) 1466 1455 { 1467 1456 sl=scan_line(y); 1468 for (x= size.x; x; x--, sl++)1457 for (x=m_size.x; x; x--, sl++) 1469 1458 if (*sl) 1470 1459 *sl=color;
Note: See TracChangeset
for help on using the changeset viewer.