summaryrefslogtreecommitdiff
path: root/src/py/cam/gl_helpers.py
diff options
context:
space:
mode:
authorAndrey Konovalov <andrey.konovalov@linaro.org>2024-04-16 11:13:46 +0200
committerKieran Bingham <kieran.bingham@ideasonboard.com>2024-04-16 13:00:21 +0100
commitc987946e43fda6ccdc21464243880850e7453a91 (patch)
tree3f0023303632193be373d20663620b597fe58d6b /src/py/cam/gl_helpers.py
parent3755d966485d96e76eef7756222baf02dea960a4 (diff)
libcamera: ipa: Add Soft IPA
Define the Soft IPA main and event interfaces, add the Soft IPA implementation. The current src/ipa/meson.build assumes the IPA name to match the pipeline name. For this reason "-Dipas=simple" is used for the Soft IPA module. Auto exposure/gain and AWB implementation by Dennis, Toon and Martti. Auto exposure/gain targets a Mean Sample Value of 2.5 following the MSV calculation algorithm from: https://www.araa.asn.au/acra/acra2007/papers/paper84final.pdf Use CameraSensorHelper to convert the analogue gain code read from the camera sensor into real analogue gain value. In the future this makes it possible to use faster AE/AGC algorithm. Right now the CameraSensorHelper lets us use the full range of analogue gain values. If there is no CameraSensorHelper for the camera sensor in use, a warning log message is printed. Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s Tested-by: Pavel Machek <pavel@ucw.cz> Reviewed-by: Pavel Machek <pavel@ucw.cz> Signed-off-by: Andrey Konovalov <andrey.konovalov@linaro.org> Co-developed-by: Dennis Bonke <admin@dennisbonke.com> Signed-off-by: Dennis Bonke <admin@dennisbonke.com> Co-developed-by: Marttico <g.martti@gmail.com> Signed-off-by: Marttico <g.martti@gmail.com> Co-developed-by: Toon Langendam <t.langendam@gmail.com> Signed-off-by: Toon Langendam <t.langendam@gmail.com> Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'src/py/cam/gl_helpers.py')
0 files changed, 0 insertions, 0 deletions
6 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (C) 2019-2020, Raspberry Pi Ltd
#
# camera tuning tool image loading

from ctt_tools import *
from ctt_macbeth_locator import *
import json
import pyexiv2 as pyexif
import rawpy as raw


