| 195 | |
| 196 | |
| 197 | int msClassifyRasterBuffer(rasterBufferObj *rb, rasterBufferObj *qrb) { |
| 198 | register int ind; |
| 199 | unsigned char *outrow,*pQ; |
| 200 | register rgbaPixel *pP; |
| 201 | acolorhash_table acht; |
| 202 | int usehash, row, col; |
| 203 | /* |
| 204 | ** Step 4: map the colors in the image to their closest match in the |
| 205 | ** new colormap, and write 'em out. |
| 206 | */ |
| 207 | acht = pam_allocacolorhash( ); |
| 208 | usehash = 1; |
| 209 | |
| 210 | for ( row = 0; row < qrb->height; ++row ) { |
| 211 | outrow = &(qrb->data.palette.pixels[row*qrb->width]); |
| 212 | col = 0; |
| 213 | pP = (rgbaPixel*)(&(rb->data.rgba.pixels[row * rb->data.rgba.row_step]));; |
| 214 | pQ = outrow; |
| 215 | do { |
| 216 | /* Check hash table to see if we have already matched this color. */ |
| 217 | ind = pam_lookupacolor( acht, pP ); |
| 218 | if ( ind == -1 ) { |
| 219 | /* No; search acolormap for closest match. */ |
| 220 | register int i, r1, g1, b1, a1, r2, g2, b2, a2; |
| 221 | register long dist, newdist; |
| 222 | |
| 223 | r1 = PAM_GETR( *pP ); |
| 224 | g1 = PAM_GETG( *pP ); |
| 225 | b1 = PAM_GETB( *pP ); |
| 226 | a1 = PAM_GETA( *pP ); |
| 227 | dist = 2000000000; |
| 228 | for ( i = 0; i < qrb->data.palette.num_entries; ++i ) { |
| 229 | r2 = PAM_GETR( qrb->data.palette.palette[i] ); |
| 230 | g2 = PAM_GETG( qrb->data.palette.palette[i] ); |
| 231 | b2 = PAM_GETB( qrb->data.palette.palette[i] ); |
| 232 | a2 = PAM_GETA( qrb->data.palette.palette[i] ); |
| 233 | /* GRR POSSIBLE BUG */ |
| 234 | newdist = ( r1 - r2 ) * ( r1 - r2 ) + /* may overflow? */ |
| 235 | ( g1 - g2 ) * ( g1 - g2 ) + |
| 236 | ( b1 - b2 ) * ( b1 - b2 ) + |
| 237 | ( a1 - a2 ) * ( a1 - a2 ); |
| 238 | if ( newdist < dist ) { |
| 239 | ind = i; |
| 240 | dist = newdist; |
| 241 | } |
| 242 | } |
| 243 | if ( usehash ) { |
| 244 | if ( pam_addtoacolorhash( acht, pP, ind ) < 0 ) { |
| 245 | usehash = 0; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* *pP = acolormap[ind].acolor; */ |
| 251 | *pQ = (unsigned char)ind; |
| 252 | |
| 253 | ++col; |
| 254 | ++pP; |
no test coverage detected