#include #include #include "interp.h" #include "isa.h" #include "runt.h" #include "raise.h" #include "image.h" #include "drawif.h" #define DEB (0) #define DPRINT if(DEB) print uchar closest[] = { 255, 255, 255, 254, 254, 237, 220, 203, 253, 236, 219, 202, 252, 235, 218, 201, 255, 255, 255, 254, 254, 237, 220, 203, 253, 236, 219, 202, 252, 235, 218, 201, 255, 255, 255, 250, 250, 250, 220, 249, 249, 249, 232, 248, 248, 248, 231, 201, 251, 251, 250, 250, 250, 250, 249, 249, 249, 249, 232, 248, 248, 248, 231, 201, 251, 251, 250, 250, 250, 233, 233, 249, 249, 232, 215, 215, 248, 231, 214, 197, 234, 234, 250, 250, 233, 233, 216, 216, 249, 232, 215, 198, 198, 231, 214, 197, 217, 217, 217, 246, 233, 216, 216, 199, 199, 215, 215, 198, 198, 198, 214, 197, 200, 200, 246, 246, 246, 216, 199, 199, 245, 245, 198, 244, 244, 244, 227, 197, 247, 247, 246, 246, 246, 246, 199, 245, 245, 245, 228, 244, 244, 244, 227, 193, 230, 230, 246, 246, 229, 229, 212, 245, 245, 228, 228, 211, 244, 227, 210, 193, 213, 213, 229, 229, 212, 212, 212, 195, 228, 228, 211, 211, 194, 227, 210, 193, 196, 196, 242, 242, 212, 195, 195, 241, 241, 211, 211, 194, 194, 240, 210, 193, 243, 243, 242, 242, 242, 195, 195, 241, 241, 241, 194, 194, 240, 240, 239, 205, 226, 226, 242, 242, 225, 225, 195, 241, 241, 224, 224, 240, 240, 239, 239, 205, 209, 209, 225, 225, 208, 208, 208, 224, 224, 223, 223, 223, 239, 239, 222, 205, 192, 192, 192, 192, 207, 207, 207, 207, 206, 206, 206, 206, 205, 205, 205, 205, 255, 255, 255, 254, 254, 237, 220, 203, 253, 236, 219, 202, 252, 235, 218, 201, 255, 238, 221, 221, 254, 237, 220, 203, 253, 236, 219, 202, 252, 235, 218, 201, 255, 221, 221, 221, 204, 250, 220, 249, 249, 249, 232, 248, 248, 248, 231, 201, 251, 221, 221, 204, 250, 250, 249, 249, 249, 249, 232, 248, 248, 248, 231, 201, 251, 251, 204, 250, 250, 233, 233, 249, 249, 232, 215, 215, 248, 231, 214, 197, 234, 234, 250, 250, 233, 233, 216, 216, 249, 232, 215, 198, 198, 231, 214, 197, 217, 217, 217, 246, 233, 216, 216, 199, 199, 215, 215, 198, 198, 198, 214, 197, 200, 200, 246, 246, 246, 216, 199, 199, 245, 245, 198, 244, 244, 244, 227, 197, 247, 247, 246, 246, 246, 246, 199, 245, 245, 245, 228, 244, 244, 244, 227, 193, 230, 230, 246, 246, 229, 229, 212, 245, 245, 228, 228, 211, 244, 227, 210, 193, 213, 213, 229, 229, 212, 212, 212, 195, 228, 228, 211, 211, 194, 227, 210, 193, 196, 196, 242, 242, 212, 195, 195, 241, 241, 211, 211, 194, 194, 240, 210, 193, 243, 243, 242, 242, 242, 195, 195, 241, 241, 241, 194, 194, 240, 240, 239, 205, 226, 226, 242, 242, 225, 225, 195, 241, 241, 224, 224, 240, 240, 239, 239, 205, 209, 209, 225, 225, 208, 208, 208, 224, 224, 223, 223, 223, 239, 239, 222, 205, 192, 192, 192, 192, 207, 207, 207, 207, 206, 206, 206, 206, 205, 205, 205, 205, 255, 255, 255, 191, 191, 191, 220, 190, 190, 190, 173, 189, 189, 189, 172, 201, 255, 221, 221, 221, 204, 191, 220, 190, 190, 190, 173, 189, 189, 189, 172, 201, 255, 221, 221, 204, 204, 204, 186, 186, 186, 186, 186, 185, 185, 185, 168, 201, 188, 221, 204, 204, 204, 187, 186, 186, 186, 186, 232, 185, 185, 185, 168, 201, 188, 204, 204, 204, 187, 187, 186, 186, 186, 186, 169, 185, 185, 185, 168, 197, 188, 188, 204, 187, 187, 233, 216, 186, 186, 186, 215, 185, 185, 185, 168, 197, 217, 217, 183, 183, 183, 216, 216, 199, 182, 182, 215, 198, 198, 181, 214, 197, 184, 184, 183, 183, 183, 183, 199, 182, 182, 182, 182, 181, 181, 181, 181, 197, 184, 184, 183, 183, 183, 183, 182, 182, 182, 182, 182, 181, 181, 181, 164, 193, 184, 184, 183, 183, 183, 183, 182, 182, 182, 228, 165, 181, 181, 164, 164, 193, 167, 167, 183, 229, 166, 212, 212, 182, 182, 165, 211, 211, 181, 164, 210, 193, 180, 180, 179, 179, 179, 179, 195, 178, 178, 178, 211, 194, 177, 177, 177, 193, 180, 180, 179, 179, 179, 179, 195, 178, 178, 178, 178, 177, 177, 177, 177, 205, 180, 180, 179, 179, 179, 179, 178, 178, 178, 161, 161, 177, 177, 177, 160, 205, 163, 163, 162, 162, 162, 162, 208, 178, 161, 161, 223, 177, 177, 160, 160, 205, 192, 192, 192, 192, 207, 207, 207, 207, 206, 206, 206, 206, 205, 205, 205, 205, 176, 176, 191, 191, 191, 191, 190, 190, 190, 190, 173, 189, 189, 189, 172, 201, 176, 221, 221, 204, 191, 191, 190, 190, 190, 190, 173, 189, 189, 189, 172, 201, 188, 221, 204, 204, 204, 187, 186, 186, 186, 186, 173, 185, 185, 185, 168, 201, 188, 204, 204, 204, 187, 187, 186, 186, 186, 186, 169, 185, 185, 185, 168, 201, 188, 188, 204, 187, 187, 187, 186, 186, 186, 186, 169, 185, 185, 185, 168, 197, 188, 188, 187, 187, 187, 170, 170, 186, 186, 169, 169, 185, 185, 168, 168, 197, 184, 184, 183, 183, 183, 170, 170, 182, 182, 169, 152, 152, 181, 168, 151, 197, 184, 184, 183, 183, 183, 183, 182, 182, 182, 182, 182, 181, 181, 181, 164, 197, 184, 184, 183, 183, 183, 183, 182, 182, 182, 182, 165, 181, 181, 181, 164, 193, 184, 184, 183, 183, 183, 166, 166, 182, 182, 165, 165, 181, 181, 164, 164, 193, 167, 167, 167, 166, 166, 166, 149, 182, 165, 165, 165, 148, 181, 164, 147, 193, 180, 180, 179, 179, 179, 179, 149, 178, 178, 178, 148, 177, 177, 177, 147, 193, 180, 180, 179, 179, 179, 179, 178, 178, 178, 178, 178, 177, 177, 177, 160, 205, 180, 180, 179, 179, 179, 162, 162, 178, 178, 161, 161, 177, 177, 160, 160, 205, 163, 163, 162, 162, 162, 162, 145, 161, 161, 161, 144, 144, 160, 160, 160, 205, 192, 192, 192, 192, 207, 207, 207, 207, 206, 206, 206, 206, 205, 205, 205, 205, 176, 176, 191, 191, 191, 174, 174, 190, 190, 173, 156, 156, 189, 172, 155, 138, 176, 176, 204, 191, 191, 174, 174, 190, 190, 173, 156, 156, 189, 172, 155, 138, 188, 204, 204, 204, 187, 187, 186, 186, 186, 186, 169, 185, 185, 185, 168, 138, 188, 188, 204, 187, 187, 187, 186, 186, 186, 186, 169, 185, 185, 185, 168, 138, 188, 188, 187, 187, 187, 170, 170, 186, 186, 169, 169, 185, 185, 168, 151, 134, 171, 171, 187, 187, 170, 170, 170, 186, 186, 169, 152, 152, 185, 168, 151, 134, 171, 171, 183, 183, 170, 170, 170, 153, 182, 169, 152, 135, 135, 168, 151, 134, 184, 184, 183, 183, 183, 183, 153, 182, 182, 182, 182, 181, 181, 181, 164, 134, 184, 184, 183, 183, 183, 183, 182, 182, 182, 182, 165, 181, 181, 181, 164, 130, 167, 167, 183, 183, 166, 166, 166, 182, 182, 165, 165, 181, 181, 164, 147, 130, 150, 150, 166, 166, 166, 149, 149, 182, 165, 165, 148, 148, 164, 164, 147, 130, 150, 150, 179, 179, 179, 149, 132, 178, 178, 178, 148, 131, 177, 177, 147, 130, 180, 180, 179, 179, 179, 179, 132, 178, 178, 178, 161, 177, 177, 177, 160, 142, 163, 163, 179, 179, 162, 162, 162, 178, 178, 161, 161, 177, 177, 160, 160, 142, 146, 146, 162, 162, 145, 145, 145, 161, 161, 144, 144, 144, 160, 160, 159, 142, 129, 129, 129, 129, 128, 128, 128, 128, 143, 143, 143, 143, 142, 142, 142, 142, 175, 175, 191, 191, 174, 174, 157, 157, 190, 173, 156, 139, 139, 172, 155, 138, 175, 175, 191, 191, 174, 174, 157, 157, 190, 173, 156, 139, 139, 172, 155, 138, 188, 188, 204, 187, 187, 187, 157, 186, 186, 186, 156, 185, 185, 185, 168, 138, 188, 188, 187, 187, 187, 170, 170, 186, 186, 169, 169, 185, 185, 168, 168, 138, 171, 171, 187, 187, 170, 170, 170, 186, 186, 169, 152, 152, 185, 168, 151, 134, 171, 171, 187, 170, 170, 170, 170, 153, 169, 169, 152, 135, 135, 168, 151, 134, 154, 154, 154, 170, 170, 170, 153, 153, 169, 152, 152, 135, 135, 135, 151, 134, 154, 154, 183, 183, 183, 153, 153, 153, 182, 182, 135, 135, 181, 181, 164, 134, 184, 184, 183, 183, 183, 166, 166, 182, 182, 165, 165, 181, 181, 164, 164, 130, 167, 167, 183, 166, 166, 166, 149, 182, 165, 165, 165, 148, 181, 164, 147, 130, 150, 150, 150, 166, 149, 149, 149, 132, 165, 165, 148, 148, 131, 147, 147, 130, 133, 133, 179, 179, 149, 132, 132, 132, 178, 148, 148, 131, 131, 131, 130, 130, 133, 133, 179, 179, 179, 132, 132, 178, 178, 178, 131, 131, 131, 177, 160, 142, 163, 163, 179, 162, 162, 162, 132, 178, 161, 161, 144, 131, 177, 160, 160, 142, 146, 146, 162, 162, 145, 145, 145, 161, 161, 144, 144, 143, 160, 160, 159, 142, 129, 129, 129, 129, 128, 128, 128, 128, 143, 143, 143, 143, 142, 142, 142, 142, 158, 158, 158, 112, 174, 157, 157, 140, 140, 156, 156, 139, 139, 139, 155, 138, 158, 158, 158, 112, 174, 157, 157, 140, 140, 156, 156, 139, 139, 139, 155, 138, 158, 158, 124, 124, 124, 157, 157, 140, 123, 123, 156, 139, 139, 122, 155, 138, 125, 125, 124, 124, 124, 170, 170, 123, 123, 169, 152, 152, 122, 168, 151, 138, 171, 171, 124, 124, 170, 170, 170, 153, 123, 169, 152, 135, 135, 168, 151, 134, 154, 154, 154, 170, 170, 170, 153, 153, 169, 152, 152, 135, 135, 135, 151, 134, 154, 154, 154, 170, 170, 153, 153, 153, 136, 152, 135, 135, 135, 135, 134, 134, 137, 137, 137, 120, 153, 153, 153, 136, 136, 136, 135, 135, 135, 118, 164, 134, 137, 137, 120, 120, 120, 166, 136, 136, 136, 165, 165, 118, 118, 164, 147, 130, 150, 150, 120, 166, 166, 149, 149, 136, 165, 165, 148, 148, 118, 164, 147, 130, 150, 150, 150, 149, 149, 149, 132, 132, 165, 148, 148, 131, 131, 147, 147, 130, 133, 133, 133, 149, 132, 132, 132, 132, 115, 148, 131, 131, 131, 131, 130, 130, 133, 133, 133, 116, 132, 132, 132, 132, 115, 115, 131, 131, 131, 131, 160, 142, 133, 133, 116, 162, 162, 132, 132, 115, 161, 161, 144, 131, 131, 160, 160, 142, 146, 146, 146, 145, 145, 145, 128, 161, 144, 144, 144, 143, 160, 160, 159, 142, 129, 129, 129, 129, 128, 128, 128, 128, 143, 143, 143, 143, 142, 142, 142, 142, 141, 141, 112, 112, 112, 157, 140, 140, 140, 127, 139, 126, 126, 126, 109, 138, 141, 141, 112, 112, 112, 157, 140, 140, 140, 127, 139, 126, 126, 126, 109, 138, 125, 125, 124, 124, 124, 124, 140, 123, 123, 123, 123, 122, 122, 122, 122, 138, 125, 125, 124, 124, 124, 124, 123, 123, 123, 123, 123, 122, 122, 122, 105, 138, 125, 125, 124, 124, 124, 124, 153, 123, 123, 123, 152, 122, 122, 122, 105, 134, 154, 154, 124, 124, 124, 153, 153, 153, 123, 123, 135, 135, 122, 122, 105, 134, 137, 137, 137, 120, 153, 153, 153, 136, 136, 136, 135, 135, 135, 118, 105, 134, 137, 137, 120, 120, 120, 153, 136, 136, 136, 119, 119, 118, 118, 118, 118, 134, 137, 137, 120, 120, 120, 120, 136, 136, 119, 119, 119, 118, 118, 118, 101, 130, 121, 121, 120, 120, 120, 120, 136, 119, 119, 119, 102, 118, 118, 118, 101, 130, 133, 133, 120, 120, 149, 132, 132, 119, 119, 102, 148, 131, 131, 101, 101, 130, 117, 117, 116, 116, 116, 132, 132, 115, 115, 115, 131, 131, 114, 114, 114, 130, 117, 117, 116, 116, 116, 116, 132, 115, 115, 115, 131, 114, 114, 114, 114, 142, 117, 117, 116, 116, 116, 116, 115, 115, 115, 115, 98, 114, 114, 114, 97, 142, 100, 100, 116, 99, 99, 99, 99, 115, 98, 98, 98, 114, 114, 97, 97, 142, 129, 129, 129, 129, 128, 128, 128, 128, 143, 143, 143, 143, 142, 142, 142, 142, 113, 113, 112, 112, 112, 112, 140, 140, 127, 127, 110, 126, 126, 126, 109, 75, 113, 113, 112, 112, 112, 112, 140, 140, 127, 127, 110, 126, 126, 126, 109, 75, 125, 125, 124, 124, 124, 124, 123, 123, 123, 123, 123, 122, 122, 122, 105, 75, 125, 125, 124, 124, 124, 124, 123, 123, 123, 123, 106, 122, 122, 122, 105, 75, 125, 125, 124, 124, 124, 124, 123, 123, 123, 123, 106, 122, 122, 122, 105, 71, 125, 125, 124, 124, 124, 107, 107, 123, 123, 106, 106, 122, 122, 105, 105, 71, 137, 137, 120, 120, 120, 107, 136, 136, 136, 106, 106, 118, 118, 105, 88, 71, 137, 137, 120, 120, 120, 120, 136, 136, 119, 119, 119, 118, 118, 118, 101, 71, 121, 121, 120, 120, 120, 120, 136, 119, 119, 119, 102, 118, 118, 118, 101, 67, 121, 121, 120, 120, 120, 103, 103, 119, 119, 102, 102, 118, 118, 101, 101, 67, 104, 104, 120, 103, 103, 103, 103, 119, 102, 102, 102, 118, 118, 101, 84, 67, 117, 117, 116, 116, 116, 116, 115, 115, 115, 115, 115, 114, 114, 114, 114, 67, 117, 117, 116, 116, 116, 116, 115, 115, 115, 115, 115, 114, 114, 114, 97, 79, 117, 117, 116, 116, 116, 99, 99, 115, 115, 98, 98, 114, 114, 97, 97, 79, 100, 100, 99, 99, 99, 99, 82, 98, 98, 98, 81, 114, 97, 97, 97, 79, 66, 66, 66, 66, 65, 65, 65, 65, 64, 64, 64, 64, 79, 79, 79, 79, 96, 96, 112, 112, 111, 111, 94, 127, 127, 110, 110, 93, 126, 109, 92, 75, 96, 96, 112, 112, 111, 111, 94, 127, 127, 110, 110, 93, 126, 109, 92, 75, 125, 125, 124, 124, 124, 124, 123, 123, 123, 123, 106, 122, 122, 105, 105, 75, 125, 125, 124, 124, 124, 107, 107, 123, 123, 106, 106, 122, 122, 105, 105, 75, 108, 108, 124, 124, 107, 107, 107, 123, 123, 106, 106, 122, 122, 105, 88, 71, 108, 108, 124, 107, 107, 107, 90, 123, 106, 106, 106, 89, 122, 105, 88, 71, 91, 91, 120, 107, 107, 90, 90, 136, 106, 106, 89, 89, 118, 105, 88, 71, 121, 121, 120, 120, 120, 120, 136, 119, 119, 119, 102, 118, 118, 118, 101, 71, 121, 121, 120, 120, 120, 103, 103, 119, 119, 102, 102, 118, 118, 101, 101, 67, 104, 104, 120, 103, 103, 103, 103, 119, 102, 102, 102, 118, 118, 101, 84, 67, 104, 104, 103, 103, 103, 103, 86, 102, 102, 102, 85, 85, 101, 101, 84, 67, 87, 87, 116, 116, 116, 86, 86, 115, 115, 115, 85, 85, 114, 114, 84, 67, 117, 117, 116, 116, 116, 116, 115, 115, 115, 115, 98, 114, 114, 114, 97, 79, 100, 100, 99, 99, 99, 99, 99, 115, 98, 98, 98, 114, 114, 97, 97, 79, 83, 83, 99, 99, 82, 82, 82, 98, 98, 81, 81, 81, 97, 97, 80, 79, 66, 66, 66, 66, 65, 65, 65, 65, 64, 64, 64, 64, 79, 79, 79, 79, 95, 95, 111, 111, 94, 94, 94, 77, 110, 110, 93, 93, 76, 109, 92, 75, 95, 95, 111, 111, 94, 94, 94, 77, 110, 110, 93, 93, 76, 109, 92, 75, 108, 108, 124, 111, 107, 94, 94, 123, 123, 106, 93, 93, 122, 105, 92, 75, 108, 108, 108, 107, 107, 107, 90, 123, 106, 106, 106, 89, 122, 105, 88, 75, 91, 91, 107, 107, 107, 90, 90, 123, 106, 106, 89, 89, 105, 105, 88, 71, 91, 91, 91, 107, 90, 90, 90, 73, 106, 106, 89, 89, 72, 88, 88, 71, 91, 91, 91, 90, 90, 90, 73, 73, 106, 89, 89, 72, 72, 88, 88, 71, 74, 74, 120, 120, 120, 73, 73, 119, 119, 102, 89, 72, 72, 101, 101, 71, 104, 104, 120, 103, 103, 103, 103, 119, 102, 102, 102, 118, 118, 101, 84, 67, 104, 104, 103, 103, 103, 103, 86, 102, 102, 102, 85, 85, 101, 101, 84, 67, 87, 87, 87, 103, 86, 86, 86, 86, 102, 85, 85, 85, 85, 84, 84, 67, 87, 87, 87, 86, 86, 86, 69, 69, 115, 85, 85, 85, 68, 68, 67, 67, 70, 70, 116, 116, 99, 69, 69, 69, 115, 98, 85, 68, 68, 97, 97, 79, 100, 100, 99, 99, 99, 82, 82, 98, 98, 98, 81, 68, 97, 97, 97, 79, 83, 83, 83, 82, 82, 82, 82, 98, 81, 81, 81, 64, 97, 97, 80, 79, 66, 66, 66, 66, 65, 65, 65, 65, 64, 64, 64, 64, 79, 79, 79, 79, 78, 78, 49, 49, 94, 77, 77, 48, 48, 93, 93, 76, 76, 63, 92, 75, 78, 78, 49, 49, 94, 77, 77, 48, 48, 93, 93, 76, 76, 63, 92, 75, 62, 62, 61, 61, 61, 61, 77, 60, 60, 60, 93, 76, 59, 59, 59, 75, 62, 62, 61, 61, 61, 61, 90, 60, 60, 60, 89, 59, 59, 59, 88, 75, 91, 91, 61, 61, 61, 90, 73, 60, 60, 60, 89, 72, 59, 59, 88, 71, 74, 74, 61, 61, 90, 73, 73, 73, 60, 89, 89, 72, 72, 72, 71, 71, 74, 74, 74, 90, 73, 73, 73, 73, 56, 89, 72, 72, 72, 72, 71, 71, 58, 58, 57, 57, 57, 73, 73, 56, 56, 56, 72, 72, 55, 55, 55, 71, 58, 58, 57, 57, 57, 57, 56, 56, 56, 56, 56, 55, 55, 55, 55, 67, 87, 87, 57, 57, 57, 86, 86, 56, 56, 56, 85, 85, 55, 55, 84, 67, 87, 87, 87, 86, 86, 86, 69, 69, 56, 85, 85, 85, 68, 68, 67, 67, 70, 70, 70, 53, 69, 69, 69, 69, 52, 85, 85, 68, 68, 68, 67, 67, 70, 70, 53, 53, 53, 69, 69, 52, 52, 52, 68, 68, 68, 51, 51, 79, 54, 54, 53, 53, 53, 69, 69, 52, 52, 52, 68, 68, 51, 51, 80, 79, 83, 83, 53, 82, 82, 65, 65, 52, 52, 81, 64, 64, 51, 80, 80, 79, 66, 66, 66, 66, 65, 65, 65, 65, 64, 64, 64, 64, 79, 79, 79, 79, 50, 50, 49, 49, 49, 77, 77, 48, 48, 48, 76, 76, 63, 63, 46, 12, 50, 50, 49, 49, 49, 77, 77, 48, 48, 48, 76, 76, 63, 63, 46, 12, 62, 62, 61, 61, 61, 61, 77, 60, 60, 60, 60, 59, 59, 59, 59, 12, 62, 62, 61, 61, 61, 61, 60, 60, 60, 60, 60, 59, 59, 59, 42, 12, 62, 62, 61, 61, 61, 61, 73, 60, 60, 60, 43, 59, 59, 59, 42, 8, 74, 74, 61, 61, 61, 73, 73, 60, 60, 60, 72, 72, 72, 59, 42, 8, 74, 74, 74, 57, 73, 73, 73, 73, 56, 56, 72, 72, 72, 72, 42, 8, 58, 58, 57, 57, 57, 57, 73, 56, 56, 56, 72, 55, 55, 55, 55, 8, 58, 58, 57, 57, 57, 57, 56, 56, 56, 56, 56, 55, 55, 55, 38, 4, 58, 58, 57, 57, 57, 57, 56, 56, 56, 56, 39, 55, 55, 55, 38, 4, 70, 70, 57, 57, 40, 69, 69, 69, 56, 39, 85, 68, 68, 38, 38, 4, 70, 70, 53, 53, 53, 69, 69, 52, 52, 52, 68, 68, 68, 51, 51, 4, 54, 54, 53, 53, 53, 69, 69, 52, 52, 52, 68, 68, 51, 51, 51, 0, 54, 54, 53, 53, 53, 53, 69, 52, 52, 52, 35, 51, 51, 51, 34, 0, 37, 37, 53, 36, 36, 36, 36, 52, 35, 35, 35, 51, 51, 34, 34, 0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 33, 33, 49, 49, 32, 32, 77, 48, 48, 47, 47, 63, 63, 46, 46, 12, 33, 33, 49, 49, 32, 32, 77, 48, 48, 47, 47, 63, 63, 46, 46, 12, 62, 62, 61, 61, 61, 61, 60, 60, 60, 43, 43, 59, 59, 59, 42, 12, 62, 62, 61, 61, 61, 44, 44, 60, 60, 43, 43, 59, 59, 42, 42, 12, 45, 45, 61, 61, 44, 44, 44, 60, 60, 43, 43, 59, 59, 42, 42, 8, 45, 45, 61, 44, 44, 44, 73, 60, 43, 43, 26, 72, 59, 42, 42, 8, 74, 74, 57, 44, 44, 73, 73, 56, 43, 43, 26, 72, 72, 42, 42, 8, 58, 58, 57, 57, 57, 57, 56, 56, 56, 56, 39, 55, 55, 55, 38, 8, 58, 58, 57, 57, 57, 40, 40, 56, 56, 39, 39, 55, 55, 38, 38, 4, 41, 41, 40, 40, 40, 40, 40, 56, 39, 39, 39, 55, 55, 38, 38, 4, 41, 41, 40, 40, 40, 23, 23, 39, 39, 39, 22, 68, 38, 38, 38, 4, 54, 54, 53, 53, 53, 69, 69, 52, 52, 52, 68, 68, 51, 51, 21, 4, 54, 54, 53, 53, 53, 53, 69, 52, 52, 52, 35, 51, 51, 51, 34, 0, 37, 37, 53, 36, 36, 36, 36, 52, 35, 35, 35, 51, 51, 34, 34, 0, 37, 37, 36, 36, 36, 36, 36, 35, 35, 35, 35, 18, 34, 34, 34, 0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 16, 16, 32, 32, 31, 31, 31, 47, 47, 30, 30, 30, 46, 46, 29, 12, 16, 16, 32, 32, 31, 31, 31, 47, 47, 30, 30, 30, 46, 46, 29, 12, 45, 45, 44, 44, 44, 44, 31, 60, 43, 43, 30, 59, 59, 42, 42, 12, 45, 45, 44, 44, 44, 44, 27, 43, 43, 43, 26, 26, 42, 42, 42, 12, 28, 28, 44, 44, 27, 27, 27, 43, 43, 26, 26, 26, 42, 42, 25, 8, 28, 28, 44, 44, 27, 27, 27, 43, 43, 26, 26, 9, 42, 42, 25, 8, 28, 28, 28, 27, 27, 27, 10, 43, 26, 26, 26, 9, 42, 42, 25, 8, 41, 41, 57, 40, 40, 40, 40, 56, 39, 39, 39, 55, 55, 38, 38, 8, 41, 41, 40, 40, 40, 40, 23, 39, 39, 39, 22, 55, 38, 38, 38, 4, 24, 24, 40, 40, 23, 23, 23, 39, 39, 22, 22, 22, 38, 38, 21, 4, 24, 24, 24, 23, 23, 23, 23, 39, 22, 22, 22, 5, 38, 38, 21, 4, 24, 24, 53, 23, 23, 6, 6, 52, 52, 22, 5, 5, 51, 21, 21, 4, 37, 37, 53, 36, 36, 36, 36, 52, 35, 35, 35, 51, 51, 34, 34, 0, 37, 37, 36, 36, 36, 36, 36, 35, 35, 35, 35, 18, 34, 34, 34, 0, 20, 20, 36, 36, 19, 19, 19, 35, 35, 18, 18, 18, 34, 34, 17, 0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, }; int *clamp = nil; int *rgbvmap = nil; #define RETERR(msg) { \ DPRINT("remap: %s\n", msg); \ if(ret_str != nil) \ *ret_str = c2string(msg, strlen(msg)); \ if(ret_img != nil) { \ *ret_img = H; \ destroy(*ret_img); \ } \ acquire(); \ return; \ } void remap(Readimage_Rawimage *i, Draw_Display *d, int errdiff, Draw_Image **ret_img, String **ret_str) { int j; Array *pic; Array *cmap; int dx; int dy; ulong rgb; Display *disp; int locked; Image *img; DPRINT("remap: called\n"); if(i == H || i == nil) RETERR("nil Rawimage"); if(d == H || d == nil) RETERR("nil Display"); if(ret_img == nil) RETERR("nil return image\n"); DPRINT("remap: releasing virtual machine\n"); release(); if(clamp == nil){ DPRINT("remap: filling clamp/rgbvmap\n"); clamp = (int*)malloc((64+256+64)*sizeof(int)); for(j=0; j<64; j++) clamp[j] = 0; for(j=0; j<256; j++) clamp[64+j] = (j>>4); for(j=0; j<64; j++) clamp[64+256+j] = (255>>4); rgbvmap = (int*)malloc((3*256)*sizeof(int)); for(j=0; j<256; j++) { rgb = cmap2rgb(j); rgbvmap[3*j+0] = (rgb>>16) & 0xFF; rgbvmap[3*j+1] = (rgb>>8) & 0xFF; rgbvmap[3*j+2] = rgb & 0xFF; } } pic = ((Array**)(i->chans->data))[0]; cmap = i->cmap; dx = i->r.max.x-i->r.min.x; dy = i->r.max.y-i->r.min.y; DPRINT("remap: translating image data (errdiff == %d)\n", errdiff); switch (i->chandesc) { case Readimage_CRGB1: DPRINT("remap: case CRGB1\n"); if(cmap == H) RETERR("image has no color map"); if(i->nchans != 1) { DPRINT("can't handle nchans %d\n", i->nchans); RETERR("bad number of image channels"); } for(j=1; j<=8; j++) if(cmap->len == 3*(1< 8) { DPRINT("can't understand colormap size 3*%d\n", cmap->len/3); RETERR("bad colormap size"); } if(errdiff == 0){ int k = 0; uchar map[256]; memset(map, 0, sizeof(map)); for(j=0; jdata[k+2]>>4)+ /* B */ 16*((cmap->data[k+1]>>4)+ /* G */ 16*(cmap->data[k]>>4))]; /* R */ k += 3; } for(j=0; jlen; j++) pic->data[j] = map[pic->data[j]]; } else { /* modified floyd steinberg, coefficients (1 0) 3/16, (0, 1) 3/16, (1, 1) 7/16 */ int *ered; int *egrn; int *eblu; int p = 0; int x, y, er, eg, eb, in, r, g, b, r1, g1, b1, col, t; ered = (int*)mallocz((dx+1)*sizeof(int), 1); egrn = (int*)mallocz((dx+1)*sizeof(int), 1); eblu = (int*)mallocz((dx+1)*sizeof(int), 1); for(y=0; ydata[p]; r = cmap->data[in+0]+ered[x]; g = cmap->data[in+1]+egrn[x]; b = cmap->data[in+2]+eblu[x]; r1 = clamp[r+64]; g1 = clamp[g+64]; b1 = clamp[b+64]; col = closest[b1+16*(g1+16*r1)]; pic->data[p++] = col; col *= 3; r -= rgbvmap[col+0]; t = (3*r)>>4; ered[x] = t+er; ered[x+1] += t; er = r-3*t; g -= rgbvmap[col+1]; t = (3*g)>>4; egrn[x] = t+eg; egrn[x+1] += t; eg = g-3*t; b -= rgbvmap[col+2]; t = (3*b)>>4; eblu[x] = t+eb; eblu[x+1] += t; eb = b-3*t; } } free(ered); free(egrn); free(eblu); } break; case Readimage_CRGB: DPRINT("remap: case CRGB\n"); { Array *rpic, *gpic, *bpic; if(i->nchans != 3 || i->chans->len != 3) { DPRINT("RGB image has %d (%d) channels\n", i->nchans, i->chans->len); RETERR("RGB image has bad number of channels"); } rpic = ((Array**)(i->chans->data))[0]; gpic = ((Array**)(i->chans->data))[1]; bpic = ((Array**)(i->chans->data))[2]; if(errdiff == 0){ int r, g, b; for(j=0; jlen; j++){ r = rpic->data[j]>>4; g = gpic->data[j]>>4; b = bpic->data[j]>>4; pic->data[j] = closest[b+16*(g+16*r)]; } } else { /* modified floyd steinberg, coefficients (1 0) 3/16, (0, 1) 3/16, (1, 1) 7/16 */ int p = 0; int x, y, er, eg, eb, r, g, b, r1, g1, b1, col, t; int *ered, *egrn, *eblu; ered = (int*)malloc((dx+1)*sizeof(int)); egrn = (int*)malloc((dx+1)*sizeof(int)); eblu = (int*)malloc((dx+1)*sizeof(int)); for(y=0; ydata[p]+ered[x]; g = gpic->data[p]+egrn[x]; b = bpic->data[p]+eblu[x]; r1 = clamp[r+64]; g1 = clamp[g+64]; b1 = clamp[b+64]; col = closest[b1+16*(g1+16*r1)]; pic->data[p++] = col; col *= 3; r -= rgbvmap[col+0]; t = (3*r)>>4; ered[x] = t+er; ered[x+1] += t; er = r-3*t; g -= rgbvmap[col+1]; t = (3*g)>>4; egrn[x] = t+eg; egrn[x+1] += t; eg = g-3*t; b -= rgbvmap[col+2]; t = (3*b)>>4; eblu[x] = t+eb; eblu[x+1] += t; eb = b-3*t; } } free(ered); free(egrn); free(eblu); } } /* case */ break; case Readimage_CY: DPRINT("remap: case CY\n"); { int r; Array *rpic; if(i->nchans != 1) { DPRINT("Y image has %d chans\n", i->nchans); RETERR("Y image has bad number of chans"); } rpic = ((Array**)(i->chans->data))[0]; if(errdiff == 0){ for(j=0; jlen; j++){ r = rpic->data[j]>>4; pic->data[j] = closest[r+16*(r+16*r)]; } } else { /* modified floyd steinberg, coefficients (1 0) 3/16, (0, 1) 3/16, (1, 1) 7/16 */ int *ered; int p = 0; int y, er, x, r1, col, t; ered = (int*)malloc((dx+1)*sizeof(int)); for(y=0; ydata[p]+ered[x]; r1 = clamp[r+64]; col = closest[r1+16*(r1+16*r1)]; pic->data[p++] = col; col *= 3; r -= rgbvmap[col+0]; t = (3*r)>>4; ered[x] = t+er; ered[x+1] += t; er = r-3*t; } } } } /* CY */ case Readimage_CINF: DPRINT("remap: case CINF\n"); break; } /* switch */ DPRINT("remap: acquiring virtual machine\n"); acquire(); DPRINT("remap: allocating image\n"); /* from Display_newimage */ disp = checkdisplay(d); locked = lockdisplay(disp, 0); *ret_img = allocdrawimage((DDisplay*)d, i->r, 3, nil, 0, 0); if(locked) unlockdisplay(disp); if(*ret_img == H) RETERR("unable to allocate image"); DPRINT("remap: writing image data\n"); img = checkimage(*ret_img); locked = lockdisplay(img->display, 0); loadimage(img, img->r, pic->data, pic->len); if(locked) unlockdisplay(img->display); DPRINT("remap: returning\n"); }