"""
Image class load image from raw data and extracts metadata.

Once image is extracted from data, it finds 24 16x16 patches for each
channel, centred at the macbeth chart squares
"""
class Image:
    def __init__(self, buf):
        self.buf = buf
        self.patches = None
        self.saturated = False

    '''
    obtain metadata from buffer
    '''
    def get_meta(self):
        self.ver = ba_to_b(self.buf[4:5])
        self.w = ba_to_b(self.buf[0xd0:0xd2])
        self.h = ba_to_b(self.buf[0xd2:0xd4])
        self.pad = ba_to_b(self.buf[0xd4:0xd6])
        self.fmt = self.buf[0xf5]
        self.sigbits = 2*self.fmt + 4
        self.pattern = self.buf[0xf4]
        self.exposure = ba_to_b(self.buf[0x90:0x94])
        self.againQ8 = ba_to_b(self.buf[0x94:0x96])
        self.againQ8_norm = self.againQ8/256
        camName = self.buf[0x10:0x10+128]
        camName_end = camName.find(0x00)
        self.camName = self.buf[0x10:0x10+128][:camName_end].decode()

        """
        Channel order depending on bayer pattern
        """
        bayer_case = {
            0: (0, 1, 2, 3),   # red
            1: (2, 0, 3, 1),   # green next to red
            2: (3, 2, 1, 0),   # green next to blue
            3: (1, 0, 3, 2),   # blue
            128: (0, 1, 2, 3)  # arbitrary order for greyscale casw
        }
        self.order = bayer_case[self.pattern]

        '''
        manual blacklevel - not robust
        '''
        if 'ov5647' in self.camName:
            self.blacklevel = 16
        else:
            self.blacklevel = 64
        self.blacklevel_16 = self.blacklevel << (6)
        return 1

    '''
    print metadata for debug
    '''
    def print_meta(self):
        print('\nData:')
        print('      ver = {}'.format(self.ver))
        print('      w = {}'.format(self.w))
        print('      h = {}'.format(self.h))
        print('      pad = {}'.format(self.pad))
        print('      fmt = {}'.format(self.fmt))
        print('      sigbits = {}'.format(self.sigbits))
        print('      pattern = {}'.format(self.pattern))
        print('      exposure = {}'.format(self.exposure))
        print('      againQ8 = {}'.format(self.againQ8))
        print('      againQ8_norm = {}'.format(self.againQ8_norm))
        print('      camName = {}'.format(self.camName))
        print('      blacklevel = {}'.format(self.blacklevel))
        print('      blacklevel_16 = {}'.format(self.blacklevel_16))

        return 1

    """
    get image from raw scanline data
    """
    def get_image(self, raw):
        self.dptr = []
        """
        check if data is 10 or 12 bits
        """
        if self.sigbits == 10:
            """
            calc length of scanline
            """
            lin_len = ((((((self.w+self.pad+3)>>2)) * 5)+31)>>5) * 32
            """
            stack scan lines into matrix
            """
            raw = np.array(raw).reshape(-1, lin_len).astype(np.int64)[:self.h, ...]
            """
            separate 5 bits in each package, stopping when w is satisfied
            """
            ba0 = raw[..., 0:5*((self.w+3)>>2):5]
            ba1 = raw[..., 1:5*((self.w+3)>>2):5]
            ba2 = raw[..., 2:5*((self.w+3)>>2):5]
            ba3 = raw[..., 3:5*((self.w+3)>>2):5]
            ba4 = raw[..., 4:5*((self.w+3)>>2):5]
            """
            assemble 10 bit numbers
            """
            ch0 = np.left_shift((np.left_shift(ba0, 2) + (ba4 % 4)), 6)
            ch1 = np.left_shift((np.left_shift(ba1, 2) + (np.right_shift(ba4, 2) % 4)), 6)
            ch2 = np.left_shift((np.left_shift(ba2, 2) + (np.right_shift(ba4, 4) % 4)), 6)
            ch3 = np.left_shift((np.left_shift(ba3, 2) + (np.right_shift(ba4, 6) % 4)), 6)
            """
            interleave bits
            """
            mat = np.empty((self.h, self.w), dtype=ch0.dtype)

            mat[..., 0::4] = ch0
            mat[..., 1::4] = ch1
            mat[..., 2::4] = ch2
            mat[..., 3::4] = ch3

            """
            There is som eleaking memory somewhere in the code. This code here
            seemed to make things good enough that the code would run for
            reasonable numbers of images, however this is techincally just a
            workaround. (sorry)
            """
            ba0, ba1, ba2, ba3, ba4 = None, None, None, None, None
            del ba0, ba1, ba2, ba3, ba4
            ch0, ch1, ch2, ch3 = None, None, None, None
            del ch0, ch1, ch2, ch3

            """
        same as before but 12 bit case
        """
        elif self.sigbits == 12:
            lin_len = ((((((self.w+self.pad+1)>>1)) * 3)+31)>>5) * 32
            raw = np.array(raw).reshape(-1, lin_len).astype(np.int64)[:self.h, ...]
            ba0 = raw[..., 0:3*((self.w+1)>>1):3]
            ba1 = raw[..., 1:3*((self.w+1)>>1):3]
            ba2 = raw[..., 2:3*((self.w+1)>>1):3]
            ch0 = np.left_shift((np.left_shift(ba0, 4) + ba2 % 16), 4)
            ch1 = np.left_shift((np.left_shift(ba1, 4) + (np.right_shift(ba2, 4)) % 16), 4)
            mat = np.empty((self.h, self.w), dtype=ch0.dtype)