JPEG Quantization for CbCr channels Args: rounding(function): rounding function to use
| 176 | |
| 177 | |
| 178 | class CQuantize(nn.Module): |
| 179 | """ JPEG Quantization for CbCr channels |
| 180 | |
| 181 | Args: |
| 182 | rounding(function): rounding function to use |
| 183 | """ |
| 184 | |
| 185 | def __init__(self, rounding): |
| 186 | super(CQuantize, self).__init__() |
| 187 | self.rounding = rounding |
| 188 | self.c_table = c_table |
| 189 | |
| 190 | def forward(self, image, factor=1): |
| 191 | """ |
| 192 | Args: |
| 193 | image(tensor): batch x height x width |
| 194 | |
| 195 | Returns: |
| 196 | Tensor: batch x height x width |
| 197 | """ |
| 198 | if isinstance(factor, (int, float)): |
| 199 | image = image.float() / (self.c_table * factor) |
| 200 | else: |
| 201 | b = factor.size(0) |
| 202 | table = self.c_table.expand(b, 1, 8, 8) * factor.view(b, 1, 1, 1) |
| 203 | image = image.float() / table |
| 204 | image = self.rounding(image) |
| 205 | return image |
| 206 | |
| 207 | |
| 208 | class CompressJpeg(nn.Module): |