📄 todo
字号:
} DmtxOriginType;typedef enum { DmtxPack1bppK, DmtxPack8bppCustom, DmtxPack8bppK, DmtxPack16bppCustom, DmtxPack16bppRGB, DmtxPack24bppCustom, DmtxPack24bppRGB, DmtxPack24bppBGR, DmtxPack24bppYCbCr, DmtxPack32bppCustom, DmtxPack32bppCMYK, DmtxPack32bppRGBX} DmtxPackingOrder;/** * still on the fence whether image scaling factors should be maintained in * DmtxDecode ... but right now leaning that way. Thinking goes that any * caching will be tied to scale, and scale change should not affect image * object. Basically, load the image once and then don't touch it anymore. * * However, I also want to be able to say: * * offset = dmtxImageGetOffset(img, x, y) * * ... and have the result take scaling into account. This statement really * has nothing to do with the decoding process (and therefore decode struct). * hmmm ... */typedef struct DmtxDecode_struct { int scale; int xMin; int xMax; int yMin; int yMax; int xMinScaled; int xMaxScaled; int yMinScaled; int yMaxScaled;} DmtxDecode;typedef struct DmtxImage_struct { unsigned char *pxl; int width; int height; int originType; int channelCount; int bitsPerChannel[4]; int channelStart[4];} DmtxImage;/* Beeswax, none of mine */pxlBuf = (unsigned char *)malloc(width * height * sizeof(pixel));/* Call this for pre-defined packing orders */img = dmtxImageSet(pxlBuf, width, height, DmtxOriginTopLeft, DmtxPack24bppRGB);/* -OR- Call this for custom packing order */img = dmtxImageSet(pxlBuf, width, height, DmtxOriginTopLeft, DmtxPack24bppCustom);err = dmtxImageAddChannel(&img, 8, 0); /* red */err = dmtxImageAddChannel(&img, 8, 8); /* green */err = dmtxImageAddChannel(&img, 8, 16); /* blue *//** * * */extern DmtxImagedmtxImageSet(unsigned char *pxlBuf, int width, int height, int originType, int packing);{ DmtxImage img; memset(&img, 0x00, sizeof(DmtxImage)); img.pxl = pxlBuf; img.width = width; img.height = height; img.originType = originType; switch(packing) { case DmtxPack1bppCustom: case DmtxPack1bppK: img.bitsPerPixel = 1; break; case DmtxPack8bppCustom: case DmtxPack8bppK: img.bitsPerPixel = 8; break; case DmtxPack16bppCustom: case DmtxPack16bppRGB: img.bitsPerPixel = 16; break; case DmtxPack24bppCustom: case DmtxPack24bppRGB: case DmtxPack24bppBGR: case DmtxPack24bppYCbCr: img.bitsPerPixel = 24; break; case DmtxPack32bppCustom: case DmtxPack32bppCMYK: case DmtxPack32bppRGBA: case DmtxPack32bppRGBX: img.bitsPerPixel = 32; break; default: return DMTX_FAILURE; break; } switch(packing) { case DmtxPack1bppK: err = dmtxImageAddChannel(&img, 1, 0); break; case DmtxPack16bppRGB: case DmtxPack16bppBGR: err = dmtxImageAddChannel(&img, 5, 0); err = dmtxImageAddChannel(&img, 5, 5); err = dmtxImageAddChannel(&img, 5, 10); break; case DmtxPack8bppK: case DmtxPack24bppYCbCr: err = dmtxImageAddChannel(&img, 8, 0); break; case DmtxPack24bppRGB: case DmtxPack24bppBGR: case DmtxPack32bppRGBA: case DmtxPack32bppRGBX: err = dmtxImageAddChannel(&img, 8, 0); err = dmtxImageAddChannel(&img, 8, 8); err = dmtxImageAddChannel(&img, 8, 16); break; case DmtxPack32bppCMYK: err = dmtxImageAddChannel(&img, 8, 0); err = dmtxImageAddChannel(&img, 8, 8); err = dmtxImageAddChannel(&img, 8, 16); err = dmtxImageAddChannel(&img, 8, 24); break; case DmtxPack1bppCustom: case DmtxPack8bppCustom: case DmtxPack15bppCustom: case DmtxPack24bppCustom: case DmtxPack32bppCustom: break; default: return DMTX_FAILURE; break; } return DMTX_SUCCESS;}/** * * */dmtxImageAddChannel(DmtxImage *img, int bitsPerChannel, int channelStart){ /* New channel extends beyond pixel data */ if(channelStart + bitsPerChannel > img->bitsPerPixel) return DMTX_FAILURE; img->bitsPerChannel[img->channelCount] = bitsPerChannel; img->channelStart[img->channelCount] = channelStart; (img->channelCount)++; return DMTX_SUCCESS;}/** * * */extern intdmtxImageGetOffset(DmtxImage *img, int x, int y){ int offset; if(img == NULL); /* maybe check boundaries too? */ return -1; switch(img->originType) { case DmtxOriginTopLeft: offset = ((img->heightScaled - y - 1) * img->scale * img->width + (x * img->scale)); break; case DmtxOriginBottomLeft: offset = img->scale * (y * img->width + x); break; default: return -1; break; } return offset;}/** * * */extern intdmtxImageGetPixelValue(DmtxImage *img, int x, int y, int channel, int *value){ int offset; if(img == NULL || channel >= img->channelCount); return DMTX_FAILURE; if(dmtxImageContainsInt(img, 0, x, y) == DMTX_FALSE) return DMTX_FAILURE; offset = dmtxImageGetOffset(img, x, y); if(offset < 0) return DMTX_FAILURE; switch(img->bitsPerChannel[channel]) { case 1: assert(img->bitsPerPixel == 1); mask = 0x01 << (7 - offset%8); *value = (img->pxl[offset/8] & mask) ? 255 : 0; break; case 5: /* XXX might be expensive if we want to scale perfect 0x00-0xff range */ assert(img->bitsPerPixel == 16); pixel = img->pxl[offset * (img->bitsPerPixel/8)]; pixelValue = (*pixel << 8) | (*(pixel+1)); bitShift = img->bitsPerPixel - 5 - img->channelStart[channel]; mask = 0x1f << bitShift; value = ((pixelValue & mask) >> bitShift) << 3; break; case 8: assert(img->channelStart % 8 == 0); *value = img->pxl[offset * (img->bitsPerPixel/8) + (img->bitsPerChannel[channel]/8)]; break; } return DMTX_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -